Bear with me. There are 3 classes. Person is the base class with name and age. Child is a derived class with a grade in school. Parent is another derived class who can have a child (yes or no)
Before we continue there are a couple of things I must point out:
This is an exercise which I thought up so I can practise inheritance a bit. The idea is to end up with a vector which contains pointers from the base class to the derived classes objects.
The “program” depends on the user entering correct values, has no error checking and so on, but that is not the point of this exercise, so that is why I haven’t done anything about it.
Feedback on how to fix the problems I am getting is greatly appreciated. Thanks in advance.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person
{
private:
string m_name;
int m_age;
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
string get_name()
{
return m_name;
}
virtual void info() =0;
};
class Child : public Person
{
private:
int m_grade;
public:
Child(string name, int age, int grade) : Person(name, age)
{
m_grade = grade;
}
void info()
{
cout <<"I am a child. I go to the " << m_grade << " grade."<<endl;
}
};
class Parent : public Person
{
private:
bool m_child;
public:
Parent(string name, int age, bool child) : Person(name, age)
{
m_child = child;
}
void info()
{
if(m_child == true)
{
cout << "I have a child." << endl;
}
else
{
cout << "I do not have a child" << endl;
}
}
};
vector create_list(const int& x)
{
vector <Person> a;
for(int a = 0; a < x; a++)
{
cout << "enter the name" << endl;
string o;
cin >> o;
cout << "enter the age" << endl;
int age;
cin >> age;
cout << "What would you like your person to be: a Child or a Parent?" << endl;
string choice;
cin >> choice;
if(choice == "Child")
{
cout << "enter it's grade" << endl;
int grade;
cin >> grade;
Child* c = new Child(o, age, grade);
a.push_back(c);
}
else
{
cout <<"enter if the parent has a child (yes/no)" << endl;
string wc;
cin >> wc;
if(wc == "yes")
{
Parent* p = new Parent(o, age, true);
a.push_back(p);
}
else
{
Parent* p = new Parent(o, age, false);
a.push_back(p);
}
}
}
return a;
}
int main()
{
cout << "How many people would you like to create?" << endl;
int x;
cin >> x;
vector<Person> a = create_list(x);
a[0]->getname();
return 0;
}
You are using the same variable name
afor bothvector<Person>andintin yourfor loop. So when you get to the linea.push_back(c);the program will think thatais an integer, not a vector.Make your variable names unique.
As others have mentioned, your container is a
vectorof typePerson, but you instantiate new derived classes of typeChild *andParent *, so yourvectorshould be of typePerson*.On that same note, the return type of your function should read
vector<Person*>Although it is not necessary in this case since your application ends immediately, it is good practice to make sure every call to
newcorresponds to a call todelete. In that case you would write afree_listmethod which goes through and deletes each Person object pointed to in the list. Note that the vector itself requires no cleanup.