0% found this document useful (0 votes)
142 views8 pages

Inhert

This document contains code examples demonstrating different types of inheritance in C++, including single inheritance, multilevel inheritance, and multiple inheritance. In the single inheritance example, an Employee Payroll program is created with a base Employee class and derived Salary class to calculate net pay. The multilevel inheritance example creates a Student Marksheet program with a base Student class, derived Marks class, and further derived Result class to calculate percentages. The multiple inheritance example creates a Student Marks program with base Student and Sports classes, and a derived Statement class to calculate total and average marks from both base classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views8 pages

Inhert

This document contains code examples demonstrating different types of inheritance in C++, including single inheritance, multilevel inheritance, and multiple inheritance. In the single inheritance example, an Employee Payroll program is created with a base Employee class and derived Salary class to calculate net pay. The multilevel inheritance example creates a Student Marksheet program with a base Student class, derived Marks class, and further derived Result class to calculate percentages. The multiple inheritance example creates a Student Marks program with base Student and Sports classes, and a derived Statement class to calculate total and average marks from both base classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

WWW.VIDYARTHIPLUS.

COM

SINGLE INHERITANCE
Ex. No : 1.d
Aim:
To write a C++ program using single inheritance, Prepare a Employee Payroll and
calculate the Net Pay for multiple employees.

Algorithm:

STEP 1: Start the program

STEP 2: Declare a base class emp and get the employee details

STEP 3: Create a derived class salary to get the salary details and also to
calculate the net pay.
STEP 4: Access the member functions of class emp and salary
STEP 5: Get the values from the class members
STEP 6: Calculate the net pay
STEP 7: Generate the Employee Payroll in the required format
STEP 8: Terminate the program.

Program:
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

void get()
{
cout<<"\n Enter the Employee NO :
cin>>eno;
cout<<"\n Enter the Employee NAME: :";
cin>>name;
cout<<"\n Enter the Employee DESIG: ";
cin>>des;
}

";

};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"\n Enter the BASIC PAY:
";
cin>>bp;
cout<<"\n Enter HRA:
";
cin>>hra;
cout<<"\n Enter DA:
";
cin>>da;
cout<<"\n Enter PF:
";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t
"<<np<<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"\n Enter the No of Employees:
";
cin>>n;
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}

multilevel inheritances
Ex. No :1.e
Aim:
To write a c++ program Using multilevel inheritances, Prepare a Student Mark sheet,
class marks for every student in three subjects and display the results. The inherited class
generates mark sheet.

Algorithm:

STEP 1: Start the program

STEP 2: Declare a base class student

STEP 3: Derive a class marks from the class student,result from marks
STEP 4: use getdata() and putdata() function for student to get information and
input() , output() for getting marks calculate() function in result to calculate percentage
STEP 5: Access the member functions of class student and marks
Get the values from the class members
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

STEP 6: Calculate the student marks


STEP 7: Generate the mark sheet and display the result.
STEP 8: Terminate the program.

EX.NO :1.e

MULTILEVEL

#include<iostream.h>
#include<conio.h>
class student
{
public:
int roll;
char name[25];
char add [25];
char city[20];
void getdata()
{
cout<<"\n enter the student roll no.";
cin>>roll;
cout<<"\n enter the student name";
cin>>name;
cout<<"\n enter ther student address";
cin>>add;
cout<<"\n enter the student city";
cin>>city;
}
void putdata()
{
cout<<"\n the student roll no:"<<roll;
cout<<"\n the student name:"<<name;
cout<<"\n the student coty:"<<city;
}
};
class marks: public student
{
public:
int sub1;
int sub2;
int sub3;
void input()
{
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

getdata();
cout<<"\n enter the marks1:";
cin>>sub1;
cout<<"\n enter the marks2:";
cin>>sub2;
cout<<"\n enter the marks3:";
cin>>sub3;
}
void output()
{
putdata();
cout<<"\n marks1:"<<sub1;
cout<<"\n marks2:"<<sub2;
cout<<"\n marks3:"<<sub3;
}
};
class result:public marks
{
public:
int per;
void calculate ()
{
per= (sub1+sub2+sub3)/3;
cout<<"\n percentage"<<per;
}
};
void main()
{
result m1;
int ch;
clrscr();
do
{
cout<<"\n1.input data";
cout<<"\n2.output data";
cout<<"\n3. Calculate percentage";
cout<<"\n4.exit";
cout<<"\n enter the choice";
cin>>ch;
getch();
switch (ch)
{
case 1:
m1.input();
break;
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

case 2:
m1.output();
break;
case 3:
m1.calculate();
break;
}
} while (ch!=4);}

MULTIPLE INHERITANCE
Ex. No : 1.f
Aim:
To write a C++ program using multiple inheritance, Prepare a Student Marks System and
Calculate the Total and Average Marks.

Algorithm:

STEP 1: Start the program

STEP 2: Declare a base class student and get the student name and 2
subject marks

STEP 3: Create another class sports and get the sports marks of that
student
STEP 4: Create a Derived Class statement that will inherit from student
and sports classes
STEP 5: Access the member functions of the classes student and sports
using the object of the derived class statement.
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

STEP 6: Calculate the total and average


STEP 7: Display the result in the required format
STEP 8: Terminate the program.

EX. NO:1.f

MUTIPLE INHERITANCE

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"\nEnter the RollNo:
";
cin>>rno;
cout<<"\n Enter 2 Marks:
";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"\n Enter the Sports Marks: ";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
WWW.VIDYARTHIPLUS.COM

V+ TEAM

WWW.VIDYARTHIPLUS.COM

avg=tot/3;
cout<<"\n\n RollNo: "<<rno<<"\n\n Total:
cout<<"\n\t Average:
"<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}

WWW.VIDYARTHIPLUS.COM

"<<tot;

V+ TEAM

You might also like