SDUT-2678-->5-1 继承与派生

5-1 继承与派生

Time Limit: 1000MS Memory Limit: 65536KB  
Problem Description

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

要求定义一个基类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()根据以下的输入输出提示,完成整个程序。

Input
 

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

Output
 

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

Example Input
5 6 2 3 1 2
Example Output
6 8 2 3
Hint
  输入 -5 -6 -2 -3 2 10

输出 -3 4 0 0

Author
 黄晶晶

        !!课本P266的样例看明白了就OK了
参考代码:
#include <iostream>
using namespace std;
class Point
{
private:
    double x,y;
public:
    Point(double a, double b)//定义基类的构造函数
    {
        this->x = a;
        this->y = b;
    };
    void Move(double xoff,double yoff);
    double Getx(){return x;};
    double Gety(){return y;};
};
void Point::Move(double xoff,double yoff)
{
    x=x+xoff;
    y=y+yoff;
}
class Rectangle:public Point//公有继承
{
private:
    double w,h;
public:
    Rectangle(double a, double b, double c, double d):Point(a,b)//←基类的构造函数要写在这个位置
    {
        //而不是写在这儿↓
        //Point(a,b);
        this->w = c;
        this->h = d;
    }
    double Geth()
    {
        if(h<=0)
            return 0;
        return h;
    };
    double Getw()
    {
        if(w<=0)
            return 0;
        return w;
    };
};
int main()
{
    double a,b,c,d;
    cin>>a>>b>>c>>d;
    Rectangle r(a,b,c,d);
    double movex,movey;
    cin>>movex>>movey;
    r.Move(movex,movey);
    cout<<r.Getx()<<' '<<r.Gety()<<' '<<r.Getw()<<' '<<r.Geth()<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单的学生集合的示例代码,您可以参考: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; class Student { private: string name; int age; double score; public: Student(string n, int a, double s) : name(n), age(a), score(s) {} string getName() const { return name; } int getAge() const { return age; } double getScore() const { return score; } }; class StudentCollection { private: vector<Student> students; public: void addStudent(Student s) { students.push_back(s); } void removeStudent(Student s) { for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) { if (it->getName() == s.getName()) { students.erase(it); break; } } } void printStudents() const { for (vector<Student>::const_iterator it = students.begin(); it != students.end(); ++it) { cout << "Name: " << it->getName() << ", Age: " << it->getAge() << ", Score: " << it->getScore() << endl; } } }; int main() { StudentCollection sc; sc.addStudent(Student("Tom", 18, 90)); sc.addStudent(Student("Jerry", 20, 80)); sc.addStudent(Student("Alice", 19, 85)); sc.printStudents(); sc.removeStudent(Student("Jerry", 20, 80)); sc.printStudents(); return 0; } ``` 这个示例代码中,定义了一个 `Student` 类和一个 `StudentCollection` 类。其中 `Student` 类表示一个学生,包括学生姓名、年龄、分数等信息。`StudentCollection` 类表示一个学生集合,包括添加学生、删除学生、打印学生信息等操作。在 `main` 函数中,演示了如何使用 `StudentCollection` 类来管理学生集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值