C++ help on polymorph

  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 22

Post 3+ Months Ago

I have been trying for a week to get this running and i keep getting the same errors can someone point out what i am doing wrong and how to correct it please. I had to change the email structure because the forum script thinks there real emails :roll:

Code: [ Select ]
#include <iostream>
#include <string>
#include <vector>

using namespace std;

//main class and functions below
class Person
{
public:
 Person();
 Person(string name, string address, string telephone, string email);
 string getname(){return name;}
 string getaddress(){return address;}
 string gettelephone(){return telephone;}
 string getemail(){return email;}
 virtual string whatami(){return "Person";}//virtual to combine inherit to other classes
 
private:
 string name;
 string address;
 string telephone;
 string email;
};

Person::Person()
{
 name = "";
 address = "";
 telephone = "";
 email ="";
} // constructor
  
Person::Person(string name, string address, string telephone, string email)
{
 this->name = name;
 cout << "Person name = " << name << endl;
 this->address = address;
 this->telephone = telephone;
 this->email=email;
}
  
class Student:virtual public Person//student to inherit virtual from Person class
{
public:
 Student(){};
 Student(string name, string address, string telephone, string email, string status);
 string getstatus(){return status;}
 string whatami(){return "Student";}
    
private:
 string status;
};
//function for student
Student::Student(string name, string address, string telephone, string email, string status):Person(name, address, telephone, email)
{
 cout << "Student name = " << name << endl;
 this->status=status;
}

class Employee:virtual public Person//employee to inherit from virtual Person
{
public:
 Employee(){};
 Employee(string name, string address, string telephone, string email, string office, string salary,string datehired);
 string getoffice(){return office;}
 string getsalary(){return salary;}
 string getdateHired(){return datehired;}
 string whatami(){return "Employee";}

private:
 string office;
 string salary;
 string datehired;
};
//Employee function
Employee::Employee(string name, string address, string telephone, string email, string office, string salary, string datehired):Person(name, address, telephone, email)
{
    
 cout << "Employee name = " << name << endl;
 this->office = office
 this->salary = salary;
 this->datehired = datehired;
}

class Staff:public Employee//Staff inherit from employee
{
public:
    Staff(){};
 Staff(string name, string address, string telephone, string email,string office,string salary,string datehired,string  jobtitle);
 string getjobtitle(){return jobtitle;}
 string whatami(){return "Staff";}
private:
 string jobtitle;

};
//staff function inherits from Employee and Person
Staff::Staff(string name, string address, string telephone, string email,string office, string salary, string datehired,string jobtitle):Employee(name,address,telephone,email,office,salary,datehired):Person(name,address,telephone,email)
{
 this->jobtitle=jobtitle;
}

class Faculty:public Employee//Faculty inherits from Employee and Person
{
public:
 Faculty(){};
 Faculty(string name, string address, string telephone, string email,string office,string salary, string datehired, string rank, string status);
    
 string getrank(){return rank;}
 string getstatus(){return status;}
 string whatami(){return "Faculty";}

private:
 string rank;
 string status;
};
//Facuty function
Faculty::Faculty(string name, string address, string telephone, string email,string office,string salary, string datehired, string rank, string status) : Employee(name,address,telephone,email,office,salary,datehired):Person(name,address,telephone,email)
{
 cout << "Faculty name = " << name << endl;
 this->rank = rank;
 this->status = status;
}
//class StaffST(a student that is also a Staff)
class StaffST:public Staff:public Student//combine staff and student using virtual in person class
{
public:
 StaffST(){};
 StaffST(string name, string address, string telephone,string email,string office,string salary,int credithours, string datehired,string jobtitle,string status);
 int getcredithours(){return credithours;}
 string whatami(){return "Staff/Student";}
  
 
private:
 int credithours;
};
//StaffST function to combine the Staff:Student:Person
StaffST::StaffST(string name, string address, string telephone,string email,string office,string salary,int credithours,string datehired,string jobtitle,string status)
:Staff(name,address,telephone,email,office,salary,datehired,jobtitle):Student(name,address,telephone,email,status):Person(name,address,telephone,email)
{
 cout << "StaffST name = " << name << endl;
 this->credithours=credithours;
}

