In: Computer Science
Taking a test on "Inheritance" in C++ tomorrow. So far we have learned about classes, structs and arrays and strings. We will be provided with coding prompts and have to hand write codes that show examples of inheritance (most likely including some of the earlier concepts as well).
I would like some code examples such as this to study and look over with comments please.
PROGRAM 1 (Includes class and strings):
#include<iostream>
using namespace std;
class Student //holds the common details relevant to students of
all streams
{
string name;
int roll, age;
public:
void input() //enters student
details
{
cout<<"Enter name of the student : ";
cin>>name;
fflush(stdin);
cout<<"Enter roll of the student : ";
cin>>roll;
cout<<"Enter age of the student : ";
cin>>age;
}
void display() //displays student details
{
cout<<"\nRoll :
"<<roll;
cout<<"\nName : "<<name;
cout<<"\nAge : "<<age;
}
};
class Engg : public Student //inherits Student class and
contains data specific to engineering students
{
string dept;
public:
void input()
{
Student::input(); //calling the function with same name of the base
class
cout<<"Enter department (civil/mechanical/cse/electrical):
";
cin>>dept;
}
void display()
{
Student::display();
cout<<"\nDepartment : "<<dept;
}
};
class Science : public Student //inherits Student class and
contains data specific to pure science students
{
string dept;
public:
void input()
{
Student::input();
cout<<"Enter department
(cmsa/math/physics/chemistry): ";
cin>>dept;
}
void display()
{
Student::display();
cout<<"\nDepartment : "<<dept;
}
};
class Medical : public Student //inherits Student class and
contains data specific to medical students
{
string dept;
public:
void input()
{
Student::input();
cout<<"Enter specialization
(heart/skin/eyes/nerve): ";
cin>>dept;
}
void display()
{
Student::display();
cout<<"\nSpecialization : "<<dept;
}
};
int main()
{
Science obj1;
Engg obj2;
Medical obj3;
cout<<"\nEnter details of science
student:\n";
obj1.input();
cout<<"\nEnter details of engineering
student:\n";
obj2.input();
cout<<"\nEnter details of medical
student:\n";
obj3.input();
//Illustrating virtual polymorphism
Student *obj;
cout<<"\nDetails of science student:";
obj=&obj1;
obj->display();
cout<<"\n";
cout<<"\nDetails of engineering student:";
obj=&obj2;
obj->display();
cout<<"\n";
cout<<"\nDetails of medical student:";
obj=&obj3;
obj->display();
cout<<"\n";
}
PROGRAM 2
(Includes class, arrays, linked list and
pointers):
#include<iostream>
using namespace std;
class Person //employee details program to implement
inheritance
{
protected:
char name[50];
int code;
public:
int cod;
void inperson()
{
cout<<"Enter name of person: ";
fflush(stdin);
gets(name);
cout<<"Enter code: ";
cin>>code;
cod=code;
}
void outperson()
{
cout<<"Name: ";
puts(name);
cout<<"Code: "<<code<<"\n";
}
};
class Account:virtual public Person
{
protected:
float pay;
public:
void inacc()
{
cout<<"Enter pay: ";
cin>>pay;
}
void outacc()
{
cout<<"Pay: "<<pay<<"\n";
}
};
class Admin:virtual public Person
{
protected:
int exp;
public:
void inadmin()
{
cout<<"Enter years of experience: ";
cin>>exp;
}
void outadmin()
{
cout<<"Experience: "<<exp<<"\n";
}
};
class Master:public Account,public Admin
{
Master *next;
public:
Master()
{
next=NULL;
}
void nextobj(Master *pt)
{
next=pt;
}
Master* retnext()
{
return
next;
}
};
int main() //creates a linked list of Master objects pointed to
by 'start'
{
Master *obj,*ptr,*start;
start=NULL;
int ch,c,k=1,up;
while(k)
{
cout<<"\n1:Input\n2:Update\n3:Display\nEnter choice: ";
cin>>ch;
switch(ch)
{
case 1:
obj=new Master;
obj->inperson();
obj->inacc();
obj->inadmin();
if(start==NULL)
start=obj;
else
{
ptr=start;
while(ptr->retnext()!=NULL)
{
ptr=ptr->retnext();
}
ptr->nextobj(obj);
}
break;
case 2:
cout<<"\nEnter Code: ";
cin>>c;
ptr=start;
while((ptr!=NULL) &&
(ptr->cod!=c))
{
ptr=ptr->retnext();
}
if(ptr==NULL)
cout<<"Person does not exist\n";
else
{
cout<<"Update:\n1.Name
& Code\n2.Pay\n3.Experience\nEnter your choice: ";
cin>>up;
if(up==1)
ptr->inperson();
else if(up==2)
ptr->inacc();
else if(up==3)
ptr->inadmin();
else
cout<<"Wrong
input\n";
}
break;
case 3:
cout<<"\nEnter Code: ";
cin>>c;
ptr=start;
while(ptr->cod!=c && ptr!=NULL)
{
ptr=ptr->retnext();
}
if(ptr==NULL)
cout<<"Person does not exist\n";
else
{
ptr->outperson();
ptr->outacc();
ptr->outadmin();
}
break;
default:
cout<<"Wrong input\n";
break;
}
cout<<"Press 1 to continue:
";
cin>>k;
}
}