Constructor with default empty parameters

Hello. What's the right way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
//Constructor
Laptop::Laptop(string model, double price, string manafacturer = "", string CPU = "", string RAM = "", string GPU = "", string monitor = "", string baterry = "")
{
	this->model = model;
	this -> price = price;
	this->manufacturer = manafacturer;
	this->CPU = CPU;
	this->RAM = RAM;
	this->GPU = GPU;
	this->monitor = monitor;
	this->battery = baterry;
}

I need only "model" and "price" to be mandatory.
I got another question.
How can I call the Battery constructor (the one which takes 2 arguments) in the Laptop constructor (so that the baterry member would be initialized too).
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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class Battery
{
public:
	Battery();
	Battery(string battery, double battery_life);
	~Battery();

private:
	string battery;
	double battery_life;

};

Battery::Battery(string battery, double battery_life)
{
	this->battery = battery;
	this->battery_life = battery_life;
}
Battery::Battery()
{
	this->battery = "";
	this->battery_life = 0;
}
Battery::~Battery()
{
}
class Laptop
{
public:
	Laptop::Laptop(string model, double price, string manafacturer, string CPU, string RAM, string GPU, string monitor);
	~Laptop();

private:
	
	string model;
	string manufacturer;
	string CPU;
	string RAM;
	Battery baterry;
	string GPU;
	string monitor;
	double price;

};

Laptop::Laptop(string model, double price, string manafacturer = " ", string CPU = " ", string RAM = " ", string GPU = " ", string monitor = " ")
{
	this->model = model;
	this -> price = price;
	this->manufacturer = manafacturer;
	this->CPU = CPU;
	this->RAM = RAM;
	this->GPU = GPU;
	this->monitor = monitor;
}

Laptop::~Laptop()
{
}
int main()
{


	system("Pause");
	return 0;
}
So, this would be the right way?
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
class Laptop
{
public:
	Laptop(string model, double price, string battery, double battery_life, string manafacturer, string CPU, string RAM, string GPU, string monitor);
	~Laptop();

private:
	
	string model;
	string manufacturer;
	string CPU;
	string RAM;
	Battery *baterry;
	string GPU;
	string monitor;
	double price;

};

Laptop::Laptop(string model, double price, string battery, double battery_life, string manafacturer = " ", string CPU = " ", string RAM = " ", string GPU = " ", string monitor = " ")
{
		this->model = model;
		this->price = price;
		this->manufacturer = manafacturer;
		this->CPU = CPU;
		this->RAM = RAM;
		this->GPU = GPU;
		this->monitor = monitor;
		this->baterry=new Battery(battery, battery_life);
	
}
Last edited on
No. Now you have a dynamically allocated object that the Laptop has to own responsibly. That is not what you want.

Read about "initializer list". Then you should see that you don't currently initialize any of the member variables of Laptop, and how to initialize class members.
I'm aware of initializer list. But still don't get what difference does it make.
Then you should see that you don't currently initialize any of the member variables of Laptop

I'm not sure if I understand you. I'm sure they're initialized.
I'm sure they're initialized.

Yes, they are. In your latest code the members are initialized between lines 20 and 21. Lines 22-29 do not initialize anything (except the unnamed Battery object in free store on line 29).

"aware of"? Read more.
1
2
3
4
Laptop::Laptop(string model, double price, string battery, double battery_life )
: model( model ), price( price ), baterry( battery, battery_life )
{
}

Fill in the rest of the initializer list yourself.


Edit.
The '=' is deliciously perilous in C++ syntax. Many mistake it for:
* assignment, when it does initialization
* initialization, when it does assignment
* equality comparison, when it is a typo
Last edited on
Yes, they are. In your latest code the members are initialized between lines 20 and 21. Lines 22-29 do not initialize anything (except the unnamed Battery object in free store on line 29).

I still don't get it.

 
this->model = model;

The argument "model" is assigned to the member model. What's wrong with this?
What's wrong with this?

