parameter constructor

output is not corrector
its something like this
Name:___________Azzz
Natinality:_______Azzz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include<iostream.h>
class Citizen{
	private:
		char *name;
		char *nationality;
	public:
		// no parameter
		Citizen()
		{
			name=NULL;
			nationality=NULL;
		}
		// two parameter
		Citizen(char* nam, char* nation)
		{
			nam=new char [30];
			nation=new char [30];
			name=nam;
			nationality=nation;
		}

		void obj2display()
		{
			cout<<"2nd Object With Two Parameter"<<endl;
			cout<<"Name:"<<name<<endl;
			cout<<"Nationality:"<<nationality<<endl;
		}
};

void main ()
{
	Citizen c1("Sohaib","Pakistani");
	c1.obj2display();
}

Line 16 should point to your name pointer, and line 17 should point to your nationality pointer.

1
2
name =new char [30];
nationality =new char [30];


Also remember you need to free the memory you allocated in the destructor.
1
2
3
delete [] name;
delete [] nationality;



Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream> ///
using std::cout; ///
using std::endl; ///
class Citizen{
	private:
		char *name;
		char *nationality;
	public:
		// no parameter
		Citizen()
		{
			name=NULL;
			nationality=NULL;
		}
		// two parameter
		Citizen(char* nam, char* nation)
		{
			/// nam=new char [30];
			/// nation=new char [30];
			name=nam;
			nationality=nation;
		}

		void obj2display()
		{
			cout<<"2nd Object With Two Parameter"<<endl;
			cout<<"Name:"<<name<<endl;
			cout<<"Nationality:"<<nationality<<endl;
		}
};

void main ()
{
	Citizen c1("Anup","Bangladeshi");
	c1.obj2display();
}
void main () runs in visual studio, but not in gcc compiler.
so, use int main ()
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include<iostream.h>
class Citizen{
	private:
		char *name;
		char *nationality;
	public:
		// no parameter
		Citizen()
		{
			name=NULL;
			nationality=NULL;
		}
		// two parameter
		Citizen(char* nam, char* nation)
		{
			name=new char [30];
			nationality=new char [30];
			name=nam;
			nationality=nation;
		}

		void obj2display()
		{
			cout<<"2nd Object With Two Parameter"<<endl;
			cout<<"Name:"<<name<<endl;
			cout<<"Nationality:"<<nationality<<endl;
		}

		~Citizen()
		{
			cout<<"Destructor is Called"<<endl;
		}
};

void main ()
{
	Citizen c1("Alex","USA");
	c1.obj2display();

	Citizen c2=c1;
}
Topic archived. No new replies allowed.