Question

C++ CODING CHALLENGE Define the classes that implement the following hierarchy with Manager and Scientist each...

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 ------+

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

Add a comment
Know the answer?
Add Answer to:
C++ CODING CHALLENGE Define the classes that implement the following hierarchy with Manager and Scientist each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT