Headlines
Loading...
Write a C++ program to implement Inheritance

Write a C++ program to implement Inheritance

Write a C++ program to implement Inheritance 
Multi level 
Multiple
Hierarchical 
Hybrid with use of virtual
Use of constructors in inheritance


PROGRAM:


MULTILEVEL

#include<iostream.h>
#include<conio.h>
#include<string.h>
const int max_len=25;

class person
{
private : 
char name[max_len];
char sex;
int age;
public : 
void readperson()
{
                cout<<"www.adkool.com";
cout<<"\nEnter Name : ";
cin>>name;
cout<<"\nEnter sex : ";
cin>>sex;
cout<<"\nEnter age : ";
cin>>age;
}
void displayperson()
{
cout<<"Name : "<<name<<endl;
cout<<"Sex : "<<sex<<endl;
cout<<"age : "<<age<<endl;
                cout<<"www.adkool.com";
       }
};
class sports : public virtual person   //virtual class
{
private : 
char name[max_len];//name of game
int score;         //score awarded for result declaration
protected : 
void readdata()
{
                        cout<<"www.adkool.com";
cout<<"\nEnter game played : ";
cin>>name;
cout<<"\nEnter game score : ";
cin>>score;
       }
       void displaydata()
       {
cout<<"\nSports played : "<<name<<endl;
cout<<"Game score : "<<score<<endl;
      }
      int sportsscore()
     {
return score;
      }
};
class student : public virtual person   //virtual class
{
  private : 
int rollno;
char branch[30];
 public : 
void readstudentdata()
{
               cout<<"www.adkool.com";
cout<<"\nEnter rollno : ";
cin>>rollno;
cout<<"\nEnter branch in studying : ";
cin>>branch;
}
void putstudentdata()
{
                 cout<<"www.adkool.com";
cout<<"\nRollno : "<<rollno;
cout<<"\nBranch : "<<branch;
}
};
class exam  :   public student
{
protected : 
int sub1marks,sub2marks;
public : 
void readdata()
{
cout<<"\nEnter scored in subject1 : ";
cin>>sub1marks;
cout<<"\nEnter scored in subject2 : ";
cin>>sub2marks;
}
void displaydata()
{
cout<<"\nInternal marks scored in subject1 : "<<sub1marks;
cout<<"\nInternal marks scored in subject2 : "<<sub2marks;
cout<<"\nTotal marks scored : "<<totalmarks()<<endl;
}
int totalmarks()
{
return(sub1marks+sub2marks);
}
};
class result :  public exam ,public sports
{
private : 
int total;
public : 
void readdata()
{
readperson(); //access person class member
student :  : readstudentdata();
exam :  : readdata();//uses readdata of exam class
sports :  : readdata();
}
void displaydata()
{
displayperson();
student :  : putstudentdata();
exam :  : displaydata();
sports :  : displaydata();
cout<<"\nOverall performance,(exam+sports) : "
     <<percentage()<<"%";
            }
            int percentage()
           {
return(exam :  : totalmarks()+sportsscore())/3;
           }
};

void main()
{
clrscr();
result student;
cout<<"\nEnter data for student..."<<endl;
student.readdata();
cout<<"\n------------------------";
cout<<"\nStudent details...\n"<<endl;
student.displaydata();

getch();
}


OUTPUT

Enter data for student...
Enter Name : Raj
Enter sex : M
Enter age : 22
Enter rollno : 4
Enter branch in studying : Computer-science
Enter scored in subject1 : 92
Enter scored in subject2 : 88
Enter gamed played : cricket
Enter game score : 85
------------------------
Student details...

Name : Raj
Sex : M
age : 22

ROllno : 4
Branch : Computer-science
Internal marks scored in subject1 : 92
Internal marks scored in subject2 : 88
Total marks scored : 180

Sports played : cricket
Game score : 85
Overall performance,(exam+sports) : 88%



MULTIPLE  INHERITANCE

/* To calculate Sum Of Series x + x^3/3! - x^5/5! + x^7/7! ... */

#include<iostream.h>
#include<conio.h>

class A1 // Base class 1
{
public : 
int power(int x,int y) // to calculate the power of x
{
int p=1;
for(int i=1;i<=y;i++)
{
p=p*x;
}
return p;
}
};

class A2 // Base class 2
{
public : 
int factorial(int x)     // to find the factorialfor denominator of x
{
int fact=1,i;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
};

class A3 : public A1,public A2 // derived class inherits from both the base classes
{
public : 
float series(int x,int n)
{
float ans=0; int y=3; ans=x; // Default values

for(int i=1;i<=n-1;i++)
{
if(i%2==0)
{
ans=ans-1.0*power(x,y)/factorial(y);
// if even position..(-) sign
}
else
{
ans=ans+1.0*power(x,y)/factorial(y);
// if odd position..(+) sign
}
y=y+2; // increments in odd numbers
}
return ans;
}
};

void main()
{
int x,n;
clrscr();
cout<<"Enter the values of X  :  ";
cin>>x;
cout<<"\nFor how many elements  :  ";
cin>>n;
A3 obj;
float k = obj.series(x,n); // Calls series function of class A3
cout<<"\nSeries sum = "<<k;
getch();
}


OUTPUT

Enter the values of X  :  2

For how many elements  :  3

Series sum = 3.066667


HIERARCHICAL INHERITANCE

#include <iostream.h>
#include <conio.h>

const int LEN = 80;                //maximum length of names

class employee    // Base class
{
private : 
char name[LEN];              //employee name
unsigned long number;        //employee number
public : 
void getdata()
{
cout << "\n   Enter name :  ";   cin >> name;
cout << "   Enter number :  ";      cin >> number;
}
void putdata() const
{
cout << "\n   Name :  " << name;
cout << "\n   Number :  " << number;
}
};

class manager  :  public employee    //manager class
{
private : 
char title[LEN];             //"vice-president" etc.
double dues;                 //golf club dues
public : 
void getdata()
{
employee :  : getdata();
cout << "   Enter title :  ";          cin >> title;
cout << "   Enter golf club dues :  "; cin >> dues;
}
void putdata() const
{
employee :  : putdata();
cout << "\n   Title :  " << title;
cout << "\n   Golf club dues :  " << dues;
}
};

class scientist  :  public employee  //scientist class
{
private : 
int pubs;                    //number of publications
public : 
void getdata()
{
employee :  : getdata();
cout << "   Enter number of pubs :  "; cin >> pubs;
}
void putdata() const
{
employee :  : putdata();
cout << "\n   Number of publications :  " << pubs;
}
};

class foreman  :  public employee    //foreman class
{
private : 
float quotas;  //percent of quotas met successfully
public : 
void getdata()
{
cout << "\n Enter quotas :  "; cin >> quotas;
}
void putdata() const
{
cout << "\n   Quotas :  " << quotas;
}
};

void main()
{
employee e1;
manager m1;
foreman f1;
scientist s1;
clrscr();
cout << endl;

cout << "\nEnter data for Manager  :  ";
m1.getdata();
cout << endl;

cout << "\nEnter data for Scientist  :  ";
s1.getdata();
cout << endl;

cout << "\nEnter data for foreman  :  ";
f1.getdata();
cout << endl;
cout << "----------------------------------------";
cout << "\nData on Manager  :  ";
m1.putdata();
cout << endl;

cout << "\nData on Scientist  :  ";
s1.putdata();
cout << endl;

cout << "\nData on foreman  :  ";
f1.putdata();
cout << endl;
getch();
}





OUTPUT

Enter data for Manager  : 
   Enter name :  abc
   Enter number :  30
   Enter title :  Asst
   Enter golf club dues :  50000


Enter data for Scientist  : 
   Enter name :  xzw
   Enter number :  25
   Enter number of pubs :  12


Enter data for foreman  : 
 Enter quotas :  56

----------------------------------------
Data on Manager  : 
   Name :  abc
   Number :  30
   Title :  Asst
   Golf club dues :  50000

Data on Scientist  : 
   Name :  xzw
   Number :  25
   Number of publications :  12

Data on foreman  : 
   Quotas :  56




HYBRID WITH USE OF VIRTUAL


#include<iostream.h>
#include<conio.h>
#include<string.h>
class publication
{
  private:
char title[15];
float price;
 public:
void getdata()
{
cout<<"\nEnter title:";
cin>>title;
cout<<"\nEnter price:";
cin>>price;
}
void putdata()
{
cout<<"\n\nTitle:"<<title;
cout<<"\n\nPrice:"<<price;
}
};
class book : public publication
{
private:
int pages;
public:
void getdata()
{
publication::getdata();
cout<<"\nEnter number of pages:";
cin>>pages;
}
void putdata()
{
publication::putdata();
cout<<"\n\npages:"<<pages;
}
};
class sales
{
private:
enum{months=3};
float salesarr[months];
public:
void getdata();
void putdata();
};
void sales::getdata()
{
cout<<"\nEnter sales for 3 months\n";
for(int j=0;j<months;j++)
{
cout<<"     Month"<<j+1<<":";
cin>>salesarr[j];
 }
}
void sales::putdata()
{
for(int j=0;j<months;j++)
{
cout<<"\n\nSales for Month"<<j+1<<":";
cout<<salesarr[j];
 }
}
class tape:public book,public sales
{
private:
char author[50];
public:
void getdata()
{
    book::getdata();
    cout<<"\nEnter author name:";
    cin>>author;
    sales::getdata();
}
void putdata()
{
book::putdata();
cout<<"\n\n Author name:"<<author;
sales::putdata();
}
};
void main()
{
clrscr();
tape t;
t.getdata();
cout<<"\n\nDetails entered are : \n";
t.putdata();
cout<<endl;
getch();
}

OUTPUT

Enter title:MasteringC++

Enter price:250

Enter number of pages:300

Enter author name:Venugopal

Enter sales for 3 months
     Month1:30
     Month2:60
     Month3:80

Details entered are : 

Title:MasteringC++

Price:250

pages:300

