You have to write a C++ Program in which you will Write aclass ‘ person’ which h
ID: 3619356 • Letter: Y
Question
You have to write a C++ Program in which you will Write aclass ‘person’ which hasthree types of data members name, age and CNIC No. and getName(),getAge() and gerCNIC() functions that returns the name, ageand CNIC No of the person respectively.
Write a second class ‘adult’ which inheritedthe person class and has “implemented in term of”relationship to person. The ‘adult’ classrestrict the functionality of getAge() function by overriding the gatAge() function of person class. You also have to check thecondition if age is greater than 18 then print “AdultPerson”.
If object of adult class is initialized with the age less than18, then it prints a message “age is less than18” and sets the value of age to default.
Explanation / Answer
#include<iostream> #include<conio> #include<string> using namespace std; class person { char name[50]; int age; char CNIC_No[7]; public: person(char name1[50]="no name",intage1=18,char CNIC[7]="000-000") //default constrctor { strcpy(name,name1); if(age1>=18) age=age1; else { cout<<" Age is Less Than 18 "; age=18; } strcpy(CNIC_No,CNIC); } char* Getname() { return name; } int Getage() { return age; } char* GetCNIC() { return CNIC_No; } void show() { if(age>=18) cout<<" Adult Person"; cout<<" Name :"<<name; cout<<" Age :"<<age; cout<<" CNIC No:"<<CNIC_No; } }; class adult:public person { public: adult(char name[],int age,charCNIC[]):person(name,age,CNIC) {} int Getage() { person::Getage(); } }; void main() { adult test("mwm_104",20,"123-456"); test.show(); adult test2("SAMS",16,"325-568"); cout<<" Checking in main "; int check=test2.Getage(); if(check>=18) cout<<" Adult Person "; cout<<" Name :"<<test2.Getname(); cout<<" Age :"<<test2.Getage(); cout<<" CNIN No:"<<test2.GetCNIC(); getche(); } #include<iostream> #include<conio> #include<string> using namespace std; class person { char name[50]; int age; char CNIC_No[7]; public: person(char name1[50]="no name",intage1=18,char CNIC[7]="000-000") //default constrctor { strcpy(name,name1); if(age1>=18) age=age1; else { cout<<" Age is Less Than 18 "; age=18; } strcpy(CNIC_No,CNIC); } char* Getname() { return name; } int Getage() { return age; } char* GetCNIC() { return CNIC_No; } void show() { if(age>=18) cout<<" Adult Person"; cout<<" Name :"<<name; cout<<" Age :"<<age; cout<<" CNIC No:"<<CNIC_No; } }; class adult:public person { public: adult(char name[],int age,charCNIC[]):person(name,age,CNIC) {} int Getage() { person::Getage(); } }; void main() { adult test("mwm_104",20,"123-456"); test.show(); adult test2("SAMS",16,"325-568"); cout<<" Checking in main "; int check=test2.Getage(); if(check>=18) cout<<" Adult Person "; cout<<" Name :"<<test2.Getname(); cout<<" Age :"<<test2.Getage(); cout<<" CNIN No:"<<test2.GetCNIC(); getche(); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.