为什么将CRectangle类、CTriangle的继承关系换乘protected和private编译就会报错,只能用public继承
#include<iostream.h>classCPolygon{protected:intwidth,height;public:voidset_values(int...
# include <iostream.h>
class CPolygon{
protected:
int width,height;
public:
void set_values(int a,int b){width=a;height=b;}
};
class CRectangle:public CPolygon{
public:
int area(void){return (width*height);}
};
class CTriangle:public CPolygon{
public:
int area(void){return (width*height/2);}
};
void main(){
CRectangle rect;
CTriangle trgl;
rect.set_values(4,5);
trgl.set_values(4,5);
cout<<rect.area()<<endl;
cout<<trgl.area()<<endl;
} 展开
class CPolygon{
protected:
int width,height;
public:
void set_values(int a,int b){width=a;height=b;}
};
class CRectangle:public CPolygon{
public:
int area(void){return (width*height);}
};
class CTriangle:public CPolygon{
public:
int area(void){return (width*height/2);}
};
void main(){
CRectangle rect;
CTriangle trgl;
rect.set_values(4,5);
trgl.set_values(4,5);
cout<<rect.area()<<endl;
cout<<trgl.area()<<endl;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询