c++例题 构造函数(一)
来源:互联网 发布:大连育知同创学费 编辑:程序博客网 时间:2024/06/05 11:43
题目来自:http://blog.csdn.net/sxhelijian/article/details/8723847
【项目1拓展(选做)】请自行设计一个矩形类,可以计算矩形的面积、周长、对象线,判断是否是正方形。请用上类似的构造函数,自己设计main()函数,对设计的类进行测试。
#include <iostream>#include <cmath>using namespace std;class Rectangle{private:double length;double width;public:Rectangle():length(1),width(1){}Rectangle(double len,double wid):length(len),width(wid){}// Rectangle(double len=1,double wid =1):length(len),width(wid){}double area(void);double perimeter(void){ return 2*(length+width); }double diagonal(void) { return sqrt(length*length+width*width); }bool square_or_not(void) { return length==width?true:false; } void show_message(void);};//Rectangle::Rectangle(double len,double wid){length = len;width = wid;}double Rectangle::area(void){return length*width;}void Rectangle::show_message(void){cout << "矩形的长宽分别为: " << length << '\t' << width <<endl;cout << "周长: " << perimeter() << "面积: " << area() << "对角线长度: "<< diagonal() << endl;cout << "是否为正方形? " << square_or_not() << endl;}int main(){Rectangle rect1;rect1.show_message();Rectangle rect2(3,4);rect2.show_message();return 0;}
- c++例题 构造函数(一)
- c++例题 构造函数(二)
- c++例题 构造函数(三)
- C语言经典例题(一)
- C经典例题一
- C++课本例题(派生类的构造函数)
- C#:一个增强的NetworkStream(一)构造函数
- C#:一个增强的TcpListener(一)构造函数
- C++(一)拷贝构造函数栈溢出的原因
- C#:一个增强的TcpListener(一)构造函数
- 派生类的构造函数例题
- C++例题分析1 构造函数初始化
- C++例题分析2,复制构造函数
- 拷贝构造函数(一)
- Map(一)构造函数
- 构造函数之(一)
- C++ 构造函数(一)
- c语言例题之函数
- JBoss7.1配置Oracle11g数据源,详细图解
- 『H.264』x264参数中文详解(X264 Settings)
- 创建型-Builder
- X264和JM的区别
- 【黑马程序员】身份证验证
- c++例题 构造函数(一)
- 挺好的话
- [Android] Ubuntu中下载android源码
- UITableView中设置section内行数以及配置cell的方法
- C++中宏的使用技巧
- 获得js文件引用的参数
- Generic Programming Techniques
- html table复杂表头、列固定
- 手机毒霸去广告功能分析之一:总体分析