OOP 2nd SOL .............. 2nd sol .................. #include ............... Write a C++ program in which you are required to define a class named Citizen. The class must include the following two data members.
#include
#include
#include
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<<"\n"<
#include
using namespace std;
class Citizen{
private:
string name;
string nationality;
public:
Citizen(string n,string nat)
{name=n;
nationality=nat;
}
Citizen()
{name="name";
nationality="nation";
}
Citizen(const Citizen &n)
{name=n.name;
nationality=n.nationality;
}
void display()
{cout<
using namespace std;
//class definition
class citizen
{
//hidden part
private:
string name; //for citizen's name
string nationality; //for nationality
// visiable interface
public:
//paramterless constructor
citizen()
{
name = "Farhan" ;
nationality = "pakistani";
}
//may be written as
/*
citizen()
{
name="name";
nationality="nation";
}
*/
//parameterized constructor
citizen(string nam,string nationty)
{
name=nam;
nationality=nationty;
}
//copy constructor
citizen(const citizen &obj)
{
name=obj.name;
nationality=obj.nationality;
}
//to displaying name and nationality
void display()
{
cout<
cout<
cout<
cout<<" \n";
}
~citizen()
{
cout<<"\ndestructor called\n";
}
};
int main()
{
citizen c1;
citizen c2("Mark", "Australian");
citizen c3(c2);
// the code can also be written as if coomented constructor is used
/*citizen c1("Farhan","Pakistan"),c2("Mark","Austrailia"),c3(b);
c1.display();
c2.display();
c3.display();*/
system("pause");
return 0;
getch();
}
// data member for Citizen Name
1: Name
//data member for Citizen Nationality
2: Nationality
Your Program should define three constructors for the class Citizen
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 their respective objects. Incase of copy constructor, you are required to assign a separate space for the data members of the new object while copying the values of previously existed object.
Declare three objects (1 for each type of constructor) in main.
Write a function in class Citizen to display the initialized data members for each object.
Also write destructor for the class Citizen. Display a message that says “destructor called” in the destructor body.
Note:- Make use of comments in source code where you use constructors , objects, copy constructors and destructors.
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 no parameters.
Mark
Australian
are the initialized values against the constructor with two parameters.
Mark
Australian
are the initialized values against the copy constructor that copies the values of already existed object.
Friday, May 7, 2010
cs304 solution
Labels:
CS and IT
0 comments:
Post a Comment