 Author name:Venugopal

Sales for Month1:30
Sales for Month2:60
Sales for Month3:80


USE OF CONSTRUCTOR IN INHERITANCE



#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
class item
{
 private:
   char title[20];
   float price;
 public:
   item()
   {
    strnset(title,0,20);
    price=0;
   }
   item(char *t,float p)
   {
    strcpy(title,t);
    price=p;
   }
   void getdata()
   {
    cout<<"\nEnter title and price";
    cin>>title>>price;
   }
   void displaydata()
   {
    cout<<endl<<"\n\nTitle and price: ";
    cout<<title<<"\t"<<price;
   }
};
 class sales
 {
  private:
    float salesfig[3];
  public:
   sales()
   {
    for(int i=0;i<3;i++)
     salesfig[i]=0;
   }
  sales(float a,float b,float c)
  {
   salesfig[0]=a;
   salesfig[1]=b;
   salesfig[2]=c;
  }
  void getdata()
  {
   cout<<"\nEnter sales figures for 3 months: ";
   for(int i=0;i<3;i++)
     cin>>salesfig[i];
  }
  void displaydata()
  {
   cout<<endl<<"Sales figures for 3 months: ";
   for(int i=0;<3;i++)
     cout<<salesfig[i]<<"\t";
  } };
 class hwitem:private item,private sales
 {
  private:
   char category[10];
   char oem[10];
  public:
   hwitem():item(),sales()
   {
    strnset(category,0,10);
    strnset(oem,0,10);
   }
   hwitem(float a,float b,float c,char *t,float p,char *cat,char *o):item(t,p),sales(a,b,c)
    {
    strcpy(category,cat);
    strcpy(oem,o);
   }
   void getdata()
   {
    item::getdata();
    cout<<endl<<"Enter category and oem:";
    cin>>category>>oem;
    sales::getdata();
    }
   void displaydata()
   {
    item::displaydata();
    cout<<endl<<"Category and oem: ";
    cout<<category<<"\t"<<oem;
    sales::displaydata();
    }
 };
 void main()
 {
  hwitem h1;
  clrscr();
  h1.getdata();
  h1.displaydata(); 
  getch();
 }



OUTPUT

Enter title and price : IBM 50000                                                       
                                                                            
Enter category and oem:IBM PC/AT                                                         
                                                                              
Enter sales figures for 3 months: 125000 17000 25000                                          
                                                                              

Title and price: IBM 50000                                                       
Category and oem: IBM PC/AT                                                      
Sales figures for 3 months: 125000 17000 25000                                    

*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***