C++类的继承问题
有报错,请高手解答!谢谢#include<iostream>usingnamespacestd;classpoint{public:point(int,int);void...
有报错,请高手解答!谢谢
#include<iostream>
using namespace std;
class point{
public:
point(int,int);
void setPoint(int a,int b){int x=a;int y=b;}
};
point(int a,int b){point::setPoint(a,b);}
class circle:public point{
private:
double radius;
public:
circle(double,int,int);
void setRadius(double r){radius=r>0 ? r:0;}
double getRadius(){return radius;}
double area(){return 3.14*radius*radius;}
void print(){
cout<<"圆:Radius="<<radius;
}
};
circle::circle(double r,int x,int y):point(a,b){circle::setRadius(r);}
class cylinder:public circle{
private:
double height;
public:
cylinder(double,double,int, int);
void setHeight(double h){height=h>0 ? h:0;}
double getHeight(){return height;}
double area(){return 2*circle::area()+2*3.14*getRadius()*heighr;}
double volume(){return circle::area()*height;}
void print(){
circle::print();
cout<<"圆柱:Height="<<height;
}
};
cylinder::cylinder(double h,double r,int x, int y):circle(r,x,y){cylinder::setHeight(h);}
void main(){
point p(2,3);
circle cr(5,2,3);
cr.print();
cr.area();
cylinder cl(4,5,2,3);
cl.print();
cl.area();
cl.volume();
} 展开
#include<iostream>
using namespace std;
class point{
public:
point(int,int);
void setPoint(int a,int b){int x=a;int y=b;}
};
point(int a,int b){point::setPoint(a,b);}
class circle:public point{
private:
double radius;
public:
circle(double,int,int);
void setRadius(double r){radius=r>0 ? r:0;}
double getRadius(){return radius;}
double area(){return 3.14*radius*radius;}
void print(){
cout<<"圆:Radius="<<radius;
}
};
circle::circle(double r,int x,int y):point(a,b){circle::setRadius(r);}
class cylinder:public circle{
private:
double height;
public:
cylinder(double,double,int, int);
void setHeight(double h){height=h>0 ? h:0;}
double getHeight(){return height;}
double area(){return 2*circle::area()+2*3.14*getRadius()*heighr;}
double volume(){return circle::area()*height;}
void print(){
circle::print();
cout<<"圆柱:Height="<<height;
}
};
cylinder::cylinder(double h,double r,int x, int y):circle(r,x,y){cylinder::setHeight(h);}
void main(){
point p(2,3);
circle cr(5,2,3);
cr.print();
cr.area();
cylinder cl(4,5,2,3);
cl.print();
cl.area();
cl.volume();
} 展开
2个回答
展开全部
#include<iostream>
using namespace std;
class point{
public:
point(int,int);
void setPoint(int a,int b){int x=a;int y=b;}
};
point::point(int a,int b){point::setPoint(a,b);} //再类外声明 要用类名::
class circle:public point{
private:
double radius;
public:
circle(double,int,int);
void setRadius(double r){radius=r>0 ? r:0;}
double getRadius(){return radius;}
double area(){return 3.14*radius*radius;}
void print(){
cout<<"圆:Radius="<<radius;
}
};
circle::circle(double r,int x,int y):point(x,y){circle::setRadius(r);} //那里来的a,b,换成x,y
class cylinder:public circle{
private:
double height;
public:
cylinder(double,double,int, int);
void setHeight(double h){height=h>0 ? h:0;}
double getHeight(){return height;}
double area(){return 2*circle::area()+2*3.14*getRadius()*height;} //heighr 拼错
double volume(){return circle::area()*height;}
void print(){
circle::print();
cout<<"圆柱:Height="<<height;
}
};
cylinder::cylinder(double h,double r,int x, int y):circle(r,x,y){cylinder::setHeight(h);}
void main(){
point p(2,3);
circle cr(5,2,3);
cr.print();
cr.area();
cylinder cl(4,5,2,3);
cl.print();
cl.area();
cl.volume();
}
using namespace std;
class point{
public:
point(int,int);
void setPoint(int a,int b){int x=a;int y=b;}
};
point::point(int a,int b){point::setPoint(a,b);} //再类外声明 要用类名::
class circle:public point{
private:
double radius;
public:
circle(double,int,int);
void setRadius(double r){radius=r>0 ? r:0;}
double getRadius(){return radius;}
double area(){return 3.14*radius*radius;}
void print(){
cout<<"圆:Radius="<<radius;
}
};
circle::circle(double r,int x,int y):point(x,y){circle::setRadius(r);} //那里来的a,b,换成x,y
class cylinder:public circle{
private:
double height;
public:
cylinder(double,double,int, int);
void setHeight(double h){height=h>0 ? h:0;}
double getHeight(){return height;}
double area(){return 2*circle::area()+2*3.14*getRadius()*height;} //heighr 拼错
double volume(){return circle::area()*height;}
void print(){
circle::print();
cout<<"圆柱:Height="<<height;
}
};
cylinder::cylinder(double h,double r,int x, int y):circle(r,x,y){cylinder::setHeight(h);}
void main(){
point p(2,3);
circle cr(5,2,3);
cr.print();
cr.area();
cylinder cl(4,5,2,3);
cl.print();
cl.area();
cl.volume();
}
追问
虽然编译没有错误了,可是圆的面积和圆柱的体积还是没有算出来呢
追答
你没打印出来
cout<<cl.area(); //打印就可以了
展开全部
point(int a,int b){point::setPoint(a,b);}这一句,改为point::point(int a, int b) { setPoint(a,b);}
然后circle::circle(double r,int x,int y):point(a,b){circle::setRadius(r);} a,b没有定义,要改改
对于第一个错误,你在类外定义应该知道作用域这个东西吧。不然它不知道point这个函数是属于哪个类的所以要用point::point 这样
然后circle::circle(double r,int x,int y):point(a,b){circle::setRadius(r);} a,b没有定义,要改改
对于第一个错误,你在类外定义应该知道作用域这个东西吧。不然它不知道point这个函数是属于哪个类的所以要用point::point 这样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询