int main()
{
  vector<Person*> v;
  v.push_back(new Person("John Adams","Boston","617-555-0000","john adams dot com"));
  v.push_back(new Employee("John Adams","Boston","617-555-0000","john dot adams com","office 201","100,000 yr","7/2/66"));
  v.push_back(new Student("John Quincy Adams","Boston","617-555-0000","johnq adams com","senior"));
  v.push_back(new Staff("Samuel Adams","Boston","617-555-BEER","sam dot adams com","brewhouse 1",1000,"9-15-1764","Brewer"));
  v.push_back(new Faculty("Joe Maker","1234 campus ln","760-902-41404","poly dot com","Docrate"));
  v.push_back(new StaffST("Samuel Smith","Boston","617-555-BEER","samsmith dot adams com","brewhouse
  5",100,"9-15-1774","Taster","junior"));

  //loop for my vector to get name and what person is
  for (int i=0; i<v.size(); i++)
   {
    cout << v[i]->getname() << " " << whatami(v[i]) << endl;
   }

  return 1;

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. //main class and functions below
  6. class Person
  7. {
  8. public:
  9.  Person();
  10.  Person(string name, string address, string telephone, string email);
  11.  string getname(){return name;}
  12.  string getaddress(){return address;}
  13.  string gettelephone(){return telephone;}
  14.  string getemail(){return email;}
  15.  virtual string whatami(){return "Person";}//virtual to combine inherit to other classes
  16.  
  17. private:
  18.  string name;
  19.  string address;
  20.  string telephone;
  21.  string email;
  22. };
  23. Person::Person()
  24. {
  25.  name = "";
  26.  address = "";
  27.  telephone = "";
  28.  email ="";
  29. } // constructor
  30.   
  31. Person::Person(string name, string address, string telephone, string email)
  32. {
  33.  this->name = name;
  34.  cout << "Person name = " << name << endl;
  35.  this->address = address;
  36.  this->telephone = telephone;
  37.  this->email=email;
  38. }
  39.   
  40. class Student:virtual public Person//student to inherit virtual from Person class
  41. {
  42. public:
  43.  Student(){};
  44.  Student(string name, string address, string telephone, string email, string status);
  45.  string getstatus(){return status;}
  46.  string whatami(){return "Student";}
  47.     
  48. private:
  49.  string status;
  50. };
  51. //function for student
  52. Student::Student(string name, string address, string telephone, string email, string status):Person(name, address, telephone, email)
  53. {
  54.  cout << "Student name = " << name << endl;
  55.  this->status=status;
  56. }
  57. class Employee:virtual public Person//employee to inherit from virtual Person
  58. {
  59. public:
  60.  Employee(){};
  61.  Employee(string name, string address, string telephone, string email, string office, string salary,string datehired);
  62.  string getoffice(){return office;}
  63.  string getsalary(){return salary;}
  64.  string getdateHired(){return datehired;}
  65.  string whatami(){return "Employee";}
  66. private:
  67.  string office;
  68.  string salary;
  69.  string datehired;
  70. };
  71. //Employee function
  72. Employee::Employee(string name, string address, string telephone, string email, string office, string salary, string datehired):Person(name, address, telephone, email)
  73. {
  74.     
  75.  cout << "Employee name = " << name << endl;
  76.  this->office = office
  77.  this->salary = salary;
  78.  this->datehired = datehired;
  79. }
  80. class Staff:public Employee//Staff inherit from employee
  81. {
  82. public:
  83.     Staff(){};
  84.  Staff(string name, string address, string telephone, string email,string office,string salary,string datehired,string  jobtitle);
  85.  string getjobtitle(){return jobtitle;}
  86.  string whatami(){return "Staff";}
  87. private:
  88.  string jobtitle;
  89. };
  90. //staff function inherits from Employee and Person
  91. Staff::Staff(string name, string address, string telephone, string email,string office, string salary, string datehired,string jobtitle):Employee(name,address,telephone,email,office,salary,datehired):Person(name,address,telephone,email)
  92. {
  93.  this->jobtitle=jobtitle;
  94. }
  95. class Faculty:public Employee//Faculty inherits from Employee and Person
  96. {
  97. public:
  98.  Faculty(){};
  99.  Faculty(string name, string address, string telephone, string email,string office,string salary, string datehired, string rank, string status);
  100.     
  101.  string getrank(){return rank;}
  102.  string getstatus(){return status;}
  103.  string whatami(){return "Faculty";}
  104. private:
  105.  string rank;
  106.  string status;
  107. };
  108. //Facuty function
  109. Faculty::Faculty(string name, string address, string telephone, string email,string office,string salary, string datehired, string rank, string status) : Employee(name,address,telephone,email,office,salary,datehired):Person(name,address,telephone,email)
  110. {
  111.  cout << "Faculty name = " << name << endl;
  112.  this->rank = rank;
  113.  this->status = status;
  114. }
  115. //class StaffST(a student that is also a Staff)
  116. class StaffST:public Staff:public Student//combine staff and student using virtual in person class
  117. {
  118. public:
  119.  StaffST(){};
  120.  StaffST(string name, string address, string telephone,string email,string office,string salary,int credithours, string datehired,string jobtitle,string status);
  121.  int getcredithours(){return credithours;}
  122.  string whatami(){return "Staff/Student";}
  123.   
  124.  
  125. private:
  126.  int credithours;
  127. };
  128. //StaffST function to combine the Staff:Student:Person
  129. StaffST::StaffST(string name, string address, string telephone,string email,string office,string salary,int credithours,string datehired,string jobtitle,string status)
  130. :Staff(name,address,telephone,email,office,salary,datehired,jobtitle):Student(name,address,telephone,email,status):Person(name,address,telephone,email)
  131. {
  132.  cout << "StaffST name = " << name << endl;
  133.  this->credithours=credithours;
  134. }
  135. int main()
  136. {
  137.   vector<Person*> v;
  138.   v.push_back(new Person("John Adams","Boston","617-555-0000","john adams dot com"));
  139.   v.push_back(new Employee("John Adams","Boston","617-555-0000","john dot adams com","office 201","100,000 yr","7/2/66"));
  140.   v.push_back(new Student("John Quincy Adams","Boston","617-555-0000","johnq adams com","senior"));
  141.   v.push_back(new Staff("Samuel Adams","Boston","617-555-BEER","sam dot adams com","brewhouse 1",1000,"9-15-1764","Brewer"));
  142.   v.push_back(new Faculty("Joe Maker","1234 campus ln","760-902-41404","poly dot com","Docrate"));
  143.   v.push_back(new StaffST("Samuel Smith","Boston","617-555-BEER","samsmith dot adams com","brewhouse
  144.   5",100,"9-15-1774","Taster","junior"));
  145.   //loop for my vector to get name and what person is
  146.   for (int i=0; i<v.size(); i++)
  147.    {
  148.     cout << v[i]->getname() << " " << whatami(v[i]) << endl;
  149.    }
  150.   return 1;


Here are my compiler error messages I am using xcode to compile and run

Code: [ Select ]

polymorph_practice.cpp:171:15: error: reference to type 'const value_type'
   (aka 'Person *const') could not bind to an rvalue of type 'Student *'
 ...new Student("John Quincy Adams","Boston","617-555-0000","johnq adams com","senior"));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
   passing argument to parameter '__x' here
  _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                               ^
polymorph_practice.cpp:172:15: error: reference to type 'const value_type'
   (aka 'Person *const') could not bind to an rvalue of type 'Staff *'
 ...new Staff("Samuel Adams","Boston","617-555-BEER","sam adams com","brewhouse 1",1000,"9-15-1764","Brewer"));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
   passing argument to parameter '__x' here
  _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                               ^
polymorph_practice.cpp:173:15: error: reference to type 'const value_type'
   (aka 'Person *const') could not bind to an rvalue of type 'Faculity *'
 ...new Faculity("name = John Holmes ","address = 121 Sant Fe Ave East Los Angeles ","Cell number = 213-211-5561 ","email = Homeboy Budwieser com ","The Sex Dr."));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
   passing argument to parameter '__x' here
  _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                               ^
polymorph_practice.cpp:174:15: error: reference to type 'const value_type'
   (aka 'Person *const') could not bind to an rvalue of type 'StaffST *'
 ...new StaffST("Samuel Smith","Boston","617-555-BEER","samsmith adams com","brewhouse 5",100,"9-15-1774","Taster","junior"));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
   passing argument to parameter '__x' here
  _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                               ^
polymorph_practice.cpp:178:40: error: no matching conversion for
   functional-style cast from 'value_type' (aka 'Person *') to 'Whatami'
  cout << v[i]->getname() << " " << Whatami(v[i]) << endl;
                    ^~~~~~~~~~~~
polymorph_practice.cpp:6:7: note: candidate constructor (the implicit copy
   constructor) not viable: no known conversion from 'value_type'
   (aka 'Person *') to 'const Whatami' for 1st argument; dereference the
   argument with *
class Whatami
   ^
polymorph_practice.cpp:6:7: note: candidate constructor (the implicit default
   constructor) not viable: requires 0 arguments, but 1 was provided
5 errors generated.
  1. polymorph_practice.cpp:171:15: error: reference to type 'const value_type'
  2.    (aka 'Person *const') could not bind to an rvalue of type 'Student *'
  3.  ...new Student("John Quincy Adams","Boston","617-555-0000","johnq adams com","senior"));
  4.    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
  6.    passing argument to parameter '__x' here
  7.   _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
  8.                                ^
  9. polymorph_practice.cpp:172:15: error: reference to type 'const value_type'
  10.    (aka 'Person *const') could not bind to an rvalue of type 'Staff *'
  11.  ...new Staff("Samuel Adams","Boston","617-555-BEER","sam adams com","brewhouse 1",1000,"9-15-1764","Brewer"));
  12.    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
  14.    passing argument to parameter '__x' here
  15.   _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
  16.                                ^
  17. polymorph_practice.cpp:173:15: error: reference to type 'const value_type'
  18.    (aka 'Person *const') could not bind to an rvalue of type 'Faculity *'
  19.  ...new Faculity("name = John Holmes ","address = 121 Sant Fe Ave East Los Angeles ","Cell number = 213-211-5561 ","email = Homeboy Budwieser com ","The Sex Dr."));
  20.    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
  22.    passing argument to parameter '__x' here
  23.   _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
  24.                                ^
  25. polymorph_practice.cpp:174:15: error: reference to type 'const value_type'
  26.    (aka 'Person *const') could not bind to an rvalue of type 'StaffST *'
  27.  ...new StaffST("Samuel Smith","Boston","617-555-BEER","samsmith adams com","brewhouse 5",100,"9-15-1774","Taster","junior"));
  28.    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:676:62: note:
  30.    passing argument to parameter '__x' here
  31.   _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
  32.                                ^
  33. polymorph_practice.cpp:178:40: error: no matching conversion for
  34.    functional-style cast from 'value_type' (aka 'Person *') to 'Whatami'
  35.   cout << v[i]->getname() << " " << Whatami(v[i]) << endl;
  36.                     ^~~~~~~~~~~~
  37. polymorph_practice.cpp:6:7: note: candidate constructor (the implicit copy
  38.    constructor) not viable: no known conversion from 'value_type'
  39.    (aka 'Person *') to 'const Whatami' for 1st argument; dereference the
  40.    argument with *
  41. class Whatami
  42.    ^
  43. polymorph_practice.cpp:6:7: note: candidate constructor (the implicit default
  44.    constructor) not viable: requires 0 arguments, but 1 was provided
  45. 5 errors generated.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 131 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.