01/05/2013

PROGRAM NO : 16 AIM: CONSIDER THE FOLLOWING CLASS DEFINITION CLASS FATHER { PROTECTED : INT AGE; PUBLIC; FATHER (INT X) {AGE = X;} VIRTUAL VOID IAM ( ) { COUT < < “I AM THE FATHER, MY AGE IS : ”<< AGE<< END1:} }; DERIVE THE TWO CLASSES SON AND DAUGHTER FROM THE ABOVE CLASS AND FOR EACH, DEFINE IAM ( ) TO WRITE OUR SIMILAR BUT APPROPRIATE MESSAGES. YOU SHOULD ALSO DEFINE SUITABLE CONSTRUCTORS FOR THESE CLASSES. NOW, WRITE A MAIN ( ) THAT CREATES OBJECTS OF THE THREE CLASSES AND THEN CALLS IAM ( ) FOR THEM. DECLARE POINTER TO FATHER. SUCCESSIVELY, ASSIGN ADDRESSES OF OBJECTS OF THE TWO DERIVED CLASSES TO THIS POINTER AND IN EACH CASE, CALL IAM ( ) THROUGH THE POINTER TO DEMONSTRATE POLYMORPHISM IN ACTION. OBJECTIVE: This program implements the concept of polymorphism by creating pointers to
the class objects. In this program three clases : father, son and daughter are created. The class ‘son’ and ‘daughter’ is publicly derived from the father class. Objects of these classes are created in the main() function. They are first called normally and then pointers to theses objects are created and then the iam() function of each class is called using these pointers.

SOURCE CODE:
#include<iostream> using namespace std; class father { protected: int age; public: father(int x=0) { age=x; }

2511265

39

my age is: "<<age. } virtual void iam() { cout<<"\nI AM THE SON. } }.. d. class son: public father { public: son(int y=0) { age=y. my age is: "<<age. class daughter: public father { public: daughter(int z=0) { age=z. daughter d(16). 2511265 40 .iam().iam(). f."<<endl. int main() { son s(20).my age is: "<<age. } }.01/05/2013 virtual void iam() { cout<<"\nI AM THE FATHER. father *p. s. p->iam().. } }. cout<<"\n\nCalling by pointers.iam(). p=&f. } virtual void iam() { cout<<"\nI AM THE DAUGHTER. father f(45).

p->iam().01/05/2013 p=&s. p->iam(). }. p=&d. return 0. OUTPUT: 2511265 41 .

CREATE A DERIVED CLASS TO STORE THE AGE OF THE PATIENTS. MONTH AND DATE AS ITS MEMBERS). THE INFORMATION TO STORE INCLUDE A) NAME OF THE PATIENT B) DATE OF ADMISSION C) DISEASE D) DATE OF DISCHARGE CREATE A STRUCTURE TO STORE THE DATE (YEAR. }. struct hospital { int year. char disease[40]. 2511265 42 . LIST THE INFORMATION ABOUT ALL THE TO STORE THE AGE OF THE PATIENTS. }. public: void get(). OBJECTIVE: This program stores the information of indoor patients. struct hospital dod. Another class age is inherited from hosp class to store the age of the patient. void display(). int month. A class ‘hosp’ is made to store the rest of the information such as the name of the patient and the disease. THE MEMBER FUNCTION SHOULD INCLUDE FUNCTIONS TO ENTER INFORMATION AND DISPLAY A LIST OF ALL THE PATIENTS IN THE DATABASE. SOURCE CODE: #include<iostream> using namespace std. struct hospital doa. A structure ‘date’ is used to stores the date of admission and discharge. The objects of the structure if created to be called from the class. CREATE A BASE CLASS TO STORE THE ABOVE INFORMATION.01/05/2013 PROGRAM NO : 17 AIM: A HOSPITAL WANTS TO CREATE A DATABASE REGARDING ITS INDOOR PATIENTS. class hosp { protected: char name[40]. int day.

cout<<"\nAge: "<<p_age. age a[50].year. int main() { int n. cin>>doa. cout<<"\nEnter the number of patients: ".day>>dod.". cin>>n. cout<<"\nDisease: "<<disease.month>>dod.i++) { cout<<"\n\nEnter the details of "<<i<<"th patient. cout<<"\nDate of discharge: "<<dod.year.year.month<<"/"<<dod.day<<"/"<<doa. cout<<"Enter the date of discharge: ".month>>doa. } }. cin>>dod. cout<<"Enter the disease: ". cout<<"\nDate of admission: "<<doa. cin>>disease.day>>doa. } void display() { hosp::display(). for(i=1. cout<<"Enter the date of admission: ". a[i].year.month<<"/"<<doa. 2511265 43 . cin>>name. cout<<"Enter the age of the patient: ".day<<"/"<<dod. public: void get() { hosp::get(). } class age:public hosp { private: int p_age.i. cin>>p_age.i<=n. } void hosp::display() { cout<<"\nName: "<<name.get().01/05/2013 void hosp::get() { cout<<"\nEnter the name of the patient: ".

} OUTPUT: 2511265 44 .01/05/2013 } cout<<"\n\n\nDetails of "<<n<<" patients. for(i=1..display(). } return 0.".i<=n. a[i].i++) { cout<<"\n\nPatient "<<i..

Sign up to vote on this title
UsefulNot useful