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.
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.
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?
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.
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.
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.
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);
}
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.