VUsolutions Transferred to AchiKhasi.com

From December 2011, this blog www.VUsolutions.blogspot.com is transferred to http://achikhasi.com/vu/ . So, you may visit http://achikhasi.com/vu/ for latest study related help.

Back to home VUsolutions

VUsolutions Fans Club [join us for MORE solutions]

VUsolutions on Facebook

cs304 solution

Friday, May 07, 2010 Posted In Edit This

OOP 2nd SOL

#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"<

..............

2nd sol

#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<

..................

#include
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();
}

...............

Write a C++ program in which you are required to define a class named Citizen. The class must include the following two data members.


// 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.

Back to home VUsolutions

Shaadi.com: Just create ur account & find ur partner or EARN money, its reall & EASY

VUsolutions Followers (Join NOW and Get Extra Benefits)

Install LATEST toolbar having lot of features - GET solutions on Desktop

toolbar powered by Conduit
Caliplus 300x250 NoFlam VitoLiv 468x60 GlucoLo