4th sem OOPC Write the following programs using INHERITANCE in CPP language.
1. To enter and display the student data using single inheritance technique.
#include<iostream>
#include<conio.h>
using namespace std;
class student{
public:
int roll_no;
char name[20];
void get(){
cout<<"\nEnter Student name: ";
cin>>name;
cout<<"Enter Student Roll No: ";
cin>>roll_no;
}
};
class subject:public student{
public:
char sub1[10],sub2[10],sub3[10];
void getsub(){
cout<<"Enter first subject: ";
cin>>sub1;
cout<<"Enter Second subject: ";
cin>>sub2;
cout<<"Enter Third subject: ";
cin>>sub3;
}
};
class school:public subject{
public:
void display(){
cout<<"\nRoll No: "<<roll_no<<"\nName: "<<name<<"\nSubject 1:"<<sub1<<"\nSubject 2:"<<sub2<<"\nSubject 3:"<<sub3<<endl;
}
};
int main(){
int i,n;
school s[10];
cout<<"\nEnter the number of student want to enter :";
cin>>n;
for(i=0;i<n;i++){
s[i].get();
s[i].getsub();
}
cout<<"\n\nStudent Details:-"<<endl;
for(i=0;i<n;i++){
s[i].display();
}
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
class student{
public:
int roll_no;
char name[20];
void get(){
cout<<"\nEnter Student name: ";
cin>>name;
cout<<"Enter Student Roll No: ";
cin>>roll_no;
}
};
class subject:public student{
public:
char sub1[10],sub2[10],sub3[10];
void getsub(){
cout<<"Enter first subject: ";
cin>>sub1;
cout<<"Enter Second subject: ";
cin>>sub2;
cout<<"Enter Third subject: ";
cin>>sub3;
}
};
class school:public subject{
public:
void display(){
cout<<"\nRoll No: "<<roll_no<<"\nName: "<<name<<"\nSubject 1:"<<sub1<<"\nSubject 2:"<<sub2<<"\nSubject 3:"<<sub3<<endl;
}
};
int main(){
int i,n;
school s[10];
cout<<"\nEnter the number of student want to enter :";
cin>>n;
for(i=0;i<n;i++){
s[i].get();
s[i].getsub();
}
cout<<"\n\nStudent Details:-"<<endl;
for(i=0;i<n;i++){
s[i].display();
}
return 0;
}
2. Write a program to enter the information of an employee and then display it using the single inheritance concept (one base class and one derived class).
#include<iostream>
#include<conio.h>
using namespace std;
class student{
public:
int roll_no;
char name[20];
void get(){
cout<<"\nEnter Student name: ";
cin>>name;
cout<<"Enter Student Roll No: ";
cin>>roll_no;
}
};
class subject:public student{
public:
char sub1[10],sub2[10],sub3[10];
void getsub(){
cout<<"Enter first subject: ";
cin>>sub1;
cout<<"Enter Second subject: ";
cin>>sub2;
cout<<"Enter Third subject: ";
cin>>sub3;
}
void display(){
cout<<"\nRoll No: "<<roll_no<<"\nName: "<<name<<"\nSubject 1:"<<sub1<<"\nSubject 2:"<<sub2<<"\nSubject 3:"<<sub3<<endl;
}
};
int main(){
int i,n;
subject s[10];
cout<<"\nEnter the number of student want to enter :";
cin>>n;
for(i=0;i<n;i++){
s[i].get();
s[i].getsub();
}
cout<<"\n\nStudent Details:-"<<endl;
for(i=0;i<n;i++){
s[i].display();
}
return 0;
}
3. Write a program to enter the information of n number of student and then display it using the multilevel inheritance concept.
#include<iostream>
#include<conio.h> using namespace std; class student_year{
public:
int en_da;
int ex_da;
void getyear(){
cout<<"Enter data of Enter school:";
cin>>en_da;
cout<<"Enter the date the person leave school:";
cin>>ex_da;
} }; class student_sub{
public:
char sub1[50],sub2[50],sub3[50];
void getsub(){
cout<<"Enter first subject: ";
cin>>sub1;
cout<<"Enter Second subject: ";
cin>>sub2;
cout<<"Enter Third subject: ";
cin>>sub3;
}
};
class student_info : public student_year, public student_sub{ public:
int roll_no;
char name[50];
void getinfo(){
cout<<"\nEnter Student name: ";
cin>>name;
cout<<"Enter Student Roll No: ";
cin>>roll_no;
}
void display(){
cout<<"\nRoll No: "<<roll_no<<"\nName: "<<name<<"\nSubject 1:"<<sub1<<"\nSubject 2:"<<sub2<<"\nSubject 3:"<<sub3<<"Admision Date:"<<en_da<<"Leaving Date"<<ex_da<<endl;
} }; int main(){
int i,n; cout<<"\nEnter the number of student want to enter :";
cin>>n;
student_info s[n];
for(i=0;i<n;i++){
s[i].getinfo();
s[i].getsub();
s[i].getyear();
}
cout<<"\n\nStudent Details:-"<<endl;
for(i=0;i<n;i++){
s[i].display();
cout<<endl;
}
return 0;
}
#include<conio.h> using namespace std; class student_year{
public:
int en_da;
int ex_da;
void getyear(){
cout<<"Enter data of Enter school:";
cin>>en_da;
cout<<"Enter the date the person leave school:";
cin>>ex_da;
} }; class student_sub{
public:
char sub1[50],sub2[50],sub3[50];
void getsub(){
cout<<"Enter first subject: ";
cin>>sub1;
cout<<"Enter Second subject: ";
cin>>sub2;
cout<<"Enter Third subject: ";
cin>>sub3;
}
};
class student_info : public student_year, public student_sub{ public:
int roll_no;
char name[50];
void getinfo(){
cout<<"\nEnter Student name: ";
cin>>name;
cout<<"Enter Student Roll No: ";
cin>>roll_no;
}
void display(){
cout<<"\nRoll No: "<<roll_no<<"\nName: "<<name<<"\nSubject 1:"<<sub1<<"\nSubject 2:"<<sub2<<"\nSubject 3:"<<sub3<<"Admision Date:"<<en_da<<"Leaving Date"<<ex_da<<endl;
} }; int main(){
int i,n; cout<<"\nEnter the number of student want to enter :";
cin>>n;
student_info s[n];
for(i=0;i<n;i++){
s[i].getinfo();
s[i].getsub();
s[i].getyear();
}
cout<<"\n\nStudent Details:-"<<endl;
for(i=0;i<n;i++){
s[i].display();
cout<<endl;
}
return 0;
}
Comments
Post a Comment