5-1 继承与派生
来源:互联网 发布:python socket 长连接 编辑:程序博客网 时间:2024/06/01 10:36
5-1 继承与派生
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
通过本题目的练习可以掌握继承与派生的概念,派生类的定义和使用方法,其中派生类构造函数的定义是重点。
要求定义一个基类Point,它有两个私有的float型数据成员X,Y;一个构造函数用于对数据成员初始化;有一个成员函数void Move(float xOff, float yOff)实现分别对X,Y值的改变,其中参数xOff和yOff分别代表偏移量。另外两个成员函数GetX() 、GetY()分别返回X和Y的值。
Rectangle类是基类Point的公有派生类。它增加了两个float型的私有数据成员W,H; 增加了两个成员函数float GetH() 、float GetW()分别返回W和H的值;并定义了自己的构造函数,实现对各个数据成员的初始化。
编写主函数main()根据以下的输入输出提示,完成整个程序。
Input
6个float型的数据,分别代表矩形的横坐标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
黄晶晶
#include <iostream>#include <string>#include <cstring>#include <stdio.h>using namespace std;class Point{public: Point(float a, float b) :x(a), y(b) {} float getX() { return x; } float getY() { return y; } void move(float offx, float offy) { x += offx; y += offy; }private: float x, y;};class Rectganle:public Point{public: Rectganle(float a, float b, float c, float d) :Point(a, b), w(c), h(d) { if (w < 0) w = 0; if (h < 0) h = 0; } float getW() { return w; } float getH() { return h; }private: float w, h;};int main(){ float a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; Rectganle prea(a, b, c, d); prea.move(e, f); cout << prea.getX() << " " << prea.getY() << " " << prea.getW() << " " << prea.getH() << " " << endl; return 0;}
阅读全文
0 0
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 继承与派生1
- 继承与派生1
- [2678]5-1 继承与派生
- SDUTOJ 2678 5-1 继承与派生
- SDUT 2678 5-1 继承与派生
- Use Prometheus+Grafana to monitor the Internet backbone connectivity quality
- 什么是JavaScript闭包?
- Qt之在运行时加载共享库
- linux 下 SVN 常用命令详解
- QT tcpsocket 在断开后重新连接程序直接崩溃
- 5-1 继承与派生
- 人工智能/深度学习极速入门课程大纲
- 基于安卓平台的数字识别
- 读书06《简约至上》
- Spring Boot Async异步执行
- 如何在十分钟内插入1亿条记录到Oracle数据库?
- sqlserver delete 删除两个表的差集
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
- android 动画