Constructor to Receive Any Primitive Type

Oct 15, 2014 at 10:00am
I'm trying to create a constructor that can receive any primitive type of int, double, or float, and it will convert these to an int. I am not sure if there is a particular syntax for it, or if I have to do it manually.
Oct 15, 2014 at 10:07am
Have you looked at templates?
Oct 15, 2014 at 10:11am
If it's going to simply cast the values to int why not simply have one constructor that takes an integer? You could still pass floats and doubles to it and their values would be converted to an int automatically.
Oct 15, 2014 at 10:14am
I'm an absolute beginner and in one of my first programming classes that is in C++ and having to design a class was my first assignment. I'm not sure what you mean. Where are these templates that I can look at?
Oct 15, 2014 at 10:36am
I'm trying to create a constructor that can receive any primitive type of int, double, or float, and it will convert these to an int.
1
2
3
4
5
6
7
8
9
10
11
12
class foo
{
    public:
    foo(int i) {};
};

int main()
{
    foo x(5); //Hello int
    foo y(3.14); //Hello double
    foo z(2.71f); //Hello float
}
Last edited on Oct 15, 2014 at 10:36am
Oct 15, 2014 at 11:14am
I don't want to simply just convert the double or float to an int. I also need to use what is after the decimal point. For example, my constructor takes in two primitive types, the first number represents feet, and the second represents inches.

I would convert the double or float to an int so that I would get feet and inches left. So if I entered in 3.25, I would get 3 feet and 3 inches.
Last edited on Oct 15, 2014 at 11:15am
Oct 15, 2014 at 11:17am
You could try simply overloading the constructor in MiiNiPaa's example; just write one for each type.
Oct 15, 2014 at 11:22am
So you want something like:
1
2
3
4
5
6
7
8
9
10
class foo
{
public:
    foo(int feet, int inches) {/*do something*/};
    foo(double feet_) //Captures floats too
    {
        int feet = static_cast<int>(feet_);
        int inches = static_cast<int>((feet_ - feet) * 12);
    }
};
Oct 15, 2014 at 11:44am
I'm not sure, especially since that code looks more advanced that what I am initially learning, and I don't understand a lot of the syntax being used there.

I could try overloading, but then I would have too many constructors and that would look messy. Is that the only way?

So I have one constructor that takes in no parameters, and this one assumes feet and inches are 0. Then I have another constructor which takes only one parameter, and this one initializes inches to whatever number that is. The third constructor takes 2 parameters and sets the first number to feet, and the second to inches.

I have to be able to take in an int, double, or float. If I receive a double or float, I have to convert that to feet and inches, accordingly (although if inches is a double or float, I will just round up or down). So if initialize a constructor like Measurement(3.5, 6), and then I tried to print out the Measurement, the console would show 4 feet.
Last edited on Oct 15, 2014 at 11:45am
Oct 15, 2014 at 11:52am
Actually you can use default values:
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
#include <iostream>

struct Measurement
{
    Measurement(double feet_ = 0, double inches_ = 0) : 
        feet(feet_), 
        inches((feet_ - feet) * 12 + inches_) 
    { 
        if(inches >= 12)
            feet += inches / 12;
            inches %= 12;
    }
    int feet;
    int inches;
};

std::ostream& operator<<(std::ostream& out, const Measurement& rhs)
{
    return out << rhs.feet << " feet, " << rhs.inches << " inches";
}

int main()
{
    std::cout << Measurement() << '\n';
    std::cout << Measurement(1) << '\n';
    std::cout << Measurement(4.25) << '\n';
    std::cout << Measurement(3.5f, 6) << '\n';
}
0 feet, 0 inches
1 feet, 0 inches
4 feet, 3 inches
4 feet, 0 inches

Last edited on Oct 15, 2014 at 11:58am
Oct 15, 2014 at 1:32pm
I reread the instruction for my assignment a bit. So, the first solution MiiNiPaa would work. However, for some decimal numbers, simply plugging in a decimal number when the constructor is expecting an int results in a huge negative number.
Oct 15, 2014 at 1:40pm
for some decimal numbers, simply plugging in a decimal number when the constructor is expecting an int results in a huge negative number.
I suppose you tried a huge decimal value?
int has a range. All values are to be within this range. If value is larger than that, then you have problems.
Doubles can represent more value for cost of precision in larger numbers:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>
#include <cmath>

int main()
{
    double d = 1e17;
    std::cout << std::fixed << d << '\n' << nextafter(d , 1.0/0);
}
100000000000000000.000000
100000000000000016.000000
(yes, there is no way to represent 100000000000000010 as double)

Can you show number you have problem with?
Oct 15, 2014 at 1:48pm
Yes I tried to do this:

m1(127.86);

My constructors only take ints, so in my .h and .cpp file they are:

Measurement()
Measurement(int in)
Measurement(int ft, int in)

and so the constructor basically just does this (in the .cpp)

Measurement(int in)
{
inches = in;
}

Where "inches" is a private value that is also an int.

When I go to my driver, and try to put in m1(127.86); I got a number of - 858993460 when I tried to cout the inches of my Measurement object. However, cout'ing the inches or feet of any of my other object worked fine, but the numbers I put in for all of them had no more than 1 number after the decimal point.
Oct 15, 2014 at 2:03pm
When I go to my driver, and try to put in m1(127.86); I got a number of - 858993460
Do you have feet variable in your inch calculations somewhere? because it looks like uninitialized variable use.
Oct 15, 2014 at 2:15pm
I think I may see the problem. I do know some Java, and when making a class in Java I could do something like this with Constructors:

Measurement(int in)
{
Measurement(0, in)
}

Measurement(int in, int ft)
{
inches = in;
feet = ft;
}

Can you not do that in C++?
Oct 15, 2014 at 2:37pm
Can you not do that in C++?
You can, by constructor delegation:
Measurement(int in) : Measurement(0, in) {} 
Requires C++11
Oct 15, 2014 at 2:52pm
Thanks for the help.
Topic archived. No new replies allowed.