YTU-OJ-Problem I: 熟悉题型——类设计( 矩形类定义【C++】)
2015-07-21 09:12
537 查看
Problem I: 熟悉题型——类设计( 矩形类定义【C++】)
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 145 Solved: 94
[Submit][Status][Web
Board]
Description
定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数、输入坐标的函数,以及计算并输出矩形面积的函数。要求使用提示中给出的测试函数并不得改动。Input
四个数,分别表示矩形左下角和右上角顶点的坐标,如输入3.7 0.4 6.5 4.9,代表左下角坐标为(3.7, 0.4),右上角坐标为(6.5, 4.9)。请根据给出的主函数,完成矩形类设计。
Output
输出一共有3行(请参考提示(hint)中的main函数):第一行:由输入的坐标确定的矩形对象p1的面积
第二行:由对象复制得到的矩形对象p2的面积
第三行:直接初始化得到的矩形对象p3的面积
Sample Input
3.7 0.4 6.5 4.9
Sample Output
12.6 12.6 10
HINT
int main(){
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
#include <iostream> #include <cmath> using namespace std; class Rectangle { private: double x1,y1,x2,y2; public: Rectangle() { x1=x2=y1=y2=0; } Rectangle(double x,double y,double xx,double yy): x1(x),y1(y),x2(xx),y2(yy) {} void input() { cin>>x1>>y1>>x2>>y2; } void output() { cout<<fabs(x1-x2)*fabs(y1-y2)<<endl; } }; int main() { Rectangle p1; p1.input(); p1.output(); Rectangle p2(p1); p2.output(); Rectangle p3(1,1,6,3); p3.output(); return 0; }
相关文章推荐
- 线性链表的创建 C语言
- PAT (Basic Level) Practise (中文)1022. D进制的A+B (20) C语言
- YTU-OJ-Problem D: C++习题 虚函数-计算图形面积
- YTU-OJ-Problem C: C++习题 抽象基类
- [leetcode-236]Lowest Common Ancestor of a Binary Tree(C语言)236
- 2015.7.21 c语言程序设计 第二堂
- C++基本变量类型
- C++ 对C语言的增强
- 如何学习C语言编程
- C++哈希表
- 算法导论 第二十五章:有向图的传递闭包
- 算法导论 第二十五章:每对顶点间的最短路径
- C++中模板类使用友元模板函数
- 9秒学院学C++11新特性
- C++中使用空对象指针调用成员函数
- C++中const用法小结
- C++知识点积累(1)
- Dividing
- 我的C++笔记(函数部分)
- 黑马程序员------C语言基础 函数