-4

I am new to c++ and is trying to learn about its Object-Oriented design. I started a small project to test out inheritance and polymorphism but encountered a problem and can't seen to figure out what went wrong.

Whenever I compile, there will be an error "class 'ShapeTwoD' has no member name getx() and gety()". I tried to directly set the x and y value with setx and sety but it still return the same error.

Class ShapeTwoD is the Base class with only the variable 'name' and 'container'. Would appreciate if anyone can direct me to the right direction.

Main.cpp

#include <iostream>
#include <string>
#include "ShapeTwoD.h"
#include "Square.h"

using namespace std;

int main()
{

    cout<<endl;
    ShapeTwoD *shape2D[100];
    ShapeTwoD *sq1 = new Square("Square", true, 4, 6);
    cout << sq1->getName() <<endl;
    cout << sq1->getContainer() <<endl;

    //sq1->setx(4) <<endl;
    //sq1->sety(6) <<endl;

    cout << sq1->getx() <<endl;
    cout << sq1->gety() <<endl;

    cout<<endl;

    delete sq1; 
}

Square.h

#include <iostream>
#include <string>
#include "ShapeTwoD.h"

using namespace std;

class ShapeTwoD; //forward declare

class Square : public ShapeTwoD
{
public:
    int x;
    int y;

    //constructor
    Square(string name, bool container,int x, int y);

    int getx();
    int gety();

    void setx(int x);
    void sety(int y);

};

Square.cpp

#include <iostream>
#include <string>
#include "Square.h"
#include "ShapeTwoD.h"

Square::Square(string name, bool containsWarpSpace, int coordx, int coordy)
   :ShapeTwoD(name, containsWarpSpace)
{
    (*this).x = coordx;
    (*this).y = coordy;
}

int Square::getx()
{
    return (*this).x;
}


int Square::gety()
{
    return (*this).y;
}

void Square::setx(int value)
{
    (*this).x = value;
}

void Square::sety(int value)
{
    (*this).y = value;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Dylan
  • 1
  • 1
  • `class ShapeTwoD; //forward declare` ShapeTwoD` does not have `getx()` function. Only `Square` does. – KamilCuk Feb 07 '19 at 07:56
  • Hi Dylan, welcome to SO. Just letting you know we have a curated [list of good C++ books](https://stackoverflow.com/q/388242/1782465), in case you want to pick one up to help your learning. – Angew is no longer proud of SO Feb 07 '19 at 07:59

2 Answers2

1

That's normal... If you declare sq1 as ShapeTwoD, you have access to ShapeTwoD public member methods/attributes. Even it was instanciated with the Square constructor. Cast it as Square, and you can use getx gety. Or declare getx/gety as methods of ShapeTwoD.

rene-d
  • 343
  • 1
  • 6
  • But if i declare it as Square, how do i put it into shape2D array and still be able to use the functions in Square? I have to do this as i will still be creating other shapes to store as into the base array. – Dylan Feb 07 '19 at 08:09
  • Well to answer you question, you have a base array your base class should have the functions you want use for your different derived classes as virtual functionsomething() and you override it in the derived classes & this way it will be simple for you – Spinkoo Feb 07 '19 at 08:43
  • I did this and it manage to save me from a lot of future trouble. Thank you! – Dylan Feb 07 '19 at 10:08
0

Well this is what you should expect since it has shape2D type , Although constructing it with the square constructor won't allow you to access the derived class members but it will allow you to have a safe type cast to use it.. the simplest way to do it is by :

cout << static_cast<Square*>(sq1)->getx() << endl;
cout << static_cast<Square*>(sq1)->gety() << endl;
Spinkoo
  • 2,080
  • 1
  • 7
  • 23
  • 1
    You should use `dynamic_cast` for downcasting. What if `sq1` is not of type `Square`? – Yksisarvinen Feb 07 '19 at 08:39
  • this is exactly why i specified the part about constructing it with square constructor after all there is a lot to talk to about this subject to explain all in an answer & not a book – Spinkoo Feb 07 '19 at 08:41