T
Tony Johansson
Hello Experts!!
Here we use multiple inheritance from two classes.We have a class named
Person at the very top
and below this class we have a Student class and an Employee class at the
same level.
There is a class TeachingAssistent that use multiple inheritance from both
Student and Employee.
There is a method named getName is class Person.
Now to my question which is a question about design.
Would it be any point to define this method getName as virtual so the
derived classes could override this method which would result to a possible
use of polymorfism.
Here are all the class definitions
*********************
#include <string>
using namespace std;
class Person
{
public:
Person(string nn = "default") : name(nn) {}
string getName() const
{
return name;
}
private:
string name;
};
class Student : public virtual Person
{
public:
Student(string nn="default") : Person(nn) {}
};
class Employee : public virtual Person
{
public:
Employee(string nn="default") : Person(nn) {}
};
class TeachingAssistent : public Student, public Employee
{
public:
TeachingAssistent(string nn="default") : Person(nn) {}
};
Here is main program
****************
#include <vector>
#include "person.h"
#include <iostream>
using namespace std;
int main()
{
vector<Person *> p;
p.push_back(new Student);
p.push_back(new Employee);
p.push_back(new TeachingAssistent);
for(int i=0; i < p.size(); i++)
cout << p->getName() << endl;
return 0;
}
Many thanks
//Tony
Here we use multiple inheritance from two classes.We have a class named
Person at the very top
and below this class we have a Student class and an Employee class at the
same level.
There is a class TeachingAssistent that use multiple inheritance from both
Student and Employee.
There is a method named getName is class Person.
Now to my question which is a question about design.
Would it be any point to define this method getName as virtual so the
derived classes could override this method which would result to a possible
use of polymorfism.
Here are all the class definitions
*********************
#include <string>
using namespace std;
class Person
{
public:
Person(string nn = "default") : name(nn) {}
string getName() const
{
return name;
}
private:
string name;
};
class Student : public virtual Person
{
public:
Student(string nn="default") : Person(nn) {}
};
class Employee : public virtual Person
{
public:
Employee(string nn="default") : Person(nn) {}
};
class TeachingAssistent : public Student, public Employee
{
public:
TeachingAssistent(string nn="default") : Person(nn) {}
};
Here is main program
****************
#include <vector>
#include "person.h"
#include <iostream>
using namespace std;
int main()
{
vector<Person *> p;
p.push_back(new Student);
p.push_back(new Employee);
p.push_back(new TeachingAssistent);
for(int i=0; i < p.size(); i++)
cout << p->getName() << endl;
return 0;
}
Many thanks
//Tony