public class Circle { protected double radius; public Circle(double radius) { this.radius = radius; } public double area() { return 3.14 * radius * radius; }}public class Cylinder extends Circle { private double height; public Cylinder(double radius, double height) { super(radius); this.height = height; } public double volume() { return area() * height; }}无法输出结果
时间: 2024-02-06 07:04:36 浏览: 129
这个代码只是定义了两个类,没有进行任何的输出操作,因此不会有任何结果输出。如果你想要测试一下 `Cylinder` 类的功能,可以在代码中添加一些输出语句,例如:
```java
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(2.0, 5.0);
System.out.println("底面圆的面积为:" + cylinder.area());
System.out.println("圆柱的体积为:" + cylinder.volume());
}
```
在这个示例中,我们新建了一个 `Cylinder` 对象,并使用该对象的 `area` 和 `volume` 方法来计算底面圆的面积和圆柱的体积,并通过 `System.out.println` 方法将结果输出到控制台。
希望这可以帮助到你!
相关问题
已知Point(点)类,点类的公有派生类Circle(圆)类,圆类的公有派生Cylinder(圆柱体)类如下: class Point { public: virtual double area() { return 0; } }; class Circle:public Point { protected: double m_radius; public: Circle(double r); virtual double area(); }; class Cylinder:public Circle { private: double m_high; public: Cylinder(double r, double h); virtual double area(); }; 试完成成员函数的定义,在主函数中输入圆的半径和圆柱的高,利用指向基类的指针,分别输出圆的面积和圆柱体的表面积。 Input 2个数,分别代表圆的半径和圆柱的高。 Output 圆的面积和圆柱体的表面积(取2位小数)。 Sample Input 1 1.5 2 Sample Output 1 7.06 32.97用c++写出此代码
```
#include <iostream>
#include <iomanip>
using namespace std;
class Point {
public:
virtual double area() { return 0; }
};
class Circle : public Point {
protected:
double m_radius;
public:
Circle(double r) : m_radius(r) {}
virtual double area() { return 3.14 * m_radius * m_radius; }
};
class Cylinder : public Circle {
private:
double m_high;
public:
Cylinder(double r, double h) : Circle(r), m_high(h) {}
virtual double area() { return 2 * Circle::area() + 2 * 3.14 * m_radius * m_high; }
};
int main() {
double r, h;
cin >> r >> h;
Point* p1 = new Circle(r);
Point* p2 = new Cylinder(r, h);
cout << fixed << setprecision(2) << p1->area() << " " << p2->area() << endl;
delete p1;
delete p2;
return 0;
}
```
用C++编写一个程序满足已知抽象类Shape及Shape类的3个公有派生类圆类Circle、矩形类Rectangle、三角形类Triangle声明如下: class Shape { public: virtual double area() const = 0; }; class Circle:public Shape { public: Circle(double r); virtual double area() const; protected: double m_radius; }; class Rectangle:public Shape { public: Rectangle(double w, double h); virtual double area() const; protected: double m_width, m_height; }; class Triangle:public Shape { public: Triangle(double a, double b, double c); virtual double area() const; protected: double m_a, m_b, m_c; }; 试完成成员函数的定义,在主函数中输入圆的半径、矩形的宽和高、三角形的三条边,定义对象,利用函数void printArea(const Shape &s)输出圆、矩形及三角形的面积。 输入 三行,第1行圆的半径,第2行矩形的宽和高,第3行三角形的三条边。 输出 圆、矩形及三角形的面积(取2位小数)。
以下是代码实现:
```c++
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
};
class Circle:public Shape {
public:
Circle(double r) {
m_radius = r;
}
virtual double area() const {
return 3.14 * m_radius * m_radius;
}
protected:
double m_radius;
};
class Rectangle:public Shape {
public:
Rectangle(double w, double h) {
m_width = w;
m_height = h;
}
virtual double area() const {
return m_width * m_height;
}
protected:
double m_width, m_height;
};
class Triangle:public Shape {
public:
Triangle(double a, double b, double c) {
m_a = a;
m_b = b;
m_c = c;
}
virtual double area() const {
double p = (m_a + m_b + m_c) / 2.0;
return sqrt(p * (p - m_a) * (p - m_b) * (p - m_c));
}
protected:
double m_a, m_b, m_c;
};
void printArea(const Shape &s) {
cout << fixed << setprecision(2) << "面积为:" << s.area() << endl;
}
int main() {
double r, w, h, a, b, c;
cin >> r >> w >> h >> a >> b >> c;
Circle circle(r);
Rectangle rectangle(w, h);
Triangle triangle(a, b, c);
cout << "圆的";
printArea(circle);
cout << "矩形的";
printArea(rectangle);
cout << "三角形的";
printArea(triangle);
return 0;
}
```
输入示例:
```
2
3 4
3 4 5
```
输出示例:
```
圆的面积为:12.56
矩形的面积为:12.00
三角形的面积为:6.00
```
阅读全文