Answer to Question #39679 in C++ for Calvin Thames

Question #39679
Declare two classes: Class and Student. Add data members for the name, the grade (integer as 0-F, 1-D, 2-C, 3-B, and 4-A), and the credit hours to the class Class. Add all proper constructors (at least two) and accessor functions.

For the class Student, add the name (string) and Total Grade Points (integer). Overload operator + that will update the Total Grade points for the student (add the Class's grade to the total grade points)
1
Expert's answer
2014-03-24T10:54:14-0400
#include <iostream>
#include <string>
using namespace std;

class Class
{

public:

Class(string x,int gr): m_Name(x), m_Grade(gr){}

Class(const Class& rhs): m_Name(rhs.m_Name), m_Grade(rhs.m_Grade) {}

virtual Class operator + (const Class& rhs)
{
Class b(m_Name + " and " + rhs.m_Name+" = ",m_Grade+rhs.m_Grade);
return b;
}

virtual void show()
{
cout << m_Name<<m_Grade << endl;
}

protected:

string m_Name;
int m_Grade;
};

class Student: public Class
{

public:

Student(string x,int gr)
: Class(x,gr) {}

Student(const Student& rhs)
: Class(rhs.m_Name,rhs.m_Grade) {}

Student(const Class& rhs)
: Class(rhs) {}


operator Class()
{
return Class(m_Name,m_Grade);
}

Student operator + (const Student& rhs)
{
Student d = static_cast<Class>(*this) + static_cast<Class>(rhs);
return d;
}

void show()
{
Class::show();
}


};

int main()
{
Student d1("First",1), d2("Second",2), d3 = d1 + d2;
d3.show();
cin.get();cin.get();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS