Question
I have been asked to fill in a header file cperson.h and create a cperson.cpp file but i cant get the compiler to give me
I have been asked to fill in a header file cperson.h and create a cperson.cpp file but i cant get the compiler to give me .o file when executing cperson.cpp -c
i have include the .h file first and .cpp file second
i have yet to figure out how to call the DisplayData() and GetData() functions but i suspect it must be down in the overloaded stream operators
Any suggestions are appreciated
#include
#ifndef CPERSON_HEADER #define CPERSON_HEADER
class CPerson { public: // constructors CPerson();//default CPerson(char *name, int age);//type CPerson(const CPerson &PCopy);//copy ~CPerson();//destructor
// accessor functions int GetAge(); char* GetName(); void SetAge(int age); void SetName(char *name);
// member functions void DisplayData(); // default to cout void GetData(); // default to cin bool operator ==(const CPerson &rhs); bool operator >(const CPerson &rhs);
private: char *m_name; // a char pointer to store a name int m_age; // an int to store the age
};
// function prototypes istream &operator >>(istream &input, CPerson &rhs); ostream &operator <<(ostream &output, const CPerson &rhs); #endif // CPERSON_HEADER ---------------------------------------------------------------------------------------------------------------
#include
//constructor definitions CPerson::CPerson()//initiates private members m_name and m_age { m_name = new char[10]; SetName("Mr. Smith"); m_age = 0; }
CPerson::CPerson(char *name, int age)//type constructor { m_name = new char[strlen(name) + 1]; SetName(name); m_age = age; }
CPerson::CPerson(const CPerson &PCopy)//copy constructor { m_name = new char[strlen(PCopy.m_name) + 1]; SetName(PCopy.m_name); m_age = PCopy.m_age; }
CPerson::~CPerson() { delete [] m_name; }
//setter functions void CPerson::SetName(char *name) { delete [] m_name; m_name = new char[strlen(name) + 1]; strcpy(m_name.name); }
void CPerson::SetAge(int age) { m_age = age; } //getter functions char* CPerson::GetName() { return m_name; }
int CPerson::GetAge() { return m_age; } //overloaded operator bool CPerson::operator == (const CPerson &rhs) { if((strcmp(m_name, rhs.m_name) == 0) && (m_age == rhs.m_age)) { return true; } else { return false; } }
bool CPerson::operator > (const CPerson &rhs) { if(m_age > rhs.m_age) { return true; }
else { return false; } } //member functions void CPerson::DisplayData() { cout << "Name: " << GetName() << endl; cout << " Age: " << GetAge() << endl; cout << endl; }
void CPerson::GetData() { cin >> SetName() >> SetAge() << endl; } // function prototypes ostream &operator << (ostream &output, const CPerson &rhs) { output << rhs.GetName() << rhs.GetAge() << endl; return output; }
istream &operator >> (istream &input, CPerson &rhs) { input >> rhs.SetName() >> rhs.SetAge() >> endl; return input; }
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