1601 优雅地写出C++代码 5-12
来源:互联网 发布:谁的行书比较好练 知乎 编辑:程序博客网 时间:2024/06/03 18:27
#include <cstdio>#include <cmath>#include <iostream>
//用DB代替double,以后可以直接用double声明#define DB double
//定义PI#define PI 3.1415926using namespace std;
//Shape类,基类class Shape{public: Shape(DB x, DB y) { X=x, Y=y; } ~Shape(){}; void SetX(DB x) { X=x; } void SetY(DB y) { Y=y; } DB GetX(){return X;} DB GetY(){return Y;}private: DB X; DB Y;};class Rectangle:public Shape //由Shape类派生出Rectangle类{public: Rectangle(DB x,DB y,DB a,DB b):Shape(x,y){X2=a,Y2=b;} //参数x,y传递给Shape,另外两个参数只能自己在类私有保存了 ~Rectangle(){} DB GetArea() { return (X2-GetX())*(Y2-GetY()); //X2,Y2可直接读取,但Shape类中的X,Y是私有的,只能用GetX方法 } DB GetPeri() { return 2*(X2-GetX())+2*(Y2-GetY()); } DB GetX2(){return X2;} DB GetY2(){return Y2;}private: DB X2; DB Y2;};class Circle:public Shape{public: Circle(DB x,DB y,DB r):Shape(x,y){R=r;} ~Circle(){} DB GetArea() { return PI*R*R; } DB GetPeri() { return 2*PI*R; }private: DB R;};class Square:public Rectangle{public: Square(DB x,DB y,DB a,DB b):Rectangle(x,y,a,b) { if((a-x)!=(b-y)) { cout<<"Opps!It's not a square TAT"<<endl; //判断是否为正方形 square = false; } else square = true; } ~Square(){} DB GetArea() { if(square){return (GetX()-GetX2())*(GetX()-GetX2());} cout<<"There must be something wrong!"<<endl; } DB GetPeri() { if(square){return 4*abs(GetX()-GetX2());} cout<<"There must be something wrong!"<<endl; }private: bool square;};int main(){ return 0;}
0 0
- 1601 优雅地写出C++代码 5-12
- 写出优雅简明代码的论题集 -- Csharp(C#)篇
- tips:用闭包优雅地写出json排序方法。
- 如何写出优雅的java代码
- Julia: 一行代码可以写出什么优雅?
- 如何写出优雅的java代码
- Android开发如何写出优雅的代码
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[1]
- 写出优雅简明代码的论题集 -- Csharp(C#)篇[2]
- 代码之美 - 如何写出优雅的PHP代码
- 代码之美 - 如何写出优雅的PHP代码
- 代码之美 - 如何写出优雅的PHP代码
- 代码之美 - 如何写出优雅的PHP代码
- 教你写出优雅的代码(一)
- 教你写出优雅的代码(二)
- 教你写出优雅的代码(三)
- 教你写出优雅的代码(四)
- 强行在C# Winform中渲染Cocos2d-x 3.6
- 为啥智能交通还没来???
- 棋盘问题
- 如何使用Logger(比较突出的日志显示)
- web storage
- 1601 优雅地写出C++代码 5-12
- “ipconfig不是内部命令或外部命令”解决方法
- 面向对象
- 并查集
- QtCreator 调试器配置
- Mysql 数据存储+索引划分
- qt之网络ftp(下)
- 亚洲诚信携手七牛云将HTTPS安全防御进行到底
- 汇编程序:找出最小值