Question
This homework should get you back into shape with C++. It will focus on pointers, dynamic memory management, overloading operators and streams. I have created
This homework should get you back into shape with C++. It will focus on pointers, dynamic memory management, overloading operators and streams.
I have created the HW01 directory for you and included a starter kit CPersonStarterKit.tar.gz which has main.cpp, cperson.h, and an executable file sampleCPerson. The command to extract the files from the starter kit is:
tar xvzf CPersonStarterKit.tar.gz
As a note, you only need to decompress the starter kit once because if you do it again, it will overwrite the existing files (after you probably made changes to them and you will lose your work). Also, to run the sample executable file, type ./sampleCPerson and press the Enter key.
All you need to do is fill in the ??? in cperson.h and create a cperson.cpp file. You wont need to change anything in main.cpp. Get a feel for what the program is doing and study what has been given in main.cpp and cperson.h to know what the functions should have as input/output. Your code should mimic the output from the sample executable file.
Constructors
Your program should have three constructors (default, type, and copy). Also, it should have a destructor as well.
Default: The default constructor will initialize your private data members. Age is assigned a zero value and a name will be assigned to your choosing (I picked Clark Kent)
Type: Will create an object and assign the private data members to the choosing of the caller.
Copy: Will copy another object.
Destructor: Release memory from the heap.
Functions
GetAge / GetName: Will return the age and the name of the objects data members, respectively.
SetAge / SetName: Will assign the age and the name to the objects data members, respectively.
DisplayData: Will display the objects data members (Name and Age). By default, it will display to the monitor.
GetData: Will read the users input and assign them to the objects data members. By default, it will read from the keyboard.
operator==: Overload the equality operator
operator>: Overload the greater than operator
operator>>: Overload the extraction operator
operator<<: Overload the insertion operator
CPerson.h
// ============================================================================ // File: cperson.h // ============================================================================ // Programmer: ??? // Date: ??? // // Description: // This is the header file for the CPerson class. // ============================================================================ #include
#ifndef CPERSON_HEADER #define CPERSON_HEADER
class CPerson { public: // constructors CPerson(); CPerson(const char *name, int age = 0); CPerson(const CPerson &object); ~CPerson();
// accessor functions int GetAge() const {return m_age; } const char* GetName()const {return m_name;} void SetAge(int age); void SetName(const char *name);
// member functions ??? DisplayData(???) // default to cout ??? GetData(???) // default to cin ??? operator==(???) ??? operator>(???)
private: char *m_name; // a char pointer to store a name int m_age; // an int to store the age };
// function prototypes ??? operator>>(???)
??? operator<<(???)
main.cpp
#include
int main() { // Create an object (Default) CPerson P1;
// Invoke type constructor char name[] = "Elon Musk"; CPerson P2(name, 47);
// Copy Constructor CPerson P3(P2);
// Display the objects cout << " Here are the three people "; cout << "P1: " << P1 << endl; cout << "P2: " << P2 << endl; cout << "P3: " << P3 << endl << endl;
// Enter name and age for P1 cout << "Enter your name and age (Hit enter after entering name): "; cin >> P1;
// Display the objects again cout << " Here are the three people (revised): "; cout << "P1: " << P1 << endl; cout << "P2: " << P2 << endl; cout << "P3: " << P3 << endl << endl;
// Find who is older between P1 and P2 if (P1 == P2) { cout << P1.GetName() << " and " << P2.GetName() << " are the same age of " << P1.GetAge() << " years old "; } else if (P1 > P2) { cout << P1.GetName() << " is the oldest at age " << P1.GetAge() << " years old "; } else { cout << P2.GetName() << " is the oldest at age " << P2.GetAge() << " years old "; }
return 0;
} // end of "main"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started