C++
CODING CHALLENGE
Define the classes that implement the following hierarchy with
Manager and Scientist each inheriting from both Employee and
Student.
Employee Student
△ △
| |
| |
+--------- Manager ----------+
| |
| |
+---------Scientist ---------+
C++
Define the classes that implement the following class hierarchy.
Use the keyword virtual to insure your classes do not suffer from
the diamond problem.
+---------▷ BaseClass ◁ ---------+
| |
| |
DerivedClass1 DerivedClass2
△ △
| |
| |
+----- DerivedFurtherClass1 ------+
| |
| |
+----- DerivedFurtherClass2 ------+
The feature by which a class derive properties and characteristics
from another class is called Inheritance.
Sub Class: The class that inherits properties from another class is
called Sub class.
Super Class:The class whose properties are inherited by sub class
is called SuperClass.
PROBLEM 1
#include <iostream>
using namespace std;
// first base class
class Employee
{
public:
Employee()
{
cout << "Employees are paid" << endl;
}
};
// second base class
class Student{
public:
Student()
{
cout << "Goes to school" << endl;
}
};
// sub class derived from two base classes
class Manager: public Employee, public Student
{
};
class Scientist: public Employee, public Student
{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Manager obj1;
Scientist obj2;
return 0;
}
PROBLEM 2
#include <iostream>
using namespace std;
class BaseClass // the superclass for all the sub classes below
{
public:
void display()
{
cout << "This is the BaseClass \n";
}
};
class DerivedClass1 : public virtual BaseClass //First derived
class //virtual keyword used so only copy of data is stored in
respective //class
{
};
class DerivedClass2 : public virtual BaseClass //2nd Derived
class
{
};
class FurtherDerived : public DerivedClass1, public
DerivedClass2
//new subclass derived from both 1st and 2nd derived classes
{
};
int main()
{
FurtherDerived Obj;
Obj.display();
}
Virtual base classes are used in inheritance for preventing multiple "instances" of a given class appearing in an inheritance when using multiple inheritances.
#plz upvote
C++ CODING CHALLENGE Define the classes that implement the following hierarchy with Manager and Scientist each...