首先定义一个类point 然后定义类point的派生类cline 两个类都有表示坐标的数据成员 要求有对应的设置和获取成员函数的成员函数

首先定义一个类point 然后定义类point的派生类cline 两个类都有表示坐标的数据成员 要求有对应的设置和获取成员函数的成员函数 用vc++

如果对你有启发和帮助,请采纳! 答案参考Chatgpt解答

以下是使用VC++实现的Point类和CLine类的示例代码,包括数据成员、设置和获取成员函数。

#include <iostream>

// Point类的定义
class Point
{
private:
    double x;
    double y;

public:
    // 构造函数
    Point(double x = 0, double y = 0)
    {
        this->x = x;
        this->y = y;
    }

    // 设置x坐标
    void SetX(double x)
    {
        this->x = x;
    }

    // 获取x坐标
    double GetX() const
    {
        return x;
    }

    // 设置y坐标
    void SetY(double y)
    {
        this->y = y;
    }

    // 获取y坐标
    double GetY() const
    {
        return y;
    }
};

// CLine类继承自Point类
class CLine : public Point
{
private:
    Point start;
    Point end;

public:
    // 构造函数
    CLine(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0) : start(x1, y1), end(x2, y2)
    {
    }

    // 设置起始点坐标
    void SetStart(double x, double y)
    {
        start.SetX(x);
        start.SetY(y);
    }

    // 获取起始点坐标
    Point GetStart() const
    {
        return start;
    }

    // 设置结束点坐标
    void SetEnd(double x, double y)
    {
        end.SetX(x);
        end.SetY(y);
    }

    // 获取结束点坐标
    Point GetEnd() const
    {
        return end;
    }
};

int main()
{
    // 创建Point对象并设置、获取坐标
    Point p;
    p.SetX(3.5);
    p.SetY(2.7);
    std::cout << "Point coordinates: (" << p.GetX() << ", " << p.GetY() << ")" << std::endl;

    // 创建CLine对象并设置、获取起始点和结束点坐标
    CLine line;
    line.SetStart(1.2, 2.3);
    line.SetEnd(4.5, 6.7);
    std::cout << "Start point coordinates: (" << line.GetStart().GetX() << ", " << line.GetStart().GetY() << ")" << std::endl;
    std::cout << "End point coordinates: (" << line.GetEnd().GetX() << ", " << line.GetEnd().GetY() << ")" << std::endl;

    return 0;
}

在此示例中,Point类具有xy两个私有数据成员,并提供了设置和获取这些成员的成员函数。CLine类从Point类派生,具有额外的startend数据成员,表示线段的起始点和结束点,同样提供了设置和获取这些成员的成员函数。

main函数中,我们创建了一个Point对象和一个CLine对象,并演示了如何使用设置和获取成员函数来操作和访问数据成员。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

#include <iostream>
using namespace std;
// 基类 point
class point {
protected:
    double x, y; // 表示坐标的数据成员
public:
    point(double a = 0, double b = 0) {
        x = a;
        y = b;
    }
    void setX(double a) {
        x = a;
    }
    void setY(double b) {
        y = b;
    }
    double getX() {
        return x;
    }
    double getY() {
        return y;
    }
};
// 派生类 cline
class cline : public point {
private:
    double x1, y1; // 表示坐标的数据成员
public:
    cline(double a = 0, double b = 0, double c = 0, double d = 0) : point(a, b) {
        x1 = c;
        y1 = d;
    }
    void setX1(double a) {
        x1 = a;
    }
    void setY1(double b) {
        y1 = b;
    }
    double getX1() {
        return x1;
    }
    double getY1() {
        return y1;
    }
};
int main() {
    // 创建 cline 对象
    cline line(1, 2, 3, 4);
    // 输出初始值
    cout << line.getX() << " " << line.getY() << " " << line.getX1() << " " << line.getY1() << endl;
    // 设置新值
    line.setX(5);
    line.setY(6);
    line.setX1(7);
    line.setY1(8);
    // 输出新值
    cout << line.getX() << " " << line.getY() << " " << line.getX1() << " " << line.getY1() << endl;
    return 0;
}