sdut 5-1 继承与派生

本文将通过练习掌握继承与派生的概念,着重讲解派生类的定义和使用方法。具体包括定义一个基类Point,增加矩形类Rectangle作为其派生类,并实现矩形类的构造函数、成员函数等关键功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5-1 继承与派生

Time Limit: 1000MS Memory limit: 65536K

题目描述

通过本题目的练习可以掌握继承与派生的概念,派生类的定义和使用方法,其中派生类构造函数的定义是重点。

要求定义一个基类Point,它有两个私有的float型数据成员X,Y;一个构造函数用于对数据成员初始化;有一个成员函数void Move(float xOff, float yOff)实现分别对X,Y值的改变,其中参数xOffyOff分别代表偏移量。另外两个成员函数GetX() GetY()分别返回XY的值。

Rectangle类是基类Point的公有派生类。它增加了两个float型的私有数据成员W,H; 增加了两个成员函数float GetH() float GetW()分别返回WH的值;并定义了自己的构造函数,实现对各个数据成员的初始化。

编写主函数main()根据以下的输入输出提示,完成整个程序。

输入

 

6float型的数据,分别代表矩形的横坐标X、纵坐标Y、宽度W,高度H、横向偏移量的值、纵向偏移量的值;每个数据之间用一个空格间隔

输出

 

输出数据共有4个,每个数据之间用一个空格间隔。分别代表偏移以后的矩形的横坐标X、纵坐标Y、宽度W,高度H的值

示例输入

5 6 2 3 1 2

示例输出

6 8 2 3

提示

 输入 -5 -6 -2 -3 2 10

输出 -3 4 0 0

来源


#include <iostream>

using namespace std;

class Point //声明Point类
{
private :
    float x, y;

public :
    Point (float x1=0, float y1=0): x(x1), y(y1) {};//定义构造函数
    void Move(float xoff, float yoff);//声明move函数
    float Getx() const {return x;}//定义成员函数Getx
    float Gety() const{return y;}//定义成员函数Gety
};

void Point :: Move(float xoff, float yoff)//定义Move 函数
{
    x = x + xoff;
    y = y + yoff;
}

class Rectangle : public Point//定义Rectangle类
{
private :
    float w, h;

public :
    Rectangle(float x1, float y1, float w1, float h);//声明Rectangle函数
    float Getw() const{return w;}
    float Geth()  const{return h;}
};

Rectangle :: Rectangle(float x1, float y1, float w1, float h1) : Point(x1, y1)//定义Rectangle函数
{
    w=w1 >= 0 ? w1:0 ;
    h=h1 >= 0 ? h1:0 ;
}

int main()//主函数
{
    float x, y, w, h, xoff, yoff;
    cin>>x>>y>>w>>h>>xoff>>yoff;
    Point p1(x, y);
    Rectangle r1(x, y, w, h);
    p1.Move(xoff, yoff);
    cout <<p1.Getx()<<" "<<p1.Gety()<<" "<<r1.Getw()<<" "<<r1.Geth()<< endl;
    return 0;
}




### Python实现复数运算 以下是基于Python的复数运算实现代码,利用Python内置的`complex`型来简化复数的操作[^1]: ```python def complex_operations(): results = [] x_real, x_imaginary = map(int, input().split()) x = complex(x_real, x_imaginary) while True: line = input() if line.strip() == "0 0 0": break y_real, y_imaginary, op = line.split() y = complex(int(y_real), int(y_imaginary)) if int(op) == 1: # 加法 result = x + y elif int(op) == 2: # 减法 result = x - y elif int(op) == 3: # 乘法 result = x * y else: continue results.append(result) for res in results: print(f"{int(res.real)} {int(res.imag)}" .replace("j", "").replace("+", "")) if __name__ == "__main__": complex_operations() ``` 上述代码实现了读取输入并执行复数加减乘操作的功能。通过循环不断接收输入直到遇到终止条件 `0 0 0`。 --- ### Java实现复数运算 对于Java中的复数运算,则需要手动定义一个`Complex`来进行封装[^2]。下面是一个完整的示例代码: ```java class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex add(Complex other) { return new Complex(this.real + other.real, this.imaginary + other.imaginary); } public Complex subtract(Complex other) { return new Complex(this.real - other.real, this.imaginary - other.imaginary); } public Complex multiply(Complex other) { double r = this.real * other.real - this.imaginary * other.imaginary; double i = this.real * other.imaginary + this.imaginary * other.real; return new Complex(r, i); } @Override public String toString() { return (real != 0 ? real + " " : "") + (imaginary >= 0 && real != 0 ? "+" : "") + (imaginary != 0 ? Math.abs(imaginary) + "i" : ""); } } public class Main { public static void main(String[] args) { java.util.Scanner scanner = new Scanner(System.in); // 初始化 X System.out.println("Enter the first complex number:"); double xr = scanner.nextDouble(); double xi = scanner.nextDouble(); Complex x = new Complex(xr, xi); List<Complex> results = new ArrayList<>(); while (true) { System.out.println("Enter Y and operation code or '0 0 0' to stop:"); double yr = scanner.nextDouble(); double yi = scanner.nextDouble(); int opCode = scanner.nextInt(); if (yr == 0 && yi == 0 && opCode == 0) { break; } Complex y = new Complex(yr, yi); switch (opCode) { case 1 -> results.add(x.add(y)); case 2 -> results.add(x.subtract(y)); case 3 -> results.add(x.multiply(y)); default -> {} } } for (Complex c : results) { System.out.println(c.toString()); } } } ``` 此代码创建了一个名为`Complex`的用于处理复数的相关计算,并提供了加法、减法以及乘法的方法。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值