// data member for Citizen Name 1: Name //data member for Citizen Nationality 2:
ID: 3618552 • Letter: #
Question
// data member for Citizen Name
1: Name
//data member for Citizen Nationality
2: Nationality
Your Program should define three constructors for the classCitizen
1: a constructor with no parameter
2: a constructor with two parameters (name,nationality)
3: a copy constructor
All of these three constructors are meant to initialize theirrespective objects. Incase of copy constructor, you are required toassign a separate space for the data members of the new objectwhile copying the values of previously existed object.
Declare three objects (1 for each type ofconstructor) in main.
Write a function in class Citizen to displaythe initialized data members for each object.
Also write destructor for the classCitizen. Display a message that says“destructor called” in the destructorbody.
Note:- Make use of comments in source code where you useconstructors , objects, copy constructors anddestructors.
OUTPUT
Your output should be similar to the following
Farhan
Pakistani
_________________
Mark
Australian
_________________
Mark
Australian
_________________
Where;
Farhan
Pakistani
are the initialized against the constructor with noparameters.
Mark
Australian
are the initialized values against the constructor with twoparameters.
Mark
Australian
are the initialized values against the copy constructor thatcopies the values of already existed object.
Explanation / Answer
#include<iostream.h>#include<conio.h>
#include<string.h>
class citizen
{
char name[50];
char nationality[50];
public:
citizen()
{
strcpy(name,"Farhan");
strcpy(nationality,"Pakistani");
}
citizen(char name1[],char nationality1[])
{
strcpy(name,name1);
strcpy(nationality,nationality1);
}
citizen(const citizen& obj)
{
strcpy(name,obj.name);
strcpy(nationality,obj.nationality);
}
void display()
{
cout<<" "<<name;
cout<<" "<<nationality<<" ---------------";
}
~citizen()
{
cout<<" Destructor called ";
getche();
}
};
void main()
{
citizenobj1; //object created and No argument constructor called
citizen obj2("Mark","Australian"); //object created and Twoargument constructor called
citizenobj3=obj2; //object created and Copy constructor called
obj1.display();
obj2.display();
obj3.display();
getche();
} //Destructor called three times for three objectobj1,obj2,obj3
Hope you got the answer .please rate.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.