简体   繁体   中英

How to access member functions of a derived class through a base pointer?

#include <iostream>
#include <string>
using namespace std;

class Shape {
    private:
        string name;
    public:
        Shape(){name = "Shape";}
        Shape(string n){name = n;}
        void setName(string n){name = n;}
        string getName(){return name;}
        double calcarea(){return 0;}
};

class Square : public Shape {
    private:
        double side;
    public:
    Square(){side = 1; Shape("Square");}
    Square(double s){side = s; Shape("Square");}
    void setSide(double s){side = s;}
    double getSide(){return side;}
    double calcarea(){double area = side * side; return area;}
};

class Circle : public Shape{
    private:
        double radius;
    public:
        Circle(){radius = 1; Shape("Circle");}
        Circle(double r){radius = r; Shape("Circle");}
        Circle(double ra, string n){radius = ra; setName(n);}
        void setRadius(double r){radius = r;}
        double getRadius(){return radius;}
        double calcarea(){double area = 3.14 * radius * radius; return area;}

};

int main()
{
    Shape* shapepointer[5];
    shapepointer[0] = new Shape;
    shapepointer[1] = new Square;
    shapepointer[2] = new Circle;
    shapepointer[3] = new Circle;
    shapepointer[4] = new Square;

    shapepointer[1]->setSide(3);
    shapepointer[2]->setRadius(2);
    shapepointer[3]->setRadius(7);
    shapepointer[4]->setSide(5);


    return 0;
}

I apologize if I haven't formatted this post correctly or failed to do something in the post that would make your lives easier, I'm new here sorry mods/people reading this. So I've been task with:

"Create a main() function to create an array of base pointers named shapepointer[] of size 5. Dynamically create/instantiate the following shapes for these pointers..."

Now everything has been going fine, I created the classes, their constructors, overloaded constructors, with accessors and mutators. However now that I've instantiated an array in main() that is a base pointer I get the errors no member named 'setSide' in 'Shape' and no member named 'setRadius' in 'Shape'

From what I interpreted from the error messages, it seems the Shape pointer is of the base class therefore can't access the member functions of the derived class it is attempting to use. I have no clue how to get around this though since the instructions of the task requires the pointer to be a base class pointer. I'm also confused because if what I interpreted is true, how are there no errors for dynamically instantiating objects from other classes using the base array/pointer that I used? I'd really appreciate some help with figuring this out, thanks.

I figured it out. A class mate helped me:

Shape* shapepointer[5];
    
    shapepointer[0] = new Shape();
    shapepointer[1] = new Square(3);
    shapepointer[2] = new Circle(2);
    shapepointer[3] = new Circle(7);
    shapepointer[4] = new Square(5);

Simple fix, changed the dynamically allocated objects into constructors. The calcarea() function was also set to become a virtual function in base class 'Shape'.

In my humble opinion, the error comes because one element in the array of shapepointer is pointed " Shape " class object. In the memory, the pointer of shapepointer class object can be accessed in the member of shape class.On the other hand, the shapepointer pointer doesn't have any function of setSide or setRadius .

You can define these functions in the Shape class as virtual function and enjoy it.

int main()
{
    Shape* shapepointer[5];
    shapepointer[0] = new Shape;
    shapepointer[1] = new Square;
    shapepointer[2] = new Circle;
    shapepointer[3] = new Circle;
    shapepointer[4] = new Square;

    ((Square*)shapepointer[1])->setSide(3);
    ((Circle*)shapepointer[2])->setRadius(2);
    ((Circle*)shapepointer[3])->setRadius(7);
    ((Square*)shapepointer[4])->setSide(5);


    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM