Directory:Derek Elder/Programs/MultipleInheritance
MyWikiBiz, Author Your Legacy — Tuesday November 26, 2024
< Directory:Derek Elder | Programs
Jump to navigationJump to searchRevision as of 23:04, 19 February 2011 by Derek Elder (talk | contribs) (MultipleInheritance program)
Main.cpp
#include <iostream> #include <fstream> #include <string> #include "Faculty.h" #include "GradStudent.h" #include "Person.h" #include "Student.h" #include "StudentVoter.h" #include "Voter.h" using namespace std; void clearScreen(); void Pause(); char displayMenuAndGetSelection(); void main() { int i, numDegrees; char menuChoice = ' '; bool done = false; string *fs, *fs2; fs = new string[2]; fs[0] = "History"; fs[1] = "Philosophy"; vector<string> vf; StudentVoter sv1("Harold",88,"000-00-0152",3.99f,"Independent"), sv2, sv4; StudentVoter sv3(sv1); Faculty f1("Lucas",50,"999-88-777",2,fs), f2, f4; Faculty f3(f1); while(!done) { menuChoice = displayMenuAndGetSelection(); clearScreen(); switch(menuChoice) { case '1': cout<<"Checking out the StudentVoter class.\n"; cout<<"\nStudentVoter sv1(\"Harold\",88,\"000-00-0152\",3.99f,\"Independent\")\n" <<"Checking out the constructor\n"; cout<<sv1; cout<<"\n----------------------"; cout<<"\nStudentVoter sv2, checking out the default constructor\n"; cout<<sv2; cout<<"\n----------------------"; cout<<"\nStudentVoter sv3(sv1), checking out the copy constructor\n"; cout<<sv3; cout<<"\n----------------------"; cout<<"\n\nHit enter to continue: "; cin.get(); sv4 = sv2; cout<<"\nStudentVoter sv4 = sv2, checking out the assignment operator\n"; cout<<sv4; cout<<"\n----------------------"; cout<<"\nChecking out the Accessors\n"; //cout<<"\nsv1.Name() = "<<sv1.Name(); //cout<<"\nsv1.Age() = "<<sv1.Age(); //cout<<"\nsv1.SSN() = "<<sv1.SSN(); //cout<<"\nsv1.GPA() = "<<sv1.GPA(); //cout<<"\nsv1.PartyAffiliation() = "<<sv1.PartyAffiliation(); cout<<"sv1.Type() = "<<sv1.Type(); cout<<"\n----------------------"; cout<<"\nBefore input for StudentVoter sv2.\n"; cin>>sv2; cout<<"\n----------------------"; cout<<"\nStudentVoter sv2 follows:\n"; cout<<sv2; Pause(); done = false; break; case '2': /*Mutator tests string fs3[3]; fs3[0] = string("Matchematics"); fs3[1] = string("English"); fs3[2] = string("Computer Sciene"); f1.Degrees(fs3,3); cout <<"\nAfter:" <<"\n string fs3[3];" <<"\n fs3[0] = string("Matchematics"); <<"\n fs3[1] = string("English"); <<"\n fs3[2] = string("Computer Sciene"); <<"\n f1.Degrees(fs3,3);" <<"\nf1 follows:\n"<<f1 //--------------------------------------------- fs[0] = string("Writing"); fs[1] = string("Business"); const Faculty f5("George Shaw",65,"294-32-2952",1.1,fs,2); cout << "\nAfter:" << "\n string fs[2];" << "\n fs[0] = string("Writing"); << "\n fs[1] = string("Business"); << "\n const Faculty f5("George Shaw",65,"294-32-2952",1.1,fs,2); << "\nf5 follows:\n"<<f5;*/ cout<<"Checking out the Faculty class.\n"; cout<<"\nFaculty f1(\"Lucas\",50,\"999-88-777\",2,fs), checking out the constructor\n"; cout<<f1; cout<<"\nContents of String *fs follows:\n"; cout<<"fs[0] = "<<fs[0]; cout<<"\n"; cout<<"fs[1] = "<<fs[1]; cout<<"\n----------------------"; cout<<"\nFaculty f2, checking out the default constructor\n"; cout<<f2; cout<<"\n----------------------"; cout<<"\nFaculty f3(f1), checking out the copy constructor\n"; cout<<f3; cout<<"\n----------------------"; cout<<"\n\nHit enter to continue: "; cin.get(); f4 = f2; cout<<"\nFaculty f4 = f2, checking out the assignment operator\n"; cout<<f4; cout<<"\n----------------------"; cout<<"\nChecking out the Accessors\n"; //cout<<"\nf1.Name() = "<<f1.Name(); //cout<<"\nf1.Age() = "<<f1.Age(); //cout<<"\nf1.SSN() = "<<f1.SSN(); //cout<<"\nf1.GPA() = "<<f1.GPA(); cout<<"\nf1.NumberOfDegrees() = "<<f1.NumberOfDegrees(); fs2 = f1.Degrees(numDegrees); cout<<"\nAfter string *fs2 = f1.Degrees(numDegrees)"; for(i = 0;i < numDegrees;i++) cout<<"\nDegree Number "<<(i+1)<<": "<<fs2[i]; cout<<"\nf1.Type() = "<<f1.Type(); vf = f1.Degrees(); //Vector testing cout<<"\nAfter vf = f1.Degrees();"; cout<<"\nvf.size() = "<<vf.size(); for(i = 0;i < static_cast<int>(vf.size());i++) cout<<"\nDegree Number "<<(i+1)<<": "<<vf[i]; cout<<"\n----------------------"; cout<<"\nBefore input for Faculty f2.\n"; cin>>f2; cout<<"\n----------------------"; cout<<"\nFaculty f2 follows:\n"; cout<<f2; Pause(); done = false; break; case '3': cout<<"Program terminated, goodbye."; cin.get(); done = true; break; default: cout<<"Incorrect choice selected"<<endl; Pause(); done = false; break; } } delete[] fs; delete[] fs2; } void clearScreen() { system("cls"); } void Pause() { cout<<"\n\nPress 'ENTER' to return to the main menu...\n"; cin.get(); } char displayMenuAndGetSelection() { char choice; clearScreen(); cout<<"\n\n\n"; cout<<"'1' -- Checking out the StudentVoter class.\n\n"; cout<<"'2' -- Checking the Faculty class.\n\n"; cout<<"'3' -- Quit the program.\n\n"; cin>>choice; cin.ignore(50,'\n'); return choice; }
Person.h
#pragma once #include <iostream> #include <string> using namespace std; class Person { private: char* m_name; int m_age; string m_ssn; public: Person(); Person(char* name, int age, string ssn); Person(const Person &p); virtual ~Person(); Person& operator=(const Person& p); void Name(const char* const name); string Name() const; void Age(int age); int Age() const; void SSN(string ssn); string SSN() const; string virtual Type() const; virtual void Read(istream& is); virtual void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const Person& p); istream& operator>>(istream& is, Person& p);
Person.cpp
#include <iostream> #include "Person.h" using namespace std; Person::Person() { int i; m_name = new char[3]; for(i = 0;i < 2; i++) m_name[i] = '*'; m_name[2] = '\0'; m_age = 0; m_ssn = "***-**-****"; } Person::Person(char* name, int age, string ssn) { int i; m_name = new char[(int)strlen(name)+1]; for(i = 0;i <= (int)strlen(name);i++) m_name[i] = name[i]; m_age = age; m_ssn = ssn; } Person::Person(const Person &p) { int i; m_name = new char[((int)strlen(p.m_name)+1)]; for(i = 0;i <= (int)strlen(p.m_name);i++) m_name[i] = p.m_name[i]; m_age = p.m_age; m_ssn = p.m_ssn; } Person::~Person() { if(m_name != NULL) { delete[] m_name; } } Person& Person::operator=(const Person& p) { if(this != &p) { int i; delete[] m_name; m_name = new char[((int)strlen(p.m_name)+1)]; for(i = 0; i <= (int)strlen(p.m_name);i++) m_name[i] = p.m_name[i]; m_age = p.m_age; m_ssn = p.m_ssn; } return *this; } void Person::Name(const char* const name) { int i; delete[] m_name; m_name = new char[((int)strlen(name)+1)]; for(i = 0;i <= (int)strlen(name);i++) m_name[i] = name[i]; } string Person::Name() const { /*int i for(i = 0;i <= (int)strlen(m_name);i++) cout<<m_name[i];*/ return string(m_name); } void Person::Age(int age) { m_age = age; } int Person::Age() const { return m_age; } void Person::SSN(string ssn) { m_ssn = ssn; } string Person::SSN() const { return m_ssn; } string Person::Type() const { return string("This is a Person Object"); } void Person::Read(istream& is) { string name; if(is == cin) { cout<<"Enter the name: "<<flush; getline(is,name); cout<<"Enter the age: "<<flush; is>>m_age; cout<<"Enter the social security number: "<<flush; is>>m_ssn; is.get(); } else { getline(is,name); if(!is) return; if(name.length() == 0) getline(is,name); is>>m_age; is>>m_ssn; is.get(); } int i; delete[] m_name; m_name = new char[(name.length()+1)]; for(i = 0;i < (int)name.length();i++) m_name[i] = name[i]; m_name[name.length()] = '\0'; } void Person::Write(ostream& os) const { if(os == cout) os<<"Name: "<<m_name<<"\nAge: "<<m_age<<"\nSSN: "<<m_ssn; else os<<m_name<<'\n'<<m_age<<'\n'<<m_ssn; } ostream& operator<<(ostream& os, const Person& p) { p.Write(os); return os; } istream& operator>>(istream& is, Person& p) { p.Read(is); return is; }
Student.h
#pragma once #include <iostream> #include <string> #include "Person.h" using namespace std; class Student : public virtual Person { private: float m_gpa; public: Student(); Student(char* name, int age, string ssn, float gpa); Student(const Student &s); const Student& operator=(const Student& s); float GPA() const; void GPA(float gpa); string Type() const; void Read(istream& is); void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const Student& s); istream& operator>>(istream& is, Student& s);
Student.cpp
#include <iostream> #include "Student.h" using namespace std; Student::Student() : Person(), m_gpa(0.0) {} Student::Student(char* name, int age, string ssn, float gpa) : Person(name, age, ssn), m_gpa(gpa) {} Student::Student(const Student &s) : Person(s), m_gpa(s.m_gpa) {} const Student& Student::operator=(const Student& s) { if(this != &s) { Person::operator=(s); m_gpa = s.m_gpa; } return *this; } float Student::GPA() const { return m_gpa; } void Student::GPA(float gpa) { m_gpa = gpa; } string Student::Type() const { return string("This is a Student Object"); } void Student::Read(istream& is) { Person::Read(is); if(is == cin) { cout<<"Enter the GPA: "<<flush; is>>m_gpa; is.get(); } else { is>>m_gpa; is.get(); } } void Student::Write(ostream& os) const { Person::Write(os); if(os == cout) os<<"\nGPA: "<<m_gpa; else os<<'\n'<<m_gpa; } ostream& operator<<(ostream& os, const Student& s) { s.Write(os); return os; } istream& operator>>(istream& is, Student& s) { s.Read(is); return is; }
Voter.h
#pragma once #include <iostream> #include <string> #include "Person.h" using namespace std; class Voter : public virtual Person { private: string m_partyAffiliation; public: Voter(); Voter(char * name, int age, string ssn, string partyAffiliation); Voter(const Voter &v); const Voter& operator=(const Voter& v); void PartyAffiliation(string partyAffiliation); string PartyAffiliation() const; string Type() const; void Read(istream& is); void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const Voter& v); istream& operator>>(istream& is, Voter& v);
Voter.cpp
#include <iostream> #include "Voter.h" using namespace std; Voter::Voter() : Person(), m_partyAffiliation("") {} Voter::Voter(char* name, int age, string ssn, string partyAffiliation) : Person(name, age, ssn), m_partyAffiliation(partyAffiliation) {} Voter::Voter(const Voter &v) : Person(v), m_partyAffiliation(v.m_partyAffiliation) {} const Voter& Voter::operator=(const Voter& v) { if(this != &v) { Person::operator=(v); m_partyAffiliation = v.m_partyAffiliation; } return *this; } void Voter::PartyAffiliation(string partyAffiliation) { m_partyAffiliation = partyAffiliation; } string Voter::PartyAffiliation() const { return m_partyAffiliation; } string Voter::Type() const { return string("This is a Voter Object"); } void Voter::Read(istream& is) { Person::Read(is); if(is == cin) { cout<<"Enter the party affiliation: "<<flush; is>>m_partyAffiliation; is.get(); } else is>>m_partyAffiliation; } void Voter::Write(ostream& os) const { Person::Write(os); if(os == cout) os<<"\nParty affiliation: "<<m_partyAffiliation; else os<<'\n'<<m_partyAffiliation; } ostream& operator<<(ostream& os, const Voter& v) { v.Write(os); return os; } istream& operator>>(istream& is, Voter& v) { v.Read(is); return is; }
StudentVoter.h
#pragma once #include <iostream> #include <string> #include "Student.h" #include "Voter.h" using namespace std; class StudentVoter : public Student, public Voter { private: //No additional data. public: StudentVoter(); StudentVoter(char* name, int age, string ssn, float gpa, string partyAffiliation); StudentVoter(const StudentVoter& sv); const StudentVoter& operator=(const StudentVoter& sv); string Type() const; void Read(istream& is); void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const StudentVoter& sv); istream& operator>>(istream& is, StudentVoter& sv);
StudentVoter.cpp
#include <iostream> #include "StudentVoter.h" using namespace std; StudentVoter::StudentVoter() : Student(), Voter(), Person() {} StudentVoter::StudentVoter(char* name, int age, string ssn, float gpa, string partyAffiliation) : Student(name,age,ssn,gpa), Voter(name,age,ssn,partyAffiliation), Person(name,age,ssn) {} StudentVoter::StudentVoter(const StudentVoter& sv) : Student(sv), Voter(sv), Person(sv) {} const StudentVoter& StudentVoter::operator=(const StudentVoter& sv) { if(this != &sv) { Student::operator=(sv); PartyAffiliation(sv.PartyAffiliation()); } return *this; } string StudentVoter::Type() const { return string("This is a StudentVoter Object"); } void StudentVoter::Read(istream& is) { string partyaffiliation; Student::Read(is); if (is == cin) { cout<<"Enter the party affiliation: "<<flush; is>>partyaffiliation; is.get(); } else { is>>partyaffiliation; is.get(); } PartyAffiliation(partyaffiliation); } void StudentVoter::Write(ostream& os) const { Student::Write(os); if (os == cout) os<<"\nParty affiliation: "<<PartyAffiliation(); else os<<'\n'<<PartyAffiliation(); } ostream& operator<<(ostream& os, const StudentVoter& sv) { sv.Write(os); return os; } istream& operator>>(istream& is, StudentVoter& sv) { sv.Read(is); return is; }
Faculty.h
#pragma once #include <iostream> #include <string> #include <vector> #include "Person.h" using namespace std; class Faculty : public Person { private: int m_numberOfDegrees; string* m_degrees; public: Faculty(); Faculty(char* name, int age, string ssn, int numberOfDegrees, string* degrees); Faculty(const Faculty &f); ~Faculty(); const Faculty& operator=(const Faculty& f); void NumberOfDegrees(int degrees); int NumberOfDegrees() const; void Degrees(const string* degrees, int numberOfDegrees); string* Degrees(int &numberOfDegrees) const; void Degrees(const vector<string> &vs); vector<string> Degrees() const; string Type() const; void Read(istream& is); void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const Faculty& f); istream& operator>>(istream& is, Faculty& f);
Faculty.cpp
#include <iostream> #include "Faculty.h" using namespace std; Faculty::Faculty() : Person(), m_numberOfDegrees(0) { int i; m_degrees = new string[3]; for(i = 0;i < 2; i++) m_degrees[i] = '*'; m_degrees[2] = '\0'; } Faculty::Faculty(char* name, int age, string ssn, int numberOfDegrees, string* degrees) : Person(name, age, ssn), m_numberOfDegrees(numberOfDegrees) { if(m_numberOfDegrees > 0) { int i; m_degrees = new string[m_numberOfDegrees]; for(i = 0;i < m_numberOfDegrees;i++) m_degrees[i] = degrees[i]; } else m_degrees = NULL; } Faculty::Faculty(const Faculty& f) : Person(f), m_numberOfDegrees(f.m_numberOfDegrees) { if(m_numberOfDegrees < 1) m_degrees = NULL; else { int i; m_degrees = new string[m_numberOfDegrees]; for (i = 0;i < m_numberOfDegrees;i++) m_degrees[i] = f.m_degrees[i]; } } Faculty::~Faculty() { if(m_degrees != NULL) { delete[] m_degrees; } } const Faculty& Faculty::operator=(const Faculty& f) { if(this != &f) { Person::operator=(f); m_numberOfDegrees = f.m_numberOfDegrees; } return *this; } void Faculty::NumberOfDegrees(int numberOfDegrees) { m_numberOfDegrees = numberOfDegrees; } int Faculty::NumberOfDegrees() const { return m_numberOfDegrees; } void Faculty::Degrees(const string* degrees, int numberOfDegrees) { delete[] m_degrees; m_numberOfDegrees = numberOfDegrees; if(m_numberOfDegrees < 1) m_degrees = NULL; else { int i; m_degrees = new string[m_numberOfDegrees]; for (i = 0;i < m_numberOfDegrees;i++) m_degrees[i] = degrees[i]; } } string* Faculty::Degrees(int &numberOfDegrees) const { string* temp; numberOfDegrees = m_numberOfDegrees; if(m_numberOfDegrees < 1) temp = NULL; else { int i; temp = new string[numberOfDegrees]; for(i = 0;i < m_numberOfDegrees;i++) temp[i] = m_degrees[i]; } return temp; } void Faculty::Degrees(const vector<string> &vs) { delete [] m_degrees; m_numberOfDegrees = static_cast<int>(vs.size()); if(m_numberOfDegrees < 1) m_degrees = NULL; else { int i; m_degrees = new string[m_numberOfDegrees]; for(i = 0;i < m_numberOfDegrees;i++) m_degrees[i] = vs[i]; } } vector<string> Faculty::Degrees() const { vector<string> temp; if(m_numberOfDegrees > 0) { int i; for(i = 0;i < m_numberOfDegrees;i++) temp.push_back(m_degrees[i]); } return temp; } string Faculty::Type() const { return string("This is a Faculty Object"); } void Faculty::Read(istream& is) { Person::Read(is); delete[] m_degrees; if(is == cin) { cout<<"Enter the number of degrees: "<<flush; is>>m_numberOfDegrees; is.get(); } else { is>>m_numberOfDegrees; is.get(); } if(m_numberOfDegrees < 1) m_degrees = NULL; else { int i; m_degrees = new string[m_numberOfDegrees]; for (i = 0;i < m_numberOfDegrees;i++) { if (is == cin) cout<<"Enter the name of degree "<<(i+1)<<": "<<flush; getline(is,m_degrees[i]); } } } void Faculty::Write(ostream& os) const { Person::Write(os); if(os == cout) os<<"\nNumber of Degrees: "<<m_numberOfDegrees; else os<<'\n'<<m_numberOfDegrees; if(m_numberOfDegrees < 1) return; else { int i; for (i = 0;i < m_numberOfDegrees;i++) { if(os == cout) os<<"\nDegree "<<(i+1)<<": "<<m_degrees[i]; else os<<'\n'<<m_degrees[i]; } } } ostream& operator<<(ostream& os, const Faculty& f) { f.Write(os); return os; } istream& operator>>(istream& is, Faculty& f) { f.Read(is); return is; }
GradStudent.h
#pragma once #include <iostream> #include <string> #include "Student.h" using namespace std; class GradStudent : public Student { private: string m_researchArea; public: GradStudent(); GradStudent(char * name, int age, string ssn, float gpa, string researchArea); GradStudent(const GradStudent &gs); const GradStudent& operator=(const GradStudent& gs); string ResearchArea() const; void ResearchArea(string researcharea); string Type() const; void Read(istream& is); void Write(ostream& os) const; }; ostream& operator<<(ostream& os, const GradStudent& gs); istream& operator>>(istream& is, GradStudent& gs);
GradStudent.cpp
#include <iostream> #include "GradStudent.h" using namespace std; GradStudent::GradStudent() : Student(), m_researchArea("") {} GradStudent::GradStudent(char * name, int age, string ssn, float gpa, string researchArea) : Student(name, age, ssn, gpa), m_researchArea(researchArea) {} GradStudent::GradStudent(const GradStudent& gs) : Student(gs), m_researchArea(gs.m_researchArea) {} const GradStudent& GradStudent::operator=(const GradStudent& gs) { if(this != &gs) { Student::operator=(gs); m_researchArea = gs.m_researchArea; } return *this; } string GradStudent::ResearchArea() const { return m_researchArea; } void GradStudent::ResearchArea(string researchArea) { m_researchArea = researchArea; } string GradStudent::Type() const { return string("This is a GradStudent Object"); } void GradStudent::Read(istream& is) { Student::Read(is); if(is == cin) { cout<<"Enter the Research area: "<<flush; is>>m_researchArea; is.get(); } else { is>>m_researchArea; is.get(); } } void GradStudent::Write(ostream& os) const { Student::Write(os); if(os == cout) os<<"\nResearch area: "<<m_researchArea; else os<<'\n'<<m_researchArea; } ostream& operator<<(ostream& os, const GradStudent& gs) { gs.Write(os); return os; } istream& operator>>(istream& is, GradStudent& gs) { gs.Read(is); return is; }