简体   繁体   English

如何通过基指针访问派生 class 的成员函数?

[英]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..." “创建一个 main() function 以创建一个名为 shapepointer[] 大小为 5 的基本指针数组。为这些指针动态创建/实例化以下形状...”

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'但是,现在我已经在 main() 中实例化了一个作为基指针的数组,我得到错误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.根据我从错误消息中的解释,形状指针似乎是基础 class 因此无法访问它尝试使用的派生 class 的成员函数。 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.我不知道如何解决这个问题,因为任务的说明要求指针是基本的 class 指针。 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: class 伙伴帮助了我:

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'. calcarea() function 也被设置为基础 class 'Shape' 中的虚拟 function。

In my humble opinion, the error comes because one element in the array of shapepointer is pointed " Shape " class object.在我看来,错误的出现是因为shapepointer数组中的一个元素指向“ Shape ” class object。 In the memory, the pointer of shapepointer class object can be accessed in the member of shape class.在 memory 中, shapepointer class object 的指针可以在ZA2F2ED4F8EBC4ABC2CBB4DZ21A29的成员中访问On the other hand, the shapepointer pointer doesn't have any function of setSide or setRadius .另一方面, shapepointer指针没有setSidesetRadius 的任何 function

You can define these functions in the Shape class as virtual function and enjoy it.您可以在Shape class 中将这些功能定义为虚拟 function并享受它。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么派生类不能通过指向基类的指针访问其基类的受保护成员? - Why can a derived class not access a protected member of its base class through a pointer to base? 尝试通过基类指针访问派生类成员函数将导致哪种错误? - What kind of error will result from attempting to access a derived class member function through a base class pointer? 如何通过类型为base的指针访问派生类中的成员,但指向派生类 - How to access member in derived classes by a pointer of type base but pointing to the derived class 从基类指针访问派生类成员的设计替代方案 - Design alternative for access to derived class member from base class pointer C++ 从基类指针访问派生类成员 - C++ Access derived class member from base class pointer 使用基类函数指针访问派生类成员函数 - Using base class function pointer to access derived class member function 从基类的指针访问派生类的成员 - Access member of derived class from pointer of base class 通过基类指针获取派生成员变量 - Getting derived member variable through base class pointer 使用对基类的引用访问派生类的私有成员函数 - Access to private member functions of derived class using reference to a base class 我们可以在派生类中访问基类的受保护成员函数吗? - Can we access protected member functions of base class in derived class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM