The concept of composition in OOP, Inheritance, Public & private inheritance, basic constructs of OOP in a program
A C++ Program in which you will learn to write a class ‘Pakistani Citizens’ which has five data members “name, age, CNIC No, province, and domicile” and two data functions getter and setter for each data member e.g setAge() getAge() etc.
A second class ‘VotersList’ which inherited the ‘PakistaniCitizens’ class and has “implemented in term of” relationship to pakistaniCitizens.
The ‘VotersList’ class restrict the functionality of setAge() function by override the setAge() function of ‘PakistaniCitizens’ class and check the condition :
If age is greater than 18 then print “Eligible for vote cast”.
If object of VotersList class is initialized with the age less than 18, then it prints a message
“Age is less than 18” and sets the value of age to default.
#include
#include
//Pakistani Citizens class
class Pakistani_Citizens{
//Attributes of pakistani citizens class
protected:
int age;
private:
char * name;
int CnicNo;
char * provience;
char * domicile;
//public interface of pakistani citizens class
public:
//Default constructor
Pakistani_Citizens();
// Parameterized constructor
Pakistani_Citizens(int, char *, int, char *,char *);
//Setter functions of paksitani citizens class
void setAge(int _age);
void setName(char * aname);
void setProvience(char * pro);
void setDomicile(char * dom);
//Getter functions of pakistani citizens class
int getAge();
char * getName();
char *getProvience();
char * getDomicile();
//Destructor of pakistani citizens class
~Pakistani_Citizens();
}; //End of pakistani citizens class
//Definition of Default constructor
Pakistani_Citizens::Pakistani_Citizens(){
age=0;
name=”ABC”;
CnicNo=1234;
provience=”abc”;
domicile=”xyz”;
}
//Definition of parameterized constructor
Pakistani_Citizens::Pakistani_Citizens(int _age, char* aname, int cno, char*prov, char*dom)
{
age=_age;
name=aname;
CnicNo=cno;
provience=prov;
domicile=dom;
}
//Definition of setAge method
void Pakistani_Citizens::setAge(int _age){
if(_age>0)
{
age=_age;
}
}
//Definition of setName method
void Pakistani_Citizens::setName(char * aname){
name=aname;
}
//Definition of setProvience method
void Pakistani_Citizens:: setProvience(char * pro){
provience=pro;
}
//Definition of setDomicile method
void Pakistani_Citizens::setDomicile(char * dom){
domicile=dom;
}
//Definition of getAge method
int Pakistani_Citizens:: getAge(){
return age;
}
//Definition of getName method
char * Pakistani_Citizens:: getName(){
return name;
}
//Definition of getProvience method
char * Pakistani_Citizens::getProvience(){
return provience;
}
// Definition of getDomicile method
char * Pakistani_Citizens::getDomicile(){
return domicile;
}
// Destructor of pakitani citizens class
Pakistani_Citizens::~Pakistani_Citizens(){
}
//VotorList class inherited from Pakistani_Citizens class
class VotorList:public Pakistani_Citizens{
//pbulic interface of class VotorList
public:
//Default constructor
VotorList();
// Declaration of setAge method
void setAge(int _age);
//Destructor of VotorList class
~VotorList();
}; //End of class VotorList
VotorList::VotorList(){
}
void VotorList:: setAge(int _age){
if (_age >=18 ) {
cout< _age=Pakistani_Citizens::age;
}
}
VotorList::~VotorList(){
}
a C++ Program in which you will have to write a class ‘Pakistani Citizens’ which has five data members “name, age, CNIC No, province, and domicile” and two data functions getter and setter for each data member e.g setAge() getAge() etc.
Write a second class ‘VotersList’ which inherited the ‘PakistaniCitizens’ class and has “implemented in term of” relationship to pakistaniCitizens.
The ‘VotersList’ class restrict the functionality of setAge() function by override the setAge() function of ‘PakistaniCitizens’ class and check the condition :
If age is greater than 18 then print “Eligible for vote cast”.
If object of VotersList class is initialized with the age less than 18, then it prints a message
“Age is less than 18” and sets the value of age to default.
Last updated: March 19, 2014