2、编写程序,声明一个基类shape,再派生出一个retangle和circle类, 二者都有getarea()函数,计算对象

我要完整的程序,请各位大侠指点下
2024-12-03 20:21:30
推荐回答(2个)
回答1:

class Shape
{
public:
virtual double getArea() =0;
};

class Rectangle
{
public:
virtual double getArea(){//具体实现请自己实现}
};
class Circle
{
public:
virtual double getArea(){//具体实现请自己实现}
};

回答2:

public abstract class shape{
public abstract double getarea();
}

class circle extends shape{
final double PI=3.14;
private double radius;
public double getRadius(){
return this.radius;
}
public setRadius(dobule r){
this.radius = r;
}
public double getarea(){
return PI*radius*radius;
}
}

class rectangle extends shape{
private double length ;
private double width;
public double getLength (){
return this.length;
}
public setLength(double len){
this.length = len;
}
public double getWidth (){
return this.width;
}
public setWidth(double w){
this.width = w;
}
public double getarea(){
return length*width;
}
}