You have to write a C++ Program in which you will Write aclass ‘ person’ which h
ID: 3619358 • Letter: Y
Question
You have to write a C++ Program in which you will Write aclass ‘person’ which has three typesof data members name, age and CNIC No. and getName(), getAge() andgerCNIC() functions that returns the name, age and CNIC No ofthe person respectively.
Write a second class ‘adult’which inherited the person class and has “implementedin term of” relationship to person. The‘adult’ class restrict the functionality of getAge()function by overriding the gatAge() function of person class.You also have to check the condition if age is greater than 18 thenprint “Adult Person”.
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(); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.