Make a story like worker or game of using inheritance.

Question

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)

Summary

We have to explain the inheritance here. For which we have to create a Parent class and Child class. With the constructor and functions. So here we will take the program of Cash to explain the inheritance with different child classes such as Paper, Quarter, Penny, Sikka.

Explanation

Here we have taken the Class Cash as the parent/base class. With the attribute ‘value’ and value() as function name . also, this class contains the constructor. Next, we have created a class Paper which is the child class with a constructor and a function named called dollar(). The next class is class Sikka which also has a constructor. After this we have created a class Quarter with a constructor and a function name as quarter() which will return a value in Quarter. . Next class is Penny which also has a constructor and a function as penny(). Once all the conversation is done it will show us the output.

Code

#include <iostream> 
using namespace std;
/* amount in money */ 
class Cash
{ int amount; 
/* return amount */
int amount() 
{ return amount; }
/* class constructor */ 
Cash(int v) { amount = v; } };
class Paper : Cash 
{ /* constructor */
Paper(int d) : Cash(d)
{ } 
/* money to dollars */
float dollar() 
{ return amount / 100.0; } };
class Sikka : public Cash { 
/* constructor */
Sikka(int v) : Cash(v) { } }; 
class Quarter Sikka {
/* constructor */
Quarter() : Sikka(25) { }
Quarter(int n):Sikka(n){}
/* money to quarter */ 
float quarter()
{ return amount/17.0; } }; 
class Penny : Sikka
{
/* constructor */
Penny() : Sikka(1) { }
/* amount in penny */ 
Penny(int v):Sikka(v)
{} float penny()
{ return amount/.5705; }
}; 
/* Main code */ 
int main()
{
Paper b(10);
Penny p(10);
Quarter q(10); 
cout<<"In money: "<<p.amount()<<endl;
cout<<"In Dollars: "<<b.dollar()<<endl; 
cout<<"In Quarter: "<<q.quarter()<<endl;
cout<<"In penny: "<<p.penny()<<endl;
return 0; }

Output

game of using inheritance

 

Also read, define a class named StarterPokemon that contains.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *