【问题标题】:can I make a pointer of base class to point to a derived object if inheritance type is protected? [duplicate]如果继承类型受到保护,我可以使基类的指针指向派生对象吗? [复制]
【发布时间】:2023-04-05 16:33:01
【问题描述】:

如果我有一个从基类继承的派生类(受保护或私有继承),我可以使基类的指针指向派生对象吗? 这是我的 C++ 代码:

#include<iostream.h>
#include<conio.h>
class Geoshape
{
protected:
    int dim1;
    int dim2;

public:
    Geoshape()
        {dim1=0;dim2=0;}
    Geoshape(int x, int y)
        {dim1=x ; dim2=y;}
    void setDim1(int x)
        {dim1=x;}
    int getDim1()
        {return dim1;}
    void setDim2(int y)
        {dim2=y;}
    int getDim2()
        {return dim2;}
    int calculateArea()
        {return dim1*dim2;}
};
class Circle:protected Geoshape
{
public:
    Circle()
        {}
    Circle(int r):Geoshape(r,r)
        {dim1=r;dim2=r;}
    void setR(int r)
        {dim1=dim2=r;}
    int getR()
        {return dim1;}
    float calculateArea()
        {return 22.0/7*dim1*dim2;}
};
class Triangle:public Geoshape
{
public:
    Triangle()
        {}
    Triangle(int x, int y):Geoshape(x,y)
        {}
    void setH(int h)
        {dim2=h;}
    int getH()
        {return dim2;}
    void setB(int b)
        {dim1=b;}
    int getB()
        {return dim1;}
    float calculateArea()
        {return .5*dim1*dim2;}
};
class Rectangle:public Geoshape
{
public:
    Rectangle()
        {}
    Rectangle(int x, int y):Geoshape(x,y)
        {}
    void setL(int l)
        {dim1=l;}
    int getL()
        {return dim1;}
    void setH(int h)
        {dim2=h;}
    int getH()
        {return dim2;}
};
class Square:protected Rectangle
{
public:
    Square()
        {}
    Square(int l):Rectangle(l,l)
        {dim1=l;dim2=l;}
    void setL(int l)
        {dim1=dim2=l;}
    int getL()
        {return dim1;}
    float calculateArea()
        {return dim1*dim1;}
};

void main()
{
clrscr();
cout<<"enter circle raduis: ";
int raduis;
cin>>raduis;
Circle c1(raduis);
cout<< "this is area of Circle: "<<c1.calculateArea();
getch();
cout<<"\n\nenter base of triangle: ";
int base;
cin>>base;
cout<<"enter height of triangle: ";
int height;
cin>>height;
Triangle t1(base,height);
cout<< "this is area of Triangle: "<<t1.calculateArea();
getch();
cout<<"\n\nenter length of rectangle: ";
int length;
cin>>length;
cout<<"enter height of rectangle: ";
int height1;
cin>>height1;
Rectangle r1(length,height1);
cout<< "this is area of Rectangle: "<<r1.calculateArea();

getch();
cout<<"\n\nenter length of square: ";
int len;
cin>>len;
Square s1(len);
cout<< "this is area of Square: "<<s1.calculateArea();
Geoshape *p1;Geoshape *p2;Geoshape *p3;Geoshape *p4;
p2=&t1;
p3=&r1;
getch();
}

我想在 main 中添加这两行:

p1=&c1;
p4=&s1; 但这会产生错误!

【问题讨论】:

  • 编译器说“不”,你不相信吗? :)
  • 受保护的继承?你为什么要用那个? (公共 -> 是一个,私有 -> 根据实现,受保护 -> !?!?
  • For future reference, you'll want to provide a minimal, complete, and verifiable example(强调最小化;不过你已经完成且可验证)
  • 拥有准确的错误文本会有所帮助..
  • @Borgleader 如果你想将一个类的数据成员放入一个单独的结构中,这很有用,也许是为了与 C 通信。受保护的从结构继承你的类以获取该数据,但也让它可用于派生类。朋友帮助函数可以提取指向基本结构的指针以传递给 C 函数。

标签: c++ oop inheritance


【解决方案1】:

您只能从类内部(从成员函数和静态成员函数)、从派生类(仅在公共或受保护继承的情况下)或从友函数执行此操作(转换为“基”类型)。要不受限制地执行此操作,您可以:

  • 使用公共继承,
  • 提供用户指定的转换算子,
  • 提供一个函数来返回这样的指针(本质上和上面一样,但不是“operator T()”的形式,而是“T* getBase()”之类的)——成员函数,静态成员独立朋友功能的功能。

【讨论】:

猜你喜欢
  • 2014-06-16
  • 2021-07-02
  • 2019-10-03
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2014-09-12
相关资源
最近更新 更多