troubleshooting Question

virtual inheritance in C++

Avatar of kuntilanak
kuntilanakFlag for United States of America asked on
C++
9 Comments4 Solutions558 ViewsLast Modified:
In the code below what is the purpose of having the TA inherit from a virtual Teacher and virtual Student?
#include <cstdio>
#include <string>
using namespace std;

class Teacher
{
public:
        Teacher( string _name ) : name( _name ) { }
        virtual ~Teacher( ) { }
        virtual string getName( ) { return name; }
protected:
        string name;
};

class Student
{
public:
        Student( string _name ) : name( _name ) { }
        virtual ~Student( ) { }
        virtual string getName( ) { return name; }
protected:
        string name;
};

class TA : virtual public Teacher, virtual public Student
{
public:
        TA( string name ) : Teacher( "Prof. " + name ), Student( name ) { }
        virtual ~TA( ) { }
        virtual string getName( ) { return Teacher::getName( ); }
        string getStudentName( ) { return Student::getName( ); }
        string getTeacherName( ) { return Teacher::getName( ); }
};
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 4 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros