Explanations for Constructors

I kept reading and watching tutorials for Constructor's types but one tutorial, mixes with the previous one, and I get confused lol.

What does a Default constructor, copy constructor and a 'constructor that initializes the values' do ?

I red that the dafult const should be contained everytime you want to define another constructor.

My exam is on tuesday, I have to study fast :/
closed account (zb0S216C)
Bashkim Ukshini wrote:
What does a Default constructor, copy constructor and a 'constructor that initializes the values' do ?

A default constructor's responsibility it to initialise all non-static data members to a respectable value. A copy constructor takes a constant reference to another instance of the class. Its job is to initialise the current object by using the values of the passed object. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Object 
{
    public:
        // Default constructor:
        Object(int init_value = 0) : member_(init_value)
        { }

        // Copy constructor:
        Object(const Object &init_object) : member_(init_object.member_)
        { }

    private:
        int member_;
};

In this code, within the copy constructor, this instance uses init_object to initialise its member_ member.

I assume constructor that initialises the values is a reference to the constructor initialiser list. In the above code, notice the colon after the constructor's parameter list? The code you see between the colon and braces is called the initialiser list. This is used to initialise the non-static data members of the class. Members initialised within the list are guaranteed to be initialised before the body of the constructor is executed. Note that it's within the initialiser list where members are initialised. Members given a value within the constructor's body is not member initialisation.

Wazzak
Last edited on
You guys rule! :)
Thanks a lot !!
I'm trying to test some things from my books, but I got tons of errors, can you please check and see what could be wrong. (I also see a library I have to include "#include "stdafx.h"", but I can't include it, i get some errors too..

Check the code please:

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
#include <iostream>
#include <string>

using namespace std;

class udella
{
private:
	char avtor[50];
	char dello[50];
	int god;
	float cena;
	char ponuduvac[50];

public:
	
	udella(char* a=" ",char* d=" ", int g=0, float c=0; char* p=" ")
	{
		strcpy(avtor,a,49);
		avtor[49]='\0';
		strcpy(dello,d,49);
		dello[49]='\0';
		god=g;
		cena=c;
		strcpy(ponuduvac,p,49);
		ponuduvac[49]='\0';
	}



}


int main()
{
	char a[50];
	char d[50];
	int g;
	fload c;
	char p[50];

	cin.get();
	return 0;
}
closed account (zb0S216C)
See my answer in this thread: http://www.cplusplus.com/forum/general/65444/#msg353789

Wazzak
Still the same bro :/
Should I try declaring an instance of that class at the end, if that makes any change? :/

check s'shot for some of the errors:
http://i2.lulzimg.com/192d67da9c.jpg


here's the code again:
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
#include <iostream>
#include <string>

using namespace std;

class udella
{
private:
	char avtor[50];
	char dello[50];
	int god;
	float cena;
	char ponuduvac[50];

public:
	
	udella(char* a=" ",char* d=" ", int g=0, float c=0; char* p=" ")
	{
		strcpy(avtor,a,49);
		avtor[49]='\0';
		strcpy(dello,d,49);
		dello[49]='\0';
		god=g;
		cena=c;
		strcpy(ponuduvac,p,49);
		ponuduvac[49]='\0';
	}

	udella(const udella& u)
	{
		strcpy(avtor,u.avtor,49);
		avtor[49]='\0';
		strcpy(dello,u.dello,49);
		dello[49]='\0';
		god=u.god;
		cena=u.cena;
		strcpy(ponuduvac,u.ponuduvac,49);
		ponuduvac[49]='\0';

	}



};


int main()
{
	char a[50];
	char d[50];
	int g;
	fload c;
	char p[50];

	cin.get();
	return 0;
}
closed account (zb0S216C)
There's a semi-colon within the parameter list of the first constructor; after c.

Wazzak
Last edited on
I see the VS 2010, doesnt allow this

1
2
strcpy(avtor,u.avtor,49);
		avtor[49]='\0';


I see it on my book, they wrote it that way, how can I make it work too?
closed account (zb0S216C)
It seems std::strcpy() only accepts 2 arguments[1].

References:
[1] http://www.cplusplus.com/reference/clibrary/cstring/strcpy/


Wazzak
A default constructor is a constructor that is called at the initialization of the of any object.

A copy constructor is a constructor that is called when an object is initialized to another struct of the same type.

Another important thing is, you may want to learn about destructors and assignment operator overloading.

Let me show you some 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
struct TestStructType
{
  //data
  int * p_int;
  int _int;
  //the default constructor takes no arguments. data initialized by being implicitly assigned to 0
  TestStructType() : p_int(NULL), _int(0) 
  {
  }
  //regular constructor, data initialized to arguments
  TestStructType(int * pointer, int intger) : p_int(pointer), _int(intger)
  {
  }
  //copy constructor. Doesn't have to be const, but recommended. Argument must be pass-by-reference
  TestStructType( const TestStructType & source) : p_int(new p_int(*source.p_int), _int(source._int)
  {
  }
  //assignment operator overloading
  TestStructType & operator = (TestStructType & source)
  {
       if(source.p_int)
            p_int = new int(*source.p_int);
    _int= source._int;
     return *this;
  }
//destructor
  ~TestStructType()
  {
      if(p_int)
         delete p_int;
  }
};

//main
int main()
{


  TestStructType test1; //default constructor called!

  TestStructType test2= test1; //copy constructor called!
  TestStructType test3(test2); //copy constructor called!

  int i=32;
  TestStruct test4(&i, 49); //constructor called!

  test3=test1; //assignment operator called!
  test1(test3); //assignment operator called!


//stack-dynamic structs will call their respective destructors when they exit the scope. Alternatively, you can explicitely call them with [object_name].~TestStructType();

return 0;
}
Last edited on
Topic archived. No new replies allowed.