Vous êtes sur la page 1sur 6

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

01/05/2013

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

2511265

40

01/05/2013

p=&s; p->iam(); p=&d; p->iam(); return 0; };

OUTPUT:

2511265

41

01/05/2013

PROGRAM NO : 17 AIM: A HOSPITAL WANTS TO CREATE A DATABASE REGARDING ITS INDOOR PATIENTS. 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, MONTH AND DATE AS ITS MEMBERS). CREATE A BASE CLASS TO STORE THE ABOVE INFORMATION. THE MEMBER FUNCTION SHOULD INCLUDE FUNCTIONS TO ENTER INFORMATION AND DISPLAY A LIST OF ALL THE PATIENTS IN THE DATABASE. CREATE A DERIVED CLASS TO STORE THE AGE OF THE PATIENTS. LIST THE INFORMATION ABOUT ALL THE TO STORE THE AGE OF THE PATIENTS. OBJECTIVE: This program stores the information of indoor patients. A structure date is used
to stores the date of admission and discharge. A class hosp is made to store the rest of the information such as the name of the patient and the disease. The objects of the structure if created to be called from the class. Another class age is inherited from hosp class to store the age of the patient.

SOURCE CODE:
#include<iostream> using namespace std; struct hospital { int year; int month; int day; }; class hosp { protected: char name[40]; struct hospital doa; char disease[40]; struct hospital dod; public: void get(); void display(); };

2511265

42

01/05/2013

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

2511265

43

01/05/2013

} cout<<"\n\n\nDetails of "<<n<<" patients..."; for(i=1;i<=n;i++) { cout<<"\n\nPatient "<<i; a[i].display(); } return 0; }

OUTPUT:

2511265

44

Vous aimerez peut-être aussi