Code for Program that provides an example of inheritance using private data in C++ Programming
#include<iostream.h>
#include<conio.h>
class stud
{
int rno;
char name[20];
public:
void getdata()
{
cout<<"Enter Your Roll Number ";
cin>>rno;
cout<<"Enter Your Name ";
cin>>name;
}
void putdata()
{
cout<<"Roll Number is : "<<rno<<endl;
cout<<"Name is : "<<name<<endl;
}
};
class science : public stud
{
int maths,sci;
public:
void getdata()
{
stud::getdata();
cout<<"Enter Maths Marks ";
cin>>maths;
cout<<"Enter Science Marks ";
cin>>sci;
}
void putdata()
{
stud::putdata();
cout<<"Maths Marks are : "<<maths<<endl;
cout<<"Science Marks are : "<<sci<<endl;
}
};
class commerce :public stud
{
int acc,stat;
public:
void getdata()
{
stud::getdata();
cout<<"Enter Account Marks ";
cin>>acc;
cout<<"Enter Statistics Marks ";
cin>>stat;
}
void putdata()
{
stud::putdata();
cout<<"Account Marks are : "<<acc<<endl;
cout<<"Statistics Marks are : "<<stat<<endl;
}
};
main()
{
clrscr();
science s1;
s1.getdata();
s1.putdata();
commerce c1;
c1.getdata();
c1.putdata();
getch();
}