I'm new to learning C++ am now especially learning about polymorphism and inheriatance, so pardon my following question, which I'm not sure if its possible? If not please advice on what I should do instead.
I have a parent class that stores the following:
ShapeTwo (string, bool, int, int)
And I have two classes (Square and Rectangle) deriving from the parent class. And I have managed to push my sub-classes into this vector:
vector<ShapeTwo*> objs;
objs.push_back(new Square(square));
objs.push_back(new Rectangle(rectangle));
so now my objs.size() = 2, since there are 2 ShapeTwo in there now.
Right now I'm stuck at figuring out how to loop it in a way that it will print out every individual type stored in each element in the class vector. The program can compile and run, but the results are not expected.
//loop to print out each shape details
for(int i = 0; i < objs.size(); i++)
{
cout << "Shape no. :" << objs.at(i) << endl;
cout << "Name :" << objs[i] -> getName() << endl;
cout << "Bool type :" << objs[i] -> getWarpSpace() << endl;
cout << endl;
}
//results
Shape no. :0x9363a60
Name :
Bool type :
Shape no. :0x9363ae0
Name :
Bool type :
It's printing out some weird characters when I'm just trying to print out the position of the vector. And the other values I'm trying to get is not printing out. How do I check if my vector has the correct values stored?
Please help.
Edited to include Declarations
ShapeTwo.h
class ShapeTwo
{
protected:
string name, specialType;
bool containsWarpSpace;
public:
ShapeTwo();
ShapeTwo(string, bool);
string getName();
void setName(string);
void setWarpSpace(string);
string getWarpSpace();
};
class Square:public ShapeTwo
{
public:
Square();
Square(string, bool, int, int);
};
class Rectangle:public ShapeTwo
{
public:
Rectangle();
Rectangle(string, bool, int, int);
};
Edited to include Definitions
void ShapeTwo::setName(string name)
{
this -> name = name;
}
string ShapeTwo::getName()
{
return (name);
}