写一个基类Point,包含属性x(int类型),y(int类型),基类Point里包含纯虚函数GetArea(),及构造函数和显示函数。再写两个派生类:rect,circle,要求派生类除了有各自的构

写一个基类Point,包含属性x(int类型),y(int类型),基类Point里包含纯虚函数GetArea(),及构造函数和显示函数。再写两个派生类:rect,circle,要求派生类除了有各自的构造函数以外,还有GetArea()函数的具体实现,以及显示函数。 此函数可以求所有矩形对象,圆对象的面积。通过调用函数,传入矩形对象,圆对象体会多态性。

#include<iostream>
using namespace std;
class Point {
   
private:
    int x, y;
public:
    Point(int px, int py)
    {
   
        x = px;
        y = py;
    }
     float getArea(){
    return 0; }  //Point类的getArea()函数,返回值类型为float
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: #include <iostream> using namespace std;//定义点类 class Point { public: Point(int x = 0, int y = 0) { this->x = x; this->y = y; } int getX() { return x; } int getY() { return y; } protected: int x, y; };//定义圆类,以点类为基类派生 class Circle : public Point { public: Circle(int x, int y, int r) : Point(x, y) { this->r = r; } double getArea() { return 3.14 * r * r; } private: int r; };int main() { int x, y, r; cout << "输入圆心坐标和半径:" << endl; cin >> x >> y >> r; Circle c(x, y, r); cout << "圆的面积:" << c.getArea() << endl; return 0; } ### 回答2: 以下是一个用C语言编的程序,其中定义了一个Point类和一个Circle类,并实现了相应的构造函数计算面积函数。 ```c #include <stdio.h> #include <math.h> // 定义Point类 typedef struct { double X; double Y; } Point; // 定义Point类的构造函数 Point createPoint(double x, double y) { Point point; point.X = x; point.Y = y; return point; } // 定义Circle类,继承自Point类 typedef struct { Point center; // 继承Point类的数据成员 double R; // 新增数据成员,圆的半径 } Circle; // 定义Circle类的构造函数 Circle createCircle(double x, double y, double r) { Circle circle; circle.center = createPoint(x, y); circle.R = r; return circle; } // 计算圆的面积 double calculateArea(Circle circle) { return 3.14159 * circle.R * circle.R; } int main() { Circle newCircle = createCircle(1.0, 2.0, 3.5); double area = calculateArea(newCircle); printf("圆的面积为:%lf\n", area); return 0; } ``` 在主程序中,我们创建了一个Circle类的实例newCircle,坐标点为(1.0, 2.0),半径为3.5。然后通过调用calculateArea函数计算出该圆的面积,并将结果输出到屏幕上。 程序输出的结果为: 圆的面积为:38.484076 ### 回答3: 下面是根据要求完成的程序: ```c #include <iostream> using namespace std; class Point { protected: int x, y; public: Point(int x = 0, int y = 0) { this->x = x; this->y = y; } }; class Circle : public Point { private: int r; public: Circle(int x = 0, int y = 0, int r = 0) : Point(x, y) { this->r = r; } double calculateArea() { return 3.14159 * r * r; } }; int main() { Circle circle(1, 2, 5); double area = circle.calculateArea(); cout << "圆面积为:" << area << endl; return 0; } ``` 以上程序中,首先定义了一个基类Point,拥有数据成员x和y表示坐标点。然后通过公有继承的方式,派生出一个Circle类,增加数据成员r表示半径。在Circle类的构造函数中,使用初始化列表将坐标点传递给基类构造函数进行初始化,并初始化半径。还定义了一个计算面积函数calculateArea(),其中使用pi近似值3.14159进行计算。 在主程序中,创建一个Circle对象circle,并传入坐标点(1, 2)和半径5。然后调用calculateArea()函数计算圆的面积,并将结果输出到屏幕上。 输出结果为:圆面积为:78.5398(保留四位小数)。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值