Overloading Contructors #2

The following code works fine and did what I wanted it to do, however if I change the double data type to float in constructor 2 parameter double height3 it gives me a truncation from double to float error.

Can someone explain why this is happening because it allows it in the first constructor hence why I left height2 with a float data type.

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
#include "stdafx.h"
#include <iostream>
#include <string>

//create a class called Bio
class Bio
{
//give access to variables outside the class
public:

	std::string name; //create a string variable called name
	int age;		  //create an integer variable called age
	int weight;		  //create an  integer variable called weight
	float height;	  //create a  float variable called height

//Constructor
	Bio(std::string name2, int age2, int weight2, float height2)
	{

	name = name2;
	age = age2;
	weight = weight2;
	height = height2;

	}

//Constructor2
	Bio(std::string name3, int age3, int weight3, double height3)
	{

	name = name3;
	age = age3;
	weight = weight3;
	height = height3;

	}


};

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//create objects of class Bio called obj and obj2 with bio details.

Bio obj("Player 1",350,510,6.11);
Bio obj2("Player 2",210,260,5.6);

//print out constructor info for both players

cout << "Name: " << obj.name << "\nAge: " << obj.age << "\nWeight: " <<
obj.weight << "\nHeight: " << obj.height << endl;

cout << endl;

cout << "Name: " << obj2.name << "\nAge: " << obj2.age << "\nWeight: " <<
obj2.weight << "\nHeight: " << obj2.height << endl;

cout << endl;

	system("pause");
	return 0;
}
gives me a truncation from double to float error.
It should be a warning instead of errors unless you made it to be error yourself.

If you change double to float in second constructor, you will have two constructor with same signature. So compiler does not know which of two it should call and stops.

You did not get any warnings about truncation in your current code because first constructor never get called. You are calling second one. Remember: all floating point values are double by default.
Adding to MiiNiPaa's point, if you wish to declare a floating point single-precision literal instead of double, you'll have to say something like this:
Bio obj2("Player 2",210,260,5.6f);
Notice the 'f' after 5.6.
Thanks for the help guys.

//Constructor2
Bio(std::string name3, int age3, float height3, int weight3)


changing it back to float and swapping weight and height parameters around allowed the signature to be different as MiiNiPaa said.

But I still doen't understand why do you need two essentually equivalent constructors. Now I can congratulate you: now you need to keep track of complex conversion and promotion rules to avoid accident call to a wrong constructor.
Topic archived. No new replies allowed.