If I have a test code :
Number const * n = nullptr;
double val = 0;
std::cin >> val;
n = new Integer( int( val ));
if( n->intValue() != int( val )) {
std::cout << "intValue() is wrong\n";
}
and I have a class Integer, to be able to evaluate n->intValue(), does it mean that I have to create a method call intValue( ) in the class Integer?
I tried to create a methode but it shows error 'const class Number' has no member named 'intValue'.
My class code :
#include <iostream>
using namespace std;
// Base class Number
class Number{
public:
Number(double theVal){
val = theVal;
cout << "Created a number with value " << val << endl;
}
protected:
double val;
};
class Integer : public Number{
public :
Integer(int val):Number(val){\
cout << "Created an integer with value " << val << endl;
}
int intValue(){
return (int)val;
}
double doubleValue(){
return (double)val;
}
};
class Double : public Number{
public :
Double(double val):Number(val){
cout << "Created a double with value " << val << endl;}
int intValue(){
return (int)val;
}
double doubleValue(){
return (double)val;
}
};