Where are constructors used ?!

I solved a problem that required this:

Create a class AVION, that has a default, copy and initialized values constructor.
I had to declare set/get functions too.
A function that calculaces the age of the plane, and a function that COUTs all the planes that take more than 400passengers & faster than 900km/h.

I tested it, and it's working fine, but what I don't get is, why did I need the constructors for? In the exercise it was asked to define all those constructors, but I don't see where is the use for them?

Here is the code:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>

using namespace std;

class avion
{
private:
	char marka[15];
	int patnici;
	int brzina;
	int god;

public:
	//Default constructor and a constructor with initialized values
	avion(char* mar=" ",int pat=0,int brz=0,int go=0)
	{
		strncpy(marka,mar,14);
		marka[14]='\0';
		patnici=pat;
		brzina=brz;
		god=go;
	}
	//Copy constructor
	avion(const avion &a)
	{
		strncpy(marka,a.marka,14);
		marka[14]=='\0';
		patnici=a.patnici;
		brzina=a.brzina;
		god=a.god;
	}

	//Get metodi:
	char* getMarka(){return marka;}
	int getPatnici(){return patnici;}
	int getBrzina(){return brzina;}
	int getGod(){return god;}

	//set metodi:
	void setMarka(char* m)
	{
		strncpy(marka,m,14);
		marka[14]='\0';
	}
	void setPatnici(int p){patnici=p;}
	void setBrzina(int b){brzina=b;}
	void setGod(int g){god=g;}

	//Funkcija koja ja presmetuva starosta na Avionot
	int starost(int cgod)
	{
		return cgod-god;
	}

	//Funkcija za pechatenje na avionot
	void pechati(int tgod)
	{
		

		cout<<marka<<"\t"<<patnici<<"\t"<<brzina<<"km/h\t"<<god<<"\t"<<starost(tgod)<<"god"<<endl;
	}
	//Prototip na funkcijata za pechatenje na site avioni so nad 400patnici koj se pobrzi od 900km/h
	void test(avion a[],int j);
};

void test(avion a[],int j)
{
	int tgod;
	int pomp,pomb;
	int i;

	cout<<"Vnesete ja tekovnata godina:\t";
		cin>>tgod;
	for(i=0;i<j;i++)
	{
		pomp=a[i].getPatnici();
		pomb=a[i].getBrzina();
		if(pomp>400&&pomb>900)
			a[i].pechati(tgod);
	}
}

int main()
{
	int i,j;
	char marka[15];
	int patnici;
	int brzina;
	int godina;

	avion a[50];
	cout<<"Kolku avioni sakas da vneses:\t";
	cin>> j;

	cout<<endl;
	
	for(i=0;i<j;i++)
	{
		cout<<endl;
		cout<<"Vnesi informacii za avion broj: "<<i+1<<endl; 
		cout<<"Vnesi marka na avionit:\t"; cin>>marka; a[i].setMarka(marka);
		cout<<"Vnesi broj na patnici:\t"; cin>>patnici; a[i].setPatnici(patnici);
		cout<<"Vnesi brzina na avionot:\t"; cin>>brzina; a[i].setBrzina(brzina);
		cout<<"Vnesi godina na proizvodstvo:\t"; cin>>godina; a[i].setGod(godina);
	}
	test(a,j);

	system("PAUSE");
	return 0;
}
Last edited on
The constructor is used to initialize an object when the object is created. If no constructor is specified the default constructor is used. On line 92 the default constructor is used to initialize all the 50 objects in the array.
closed account (zb0S216C)
In addition to Peter87's post:

A default constructor is used to create and initialise an object with no special parameters. Default constructors can be called like so:

1
2
avion x; // Default constructor;
avion(); // Creates a temporary 'avion' object and calls its default constructor. 


A copy-constructor creates a new object based on an existing one. The copy-constructor can be invoked in two ways:

1
2
3
avion x;
avion y(x); // Copy constructor called.
avion z = y; // Copy constructor called. 

A trivial copy-constructor (a constructor provided by the compiler) will perform a member-wise copy of the object given to it.

Wazzak
How about the constructor with initialized values, like
1
2
3
4
5
6
7
8
avion(char* mar=" ",int pat=0,int brz=0,int go=0)
{
		strncpy(marka,mar,14);
		marka[14]='\0';
		patnici=pat;
		brzina=brz;
		god=go;
	}


avion(char* mar=" ",int pat=0,int brz=0,int go=0)
I'm not using this anywhere I guess, except the default one.
closed account (zb0S216C)
They are default values. The initialiser list is where the data memebers are initialised. This example exemplifies this:

1
2
3
avion(...)
    : marka({0}), patnici(pat), ...
{ }

The code between the colon and braces is the initialiser list. The list can appear in any constructor.

Wazzak
Thank you guys, thanks a lot!
Topic archived. No new replies allowed.