This is a sample C++ Program. using inheritance. Using Inheritance, this program accepts details of a student and print marks and percentage using inheritance.
#include <iostream.h>
#include <conio.h>
class person
{
private:
char name [20];
long int phone;
public:
void read ( )
{
cout << "Enter Name & Phone Number ";
cin >> name >> phone;
}
void show ( )
{
cout << "\n Name Is: " << name;
cout << "\n Phone Number Is: " << phone;
}
};
class student : public person
{
private:
int rollno;
char course [20];
public:
void read ( )
{
person :: read ( );
cout << "Enter The Roll Number & Course Name ";
cin >> rollno >> course;
}
void show ( )
{
person :: show ( );
cout << "\n Roll Number Is: " << rollno;
cout << "\n Course Is: " << course;
}
};
class exam : public student
{
private:
int m [4];
double per;
public:
void read ( );
void cal ( );
void show ( );
};
void exam :: read ( )
{
student :: read ( );
cout << "Enter Marks: ";
for (int i = 0; i < 4; i++)
cin >> m [i];
}
void exam :: cal ( )
{
int tot_marks = 0;
for (int i = 0; i < 4; i++)
tot_marks+= m [i];
per = double (tot_marks) / 4;
}
void exam :: show ( )
{
student :: show ( );
cout << "\n Marks: ";
for (int i = 0; i < 4; i++)
cout << m [i] << "\t";
cout <<"\n Percentage Is: " << per;
}
int main ( )
{
clrscr ( );
exam e1;
e1.read ( );
e1.cal ( );
e1.show ( );
getch ( );
return 0;
}
OUTPUT:
Enter Name & Phone Number
Naina
256552
Enter The Roll Number & Course Name
33
Law
Enter Marks:
99
90
95
94
Name Is: Naina
Phone Number is: 256552
Roll Number Is: 33
Course Is: Law
Marks : 99 90 95 94
Percentage = 94.5
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com