Inheritance Programs in C++
#include< iostream> using namespace std; class Student { public: string name; int roll; Student() { cout<<"Default Const of Student\n"; } }; class Marks : public Student { public : int m1,m2,m3; Marks(){ cout<<"Default Const of Marks\n"; } }; class Result : public Marks { public: int total; float per; Result() { cout<<"Const. of Result "<< endl; } void input(){ cout<<"Enter name of Student "; getline(cin,name); cout<<"Enter the roll no. "; cin>>roll; cout<<"Enter Marks of 3 subjects "; cin>>m1>>m2>>m3; cin.ignore(1,'\n'); } void display() { total = m1+m2+m3; per = (m1+m2+m3)/3.0; cout<< name<<"\t"<< roll<<"\t"; cout<< total<<"\t"<< per<< endl; } }; int main() { Result s[5]; for(int i = 0; i<5; i++) { s[i].input(); } for(int i = 0; i<5; i++) { s[i].display(); } return 0; }OUTPUT
Default Const of Student Default Const of Marks Const. of Result Default Const of Student Default Const of Marks Const. of Result Default Const of Student Default Const of Marks Const. of Result Default Const of Student Default Const of Marks Const. of Result Default Const of Student Default Const of Marks Const. of Result Enter name of Student Alok Enter the roll no. 12 Enter Marks of 3 subjects 90 90 90 Enter name of Student Deepak Enter the roll no. 23 Enter Marks of 3 subjects 85 85 85 Enter name of Student Ayan Enter the roll no. 34 Enter Marks of 3 subjects 99 99 99 Enter name of Student Dan Enter the roll no. 67 Enter Marks of 3 subjects 87 78 65 Enter name of Student Dan Enter the roll no. 56 Enter Marks of 3 subjects 50 50 50 Alok 12 270 90 Deepak 23 255 85 Ayan 34 297 99 Dan 67 230 76.6667 Dan 56 150 50
#include< iostream> using namespace std; class Student { public: string name; int roll; Student() { cout<<"Default Const of Student\n"; } }; class Marks : virtual public Student { public : int m1,m2,m3; Marks(){ cout<<"Default Const of Marks\n"; } }; class Sports:virtual public Student { public : int grade; Sports() { cout<<"Default Cons. of Sport\n"; } }; class Result : public Marks, public Sports { public: int total; float per; Result() { cout<<"Const. of Result\n"; } void input(){ cout<<"Enter name of Student "; getline(cin,name); cout<<"Enter the roll no. "; cin>>roll; cout<<"Enter Marks of 3 Subjects"; cin>>m1>>m2>>m3; cout<<"Enter the marks of Sports"; cin>>grade; cin.ignore(1,'\n'); } void display() { total = m1+m2+m3+grade; per = (m1+m2+m3+grade)/4.0; cout<< name<<"\t"<< roll<<"\t"; cout<< total<<"\t"<< per<< endl; } }; int main() { Result s[5]; for(int i = 0; i<5; i++) { s[i].input(); } for(int i = 0; i<5; i++) { s[i].display(); } return 0; }OUTPUT
Default Const of Student Default Const of Marks Default Cons. of Sport Const. of Result Default Const of Student Default Const of Marks Default Cons. of Sport Const. of Result Default Const of Student Default Const of Marks Default Cons. of Sport Const. of Result Default Const of Student Default Const of Marks Default Cons. of Sport Const. of Result Default Const of Student Default Const of Marks Default Cons. of Sport Const. of Result Enter name of Student Ashok Enter the roll no. 11 Enter Marks of 3 Subjects 78 79 80 Enter the marks of Sports75 Enter name of Student Alok Enter the roll no. 90 Enter Marks of 3 Subjects 90 90 90 Enter the marks of Sports95 Enter name of Student Ayan Enter the roll no. 23 Enter Marks of 3 Subjects 99 99 99 Enter the marks of Sports99 Enter name of Student Dan Enter the roll no. 34 Enter Marks of 3 Subjects 78 98 96 Enter the marks of Sports93 Enter name of Student Arif Enter the roll no. 45 Enter Marks of 3 Subjects 87 96 90 Enter the marks of Sports99 Ashok 11 312 78 Alok 90 365 91.25 Ayan 23 396 99 Dan 34 365 91.25 Arif 45 372 93
#include< iostream> using namespace std; class Shape { int ar; public: void areaCircle(int a) { ar = 3.14*a*a; cout<<"Area of Circle = "<< ar<< endl; } void areaRec(int a, int b) { ar = a*b; cout<<"Area of Rectangle "<< ar<< endl; } void areaTri(int a, int b) { ar = a*b/2; cout<<"Area of Triangle "<< ar<< endl; } }; class Rectangle :public Shape { public : int l,b; void input() { cout<<"Enter Length , Breadth : "; cin>>l>>b; areaRec(l,b); } }; class Circle : public Shape { public: int r; void input() { cout<<"Enter Radius : "; cin>>r; areaCircle(r); } }; class Triangle : public Shape { public : int b,h; void input() { cout<<"Enter Base,Height : "; cin>>b>>h; areaTri(b,h); } }; int main() { Rectangle r; r.input(); Triangle t; t.input(); Circle c; c.input(); return 0; }OUTPUT
Enter Length , Breadth : 4 5 Area of Rectangle 20 Enter Base,Height : 5 4 Area of Triangle 10 Enter Radius : 7 Area of Circle = 153
#include< iostream> using namespace std; class Shape { public: virtual void area() = 0; }; class Rectangle : public Shape { public: int l,b,ar; void area() { cout<<"Enter Length, Breadth "; cin>>l>>b; ar = l*b; cout<<"Area of Rectangle = "<< ar; cout<< endl; } }; class Triangle : public Shape { public: int h,b,ar; void area() { cout<<"Enter Height, Base "; cin>>h>>b; ar = h*b/2; cout<<"Area of Triangle = "<< ar; cout<< endl; } }; class Circle : public Shape { public: int r,ar; void area() { cout<<"Enter Radius "; cin>>r; ar = 3.14*r*r; cout<<"Area of Circle = "<< ar; cout<< endl; } }; int main() { Rectangle r; r.area(); Triangle t; t.area(); Circle c; c.area(); return 0; }OUTPUT
Enter Length, Breadth 4 3 Area of Rectangle = 12 Enter Height, Base 6 8 Area of Triangle = 24 Enter Radius 7 Area of Circle = 153
#include< iostream> using namespace std; class Media { public : string title; int price; virtual void input() = 0; virtual void display() = 0; }; class Book : public Media { public: int page; void input() { cout<<"Enter Title of Book: "; getline(cin,title); cout<<"Enter Price: "; cin>>price; cout<<"Enter No. of Page: "; cin>>page; cin.ignore(1,'\n'); } void display() { cout<<"Information of Book\n"; cout<<"Title - "<< title<< endl; cout<<"Price - "<< price<< endl; cout<<"Pages - "<< page<< endl; } }; class Tape:public Media { public : int r; void input() { cout<<"Enter Title of Tape: "; getline(cin,title); cout<<"Enter Price: "; cin>>price; cout<<"Enter Run Time in Min: "; cin>>r; cin.ignore(1,'\n'); } void display() { cout<<"Information of Tape\n"; cout<<"Title - "<< title<< endl; cout<<"Price - "<< price<< endl; cout<<"Run Time - "<< r<<" min\n"; } }; class CD : public Media { public : int cap; void input() { cout<<"Enter Title of CD: "; getline(cin,title); cout<<"Enter Price: "; cin>>price; cout<<"Enter Capacity in GB: "; cin>>cap; } void display() { cout<<"Information of CD\n"; cout<<"Title - "<< title<< endl; cout<<"Price - "<< price<< endl; cout<<"CAPACITY- "<< cap<<" GB\n"; } }; int main() { Media *m = new Book(); Media *m2 = new Tape(); Media * m3 = new CD; m->input(); m2->input(); m3->input(); m->display(); m2->display(); m3->display(); return 0; }OUTPUT
Enter Title of Book: Java Enter Price: 400 Enter No. of Page: 234 Enter Title of Tape: Song Enter Price: 30 Enter Run Time in Min: 120 Enter Title of CD: Panasonic Enter Price: 20 Enter Capacity in GB: 1 Information of Book Title - Java Price - 400 Pages - 234 Information of Tape Title - Song Price - 30 Run Time - 120 min Information of CD Title - Panasonic Price - 20 CAPACITY - 1 GB
#include< iostream> using namespace std; class Employee { public : string name,doj; int id; void input() { cout<<"Enter Emp. Id "; cin>>id; cin.ignore(1,'\n'); cout<<"Enter Name "; getline(cin,name); cout<<"Enter Date of Joining "; getline(cin,doj); } virtual void calc() = 0; virtual void disp() = 0; }; class Regular : public Employee { public : int bsal,gsal; static int HA, DRA; void calc() { cout<<"Enter Basic Salary "; cin>>bsal; gsal = bsal+HA+DRA; } void disp() { cout<<"Regular Time Employee\n"; cout<<"Emp id - "<< id<< endl; cout<<"Emp Name- "<< name<< endl; cout<<"Date of Joining "<< doj; cout<< endl; cout<<"Gross Salary "<< gsal; cout<< endl; } }; int Regular :: HA = 3000; int Regular :: DRA = 5000; class Part_Time : public Employee { public : int hrs,gsal; static int pay; void calc() { cout<<"Enter Hrs. "; cin>>hrs; gsal = hrs*pay; } void disp() { cout<<"Part Time Employee "; cout<< endl; cout<<"Emp id - "<< id; cout<< endl; cout<<"Emp Name- "<< name; cout<< endl; cout<<"Date of Joining "<< doj; cout<< endl; cout<<"Gross Salary "<< gsal; cout<< endl; } }; int Part_Time :: pay = 500; int main() { cout<<"Enter Regular Emp Info\n"; Regular r; r.input(); r.calc(); cout<<"Enter Part Time Emp Info\n"; Part_Time p; p.input(); p.calc(); r.disp(); p.disp(); return 0; }OUTPUT
Enter Regular Emp Info Enter Emp. Id 12 Enter Name Alok Enter Date of Joining 01-07-2015 Enter Basic Salary 25000 Enter Part Time Emp Info Enter Emp. Id 23 Enter Name Ashok Enter Date of Joining 01-10-2016 Enter Hrs. 30 Regular Time Employee Emp id - 12 Emp Name- Alok Date of Joining 01-07-2015 Gross Salary 33000 Part Time Employee Emp id - 23 Emp Name- Ashok Date of Joining 01-10-2016 Gross Salary 15000