2014-11-21 36 views
0

,如果我有一個基礎類(受保護的或私有繼承)派生類繼承我可以讓基類的指針指向派生類對象? 這是我的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(); 
} 

我想補充這兩條線在主:

P1 = &c1;
P4 = &s1; 但是這給錯誤!

+0

編譯器說「不」,你不相信嗎? :) – jrok 2014-11-21 17:15:28

+0

受保護的繼承?你爲什麼要使用它? (公共 - > *是*,私人 - > *在*方面實現的,受保護的 - !?!?> *。*) – Borgleader 2014-11-21 17:15:40

+0

[以供將來參考,你要提供一個** **最小的,完整的,和可覈查的示例](http://stackoverflow.com/help/mcve)(強調最小的;你有完全和可驗證雖然) – Cornstalks 2014-11-21 17:15:47

回答

1

你可以做到這一點(投給「基地」型)只能從類(從成員函數和靜態成員函數)內,從派生類(僅適用於公共或保護繼承的情況下)或者從朋友的功能。若要做到這一點不受限制的能力,您可以:

  • 使用公有繼承,
  • 提供用戶指定的轉換操作符,
  • 提供將返回這樣的指針(essentialy與上述相同的功能,但不是「運算符T()」,而是「T * getBase()」或者這樣的形式) - 成員函數,獨立好友函數的靜態成員函數。
+1

感謝你們所有人。這裏是我的問題的答案 http://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b – Mouneer 2014-11-21 18:14:48

相關問題