c++ 1) make a story like worker or game of using inheritance // 2) you have to create 1 or more than 1 base class, and 2 or more than 2 derived class // 3) at least 1 constructor and 1 function should be included. // 4) use the class (not struct) #include using namespace std; class M
c++
1) make a story like worker or game of using inheritance
// 2) you have to create 1 or more than 1 base class, and 2 or more than 2 derived class
// 3) at least 1 constructor and 1 function should be included.
// 4) use the class (not struct)
#include <iostream>
using namespace std;
class Money {
protected:
int value;
public:
int getValue() { return value; }
Money(int v) { value = v; }
};
class Bill : public Money
{
public:
Bill(int d) : Money(d * 100) { }
int getDollar() { return value / 100; }
};
class Coin : public Money
{
public:
Coin(int v) : Money(v) { }
};
class Quarter : public Coin
{
public:
Quarter() : Coin(25) { }
};
class Penny : public Coin
{
public:
Penny() : Coin(1) { }
};
int main()
{
Quarter q;
Bill b(5);
cout << q.getValue() << endl;
cout << b.getDollar() << endl;
cout << b.getValue() << endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images