Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pointers in C++ MAIN pt.1: #include Student.h #include /******************************************************************************* * Function Prototypes *******************************************************************************/ Student* getInput(int*); void display(const Student*, const int*); /******************************************************************************* * int main() *

Pointers in C++

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

MAIN pt.1:

#include "Student.h" #include

/******************************************************************************* * Function Prototypes *******************************************************************************/

Student* getInput(int*); void display(const Student*, const int*);

/******************************************************************************* * int main() * Starting point of the program. * * Output: * An integer to signal to the OS the exit code. *******************************************************************************/

int main() { // variables int arraySize = 0; Student* objArray = nullptr;

// call the getInput function objArray = getInput(&arraySize);

// call the display function display(objArray, &arraySize);

// release the dynamic memory for the array delete[] objArray;

// terminate return 0; }

/******************************************************************************* * Student* getInput(int* sizePtr) * Let the user decide the size of the array. Use dynamic memory allocation to * create the array. Use a for loop to prompt the user for the name/age of each * student. Store the information in the appropriate object. * * Input: * sizePtr - a pointer to an integer that will hold the size of the array * * Output: * A pointer (array) to the Student objects. *******************************************************************************/

Student* getInput(int* sizePtr) { // temporary variables int t = 0; string s = "";

// for each student for (int i = 0; i

// prompt and store for the age of the current student cout > t;

// need to ignore the newline for the next iteration cin.ignore();

// store the data from the user into the current object in the array objArray[i].setName(s); objArray[i].setAge(t); } }

/******************************************************************************* * void display(const Student* objArray, const int* sizePtr) * Use a while loop to display the information about the students stored in the * dynamic array. * * Inputs: * objArray - a constant pointer (array) of Student objects * sizePtr - a constant pointer to an integer that represents the size of the * array *******************************************************************************/

void display(const Student* objArray, const int* sizePtr) { // counter variable int i = 0;

// iterate through each student while (i

// update the counter i++; } }

MAIN pt.2:

#include "CS_Student.h" #include "EnglishStudent.h"

/******************************************************************************* * Function Prototypes *******************************************************************************/

void getInput(CS_Student**, const int); void getInput(EnglishStudent**, const int); void display(Student*);

/******************************************************************************* * int main() * Starting point of the program. * * Output: * An integer to signal to the OS the exit code. *******************************************************************************/

int main() { // variables const int CS_SIZE = 2; const int ENG_SIZE = 3; CS_Student* csArray[CS_SIZE]; EnglishStudent* engArray[ENG_SIZE];

// call the getInput method overloads for both arrays getInput(csArray, CS_SIZE); getInput(engArray, ENG_SIZE);

// call the polymorphic display function for both arrays for (int i = 0; i

// release the dynamic memory for objects inside both arrays for (int i = 0; i

// terminate return 0; }

/******************************************************************************* * void getInput(CS_Student** objArray, const int SIZE) * Use a for loop to create dynamic CS student objects. Prompt the user for the * name/age/favorite programming language of each CS student. Store the * information in the dynamic object. * * Inputs: * objArray - a double-pointer that should be treated as an array of dynamic * CS student objects * SIZE - a constant integer that represents the size of the array *******************************************************************************/

void getInput(CS_Student** objArray, const int SIZE) { // variables string name = "", fpl = ""; int age = 0;

// for each CS student for (int i = 0; i

// prompt and store the name of the current CS student cout

// prompt and store for the age of the current CS student cout > age;

// need to ignore the newline in the buffer cin.ignore();

// prompt and store the favorite programming language of the current // CS student cout

// add the data to the dynamic object objArray[i]->setName(name); objArray[i]->setAge(age); objArray[i]->setFavProgLang(fpl); } }

/******************************************************************************* * void getInput(EnglishStudent** objArray, const int SIZE) * Use a for loop to create dynamic English student objects. Prompt the user * for the name/age/favorite book of each English student. Store the * information in the dynamic object. * * Inputs: * objArray - a double-pointer that should be treated as an array of dynamic * English student objects * SIZE - a constant integer that represents the size of the array *******************************************************************************/

void getInput(EnglishStudent** objArray, const int SIZE) { // variables string name = "", book = ""; int age = 0;

// for each English student for (int i = 0; i

// prompt and store the name of the current English student cout

// prompt and store for the age of the current English student cout > age;

// need to ignore the newline in the buffer cin.ignore();

// prompt and store the favorite book of the current English student cout

// add the data to the dynamic object objArray[i]->setName(name); objArray[i]->setAge(age); objArray[i]->setFavBook(book); } }

/******************************************************************************* * void display(Student* obj) * A polymorphic function. Simply calls the (virtual) print method. Used to * demonstrate polymorphism. Any derived class that inherits from the Student * class can be used as an argument. * * Input: * obj - a pointer to an object that inherits from the Student class *******************************************************************************/

void display(Student* obj) { obj->print(); }

For the first part of the lab, I want you to refactor the previous code from lab 1. First, modify the Student class to use dynamic memory allocation for its attributes. You need to use class separation (i.e., create a Student.h and a Student.cpp file) and have include guards. The UML diagram for the revised Student class is as follows: UML diagram for Student class Student - name : string* - age: int* + + + Student() +~Student() + setName(s: string): void + setAge(i: int) : void + getName() const: string + getAge() const : int + + Because the attributes are now meant to be dynamically allocated, you will need to handle requesting for the memory and releasing that memory within the class. Specifically, in the constructor, you should allocate the dynamic memory for both attributes. Similarly, in the destructor, you should release the dynamic memory for both attributes. Additionally, both the constructor and destructor should still have messages being displayed. For the constructor, make it display the message "Student object created!". For the destructor, make it display the message "Student object destroyed!". After creating the revised Student class, work on the main file . This file is similar to the one from lab 1. The difference now is that you are going to use pointers. The main function and the display function have already been written for you; do not modify them. Your task here is to implement the getInput function according to its description. If you managed to do this correctly, you should be able to replicate the following session: Enter how many students there are: 5 Student object created! Student object created! Student object created! Student object created! Student object created! Enter the name for student #1: Patricia Perez Enter the age for student #1: 29 Enter the name for student #2: Carmen Enter the age for student #2: 23 Enter the name for student #3: Albert DeLeon Enter the age for student #3: 16 Enter the name for student #4: Brandon W Perry Enter the age for student #4: 54 Enter the name for student #5: Nicole Henderson Enter the age for student #5: 30 Student #1 Name: Patricia Perez Age: 29 Student #2 Name: Carmen Age: 23 Student #3 Name: Albert DeLeon Age: 16 Student #4 Name: Brandon W Perry Age: 54 Student #5 Name: Nicole Henderson Age: 30 Student object destroyed! Student object destroyed! Student object destroyed! Student object destroyed! Student object destroyed! In the second part of the lab, you are going to adjust the Student class one more time. This change is necessary to make the Student class an abstract base class. The new UML diagram for the Student class is as follows: UML diagram for Student class Student -name: string* - age: int* + Student() + virtual -Student() + setName(s: string): void + setAge(i: int) : void + getName() const: string + getAge() const: int + virtual print() const : void = 0 The print method is a pure virtual method, which means the Student class does not implement the function. The other methods retain the same behavior as in part one. Now, you are going to create two derived classes that inherit from the Student class using the public access specifier. The first is the CS_Student class, whose UML is: UML diagram for CS_Student class CS_Student - favProgLang: string* +CS_Student() + virtual -CS_Student() + setFav ProgLang(s: string): void + getFavProgLang() const: string + virtual print() const override: void The methods for the CS_Student class are similar in behavior to the Student class. The constructor should print a message and dynamically allocate memory for its attribute. The destructor should print a message and release the dynamic memory for its attribute. The print method needs to be implemented for the class. You should print out the message "PRINT REPORT FOR CS_STUDENT OBJECT", then print out the information about the object. This includes the name, age, and favorite programming language. The second derived class is the English Student class, whose UML is: UML diagram for English Student class English Student - favBook:string* + English Student() + virtual ~English Student() + setFavBook(s: string) : void + getFavBook() const: string + virtual print() const override: void Just like with the CS Student class, the methods here are similar to the Student class. The print method is also similar, the difference being the first message should say "PRINT REPORT FOR ENGLISH STUDENT OBJECT", followed by the name, age, and favorite book of the object. . You do not need to modify anything in the file. I Once you have the inheritance figured out, download this main file recommend you look through it and understand what is happening. If you managed to do this correctly, you should be able to replicate the following session: Student object created! CS_Student object created! Enter the name for CS student #1: Patricia Perez Enter the age for CS student #1: 29 Enter the favorite programming language for CS student # 1: C++ (pre-17) Student object created! CS_Student object created! Enter the name for CS student #2: Carmen Enter the age for CS student #2: 23 Enter the favorite programming language for CS student # 2: Haskell Student object created! EnglishStudent object created! Enter the name for English student #1: Albert DeLeon Enter the age for English student #1: 16 Enter the favorite book for English student #1: 1984 Student object created! EnglishStudent object created! Enter the name for English student #2: Brandon W Perry Enter the age for English student #2: 54 Enter the favorite book for English student #2: Twilight Student object created! EnglishStudent object created! Enter the name for English student #3: Nicole Henderson Enter the age for English student #3: 30 Enter the favorite book for English student #3: House of Leaves PRINT REPORT FOR CS_STUDENT OBJECT Name: Patricia Perez Age: 29 Favorite Programming Language: C++ (pre-17) PRINT REPORT FOR CS_STUDENT OBJECT Name: Carmen Age: 23 Favorite Programming Language: Haskell PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Albert DeLeon Age: 16 Favorite Book: 1984 DDTNT PEDODT FOD ENCLICU STUDENT DECT PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Brandon W Perry Age: 54 Favorite Book: Twilight PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Nicole Henderson Age: 30 Favorite Book: House of Leaves CS_Student object destroyed! Student object destroyed! CS_Student object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed! For the first part of the lab, I want you to refactor the previous code from lab 1. First, modify the Student class to use dynamic memory allocation for its attributes. You need to use class separation (i.e., create a Student.h and a Student.cpp file) and have include guards. The UML diagram for the revised Student class is as follows: UML diagram for Student class Student - name : string* - age: int* + + + Student() +~Student() + setName(s: string): void + setAge(i: int) : void + getName() const: string + getAge() const : int + + Because the attributes are now meant to be dynamically allocated, you will need to handle requesting for the memory and releasing that memory within the class. Specifically, in the constructor, you should allocate the dynamic memory for both attributes. Similarly, in the destructor, you should release the dynamic memory for both attributes. Additionally, both the constructor and destructor should still have messages being displayed. For the constructor, make it display the message "Student object created!". For the destructor, make it display the message "Student object destroyed!". After creating the revised Student class, work on the main file . This file is similar to the one from lab 1. The difference now is that you are going to use pointers. The main function and the display function have already been written for you; do not modify them. Your task here is to implement the getInput function according to its description. If you managed to do this correctly, you should be able to replicate the following session: Enter how many students there are: 5 Student object created! Student object created! Student object created! Student object created! Student object created! Enter the name for student #1: Patricia Perez Enter the age for student #1: 29 Enter the name for student #2: Carmen Enter the age for student #2: 23 Enter the name for student #3: Albert DeLeon Enter the age for student #3: 16 Enter the name for student #4: Brandon W Perry Enter the age for student #4: 54 Enter the name for student #5: Nicole Henderson Enter the age for student #5: 30 Student #1 Name: Patricia Perez Age: 29 Student #2 Name: Carmen Age: 23 Student #3 Name: Albert DeLeon Age: 16 Student #4 Name: Brandon W Perry Age: 54 Student #5 Name: Nicole Henderson Age: 30 Student object destroyed! Student object destroyed! Student object destroyed! Student object destroyed! Student object destroyed! In the second part of the lab, you are going to adjust the Student class one more time. This change is necessary to make the Student class an abstract base class. The new UML diagram for the Student class is as follows: UML diagram for Student class Student -name: string* - age: int* + Student() + virtual -Student() + setName(s: string): void + setAge(i: int) : void + getName() const: string + getAge() const: int + virtual print() const : void = 0 The print method is a pure virtual method, which means the Student class does not implement the function. The other methods retain the same behavior as in part one. Now, you are going to create two derived classes that inherit from the Student class using the public access specifier. The first is the CS_Student class, whose UML is: UML diagram for CS_Student class CS_Student - favProgLang: string* +CS_Student() + virtual -CS_Student() + setFav ProgLang(s: string): void + getFavProgLang() const: string + virtual print() const override: void The methods for the CS_Student class are similar in behavior to the Student class. The constructor should print a message and dynamically allocate memory for its attribute. The destructor should print a message and release the dynamic memory for its attribute. The print method needs to be implemented for the class. You should print out the message "PRINT REPORT FOR CS_STUDENT OBJECT", then print out the information about the object. This includes the name, age, and favorite programming language. The second derived class is the English Student class, whose UML is: UML diagram for English Student class English Student - favBook:string* + English Student() + virtual ~English Student() + setFavBook(s: string) : void + getFavBook() const: string + virtual print() const override: void Just like with the CS Student class, the methods here are similar to the Student class. The print method is also similar, the difference being the first message should say "PRINT REPORT FOR ENGLISH STUDENT OBJECT", followed by the name, age, and favorite book of the object. . You do not need to modify anything in the file. I Once you have the inheritance figured out, download this main file recommend you look through it and understand what is happening. If you managed to do this correctly, you should be able to replicate the following session: Student object created! CS_Student object created! Enter the name for CS student #1: Patricia Perez Enter the age for CS student #1: 29 Enter the favorite programming language for CS student # 1: C++ (pre-17) Student object created! CS_Student object created! Enter the name for CS student #2: Carmen Enter the age for CS student #2: 23 Enter the favorite programming language for CS student # 2: Haskell Student object created! EnglishStudent object created! Enter the name for English student #1: Albert DeLeon Enter the age for English student #1: 16 Enter the favorite book for English student #1: 1984 Student object created! EnglishStudent object created! Enter the name for English student #2: Brandon W Perry Enter the age for English student #2: 54 Enter the favorite book for English student #2: Twilight Student object created! EnglishStudent object created! Enter the name for English student #3: Nicole Henderson Enter the age for English student #3: 30 Enter the favorite book for English student #3: House of Leaves PRINT REPORT FOR CS_STUDENT OBJECT Name: Patricia Perez Age: 29 Favorite Programming Language: C++ (pre-17) PRINT REPORT FOR CS_STUDENT OBJECT Name: Carmen Age: 23 Favorite Programming Language: Haskell PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Albert DeLeon Age: 16 Favorite Book: 1984 DDTNT PEDODT FOD ENCLICU STUDENT DECT PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Brandon W Perry Age: 54 Favorite Book: Twilight PRINT REPORT FOR ENGLISH STUDENT OBJECT Name: Nicole Henderson Age: 30 Favorite Book: House of Leaves CS_Student object destroyed! Student object destroyed! CS_Student object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed! EnglishStudent object destroyed! Student object destroyed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions