INHERITANCE PROGRAM EXAMPLE
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class student
{
protected:
int roll_no;
char name[20];
public:
void details()
{
cout<<"enter roll number and name of the student:\n";
cin>>roll_no>>name;
}
};
class subject: public student
{
protected:
int thm,prm;
public:
void tp()
{
cout<<"\ntheory marks: ";
cin>>thm;
cout<<"\npractical marks: ";
cin>>prm;
}
};
class sports
{
protected:
int spm;
public:
void sp()
{
cout<<"\nsports marks: ";
cin>>spm;
}
};
class result:public subject,public sports
{
protected:
int total;
public:
int t()
{
total=thm+prm+spm;
return total;
}
void res()
{
cout<<"\n\nName"<<" "<<"Roll No."<<" "<<"Theory"<<" "
<<"Practical"<<" "<<"Total"<<endl;
cout<<name<<" "<<roll_no<<" "<<thm<<" "
<<prm<<" "<<total;
}
};
void main()
{
result r;
r.details();
r.tp();
r.sp();
r.t();
r.res();
getch();
}
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class student
{
protected:
int roll_no;
char name[20];
public:
void details()
{
cout<<"enter roll number and name of the student:\n";
cin>>roll_no>>name;
}
};
class subject: public student
{
protected:
int thm,prm;
public:
void tp()
{
cout<<"\ntheory marks: ";
cin>>thm;
cout<<"\npractical marks: ";
cin>>prm;
}
};
class sports
{
protected:
int spm;
public:
void sp()
{
cout<<"\nsports marks: ";
cin>>spm;
}
};
class result:public subject,public sports
{
protected:
int total;
public:
int t()
{
total=thm+prm+spm;
return total;
}
void res()
{
cout<<"\n\nName"<<" "<<"Roll No."<<" "<<"Theory"<<" "
<<"Practical"<<" "<<"Total"<<endl;
cout<<name<<" "<<roll_no<<" "<<thm<<" "
<<prm<<" "<<total;
}
};
void main()
{
result r;
r.details();
r.tp();
r.sp();
r.t();
r.res();
getch();
}
Output
24 Namrata
theory marks: 89
practical marks: 45
sports marks: 23
Name Roll
No. Theory Practical
Total
Namrata 24 89 45 157