I don't think there is in this specific context but I think keskiverto is trying to say in your example it's relating to his comment:

* initialization, when it does assignment


He's saying (i hope) it's a lot better to use initialisation lists.
take a read of this:
http://stackoverflow.com/questions/1598967/benefits-of-initialization-lists

In other words, there's a hell of a lot less ambiguity when you use initialisation lists.
Last edited on
Yes, indeed.

The argument "model" is assigned to the member model.

That is the point. Assigned. The this->model has already been constructed and does contain some value. The assignment changes the value of an existing object.

Technical meaning:
* Initialization. Object starts existence with specified value.
* Assignment. The current value of an object is replaced with a new value.

Logical view:
* After the constructor of a Laptop has completed, all the member variables of the Laptop have the the values that you want them to have, so your Laptop does not have undefined state. Logically initialized.

If there is something "wrong", it is all the unnecessary work that your constructor does.


The question was:
How can I call the Battery constructor (the one which takes 2 arguments)

The answer is: use initialization list
1
2
3
4
Laptop::Laptop( string battery, double battery_life )
: baterry( battery, battery_life ) // calls constructor to create the baterry object.  This is initialization
{
}


If you don't write initialization list, then the compiler will create it with default constructors:
1
2
3
4
Laptop::Laptop( string battery, double battery_life )
: baterry() // calls constructor to create the baterry.  This is initialization
{
}

But you want to use those parameters, yes?
1
2
3
4
5
6
Laptop::Laptop( string battery, double battery_life )
: baterry() // calls constructor to create the baterry.  This is initialization
{
  Battery temp( battery, battery_life ); // construct a temporary object
  baterry = temp; // this is copy assignment
} // the destructor of temp is called here 

Compared to the initializer list version, there are additional calls to Battery default constructor, copy assignment, and destructor.


Lets try something different:
1
2
3
4
5
class Foo {
  const int bar;
public:
  Foo( int bar );
};

Implement that constructor. The constructor's parameter value must be stored in the class' member bar.
Thank you.
1
2
3
4
5
class Foo {
  const int bar;
public:
  Foo( int bar );
};

I knew this wouldn't work in my way. I just wasn't sure why.
So, first some Foo object would be created with some random value for bar. Then my constructor would be called but won't be able to change bar's value because of const. Right?
Also, is that mean these are different?
1
2
        int ss(5);
	int ss2 = 5;





1
2
3
4
Laptop::Laptop( string battery, double battery_life )
: baterry() // calls constructor to create the baterry.  This is initialization
{
}

Why do I need to call "battery()" instead of just "battery"?




1
2
3
4
5
6
7
8
9
10
Laptop::Laptop( string battery, double battery_life )
: baterry() // calls constructor to create the baterry.  This is initialization
{
  Battery temp( battery, battery_life ); // construct a temporary object
  baterry = temp; // this is copy assignment

  //And I'd be better off using copy constructor?
  //And I don't need overloaded '=' for this?
} // the destructor of temp is called here 
//So, every object I create in a function is deleted at the end of the function? 


So, in general it is a bad idea to have pointer in a class? What are the cases I might need it?



Also, is it bad idea to use references for constructors' parameters?
Last edited on
http://herbsutter.com/2013/05/09/gotw-1-solution/

http://en.cppreference.com/w/cpp/language/initializer_list


A pointer in a class is not a bad idea, if used correctly. One has to understand that there are more than one type of things that a pointer may point to, and each has different role and responsibilities.

For example, a linked list. Self-made list is mainly educational, for the standard library offers linked lists. Nodes are allocated dynamically and a node has a pointer to next node. A node does not usually "own" the next node. The list and the nodes have to keep their pointers in consistent state in operations like insert and erase. The list does own the nodes, and is responsible for deallocating them. A list would be hard to implement without pointers.

Be aware that there are smart pointers (and iterators). Objects that can be used like raw pointers, but which add functionality (+ convenience and safety).

In short, pointers are a big topic.
Topic archived. No new replies allowed.