062、063、064-C++
来源:互联网 发布:ign 知乎 编辑:程序博客网 时间:2024/04/29 05:36
062
#include <iostream> using namespace std;class Box{ public: // 构造函数定义 Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } int compare(Box box) { return this->Volume() > box.Volume(); } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box};int main(void){ Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 if(Box1.compare(Box2)) { cout << "Box2 is smaller than Box1" <<endl; } else { cout << "Box2 is equal to or larger than Box1" <<endl; } return 0;}
063
#include <iostream> using namespace std;class Box{ public: // 构造函数定义 Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box};int main(void){ Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 Box *ptrBox; // Declare pointer to a class. // 保存第一个对象的地址 ptrBox = &Box1; // 现在尝试使用成员访问运算符来访问成员 cout << "Volume of Box1: " << ptrBox->Volume() << endl; // 保存第二个对象的地址 ptrBox = &Box2; // 现在尝试使用成员访问运算符来访问成员 cout << "Volume of Box2: " << ptrBox->Volume() << endl; return 0;}
064
#include <iostream> using namespace std;class Box{ public: static int objectCount; // 构造函数定义 Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // 每次创建对象时增加 1 objectCount++; } double Volume() { return length * breadth * height; } private: double length; // 长度 double breadth; // 宽度 double height; // 高度};// 初始化类 Box 的静态成员int Box::objectCount = 0;int main(void){ Box Box1(3.3, 1.2, 1.5); // 声明 box1 Box Box2(8.5, 6.0, 2.0); // 声明 box2 // 输出对象的总数 cout << "Total objects: " << Box::objectCount << endl; return 0;}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- 关于归档日志空间被占满而导致数据库down掉的问题
- 谈谈HTTP连接管理
- 在jsp页面中使用bootstrap制作
- 黑客入门视频教程(共57个)全实战过程
- post创建一个空索引
- 062、063、064-C++
- 如何评论浏览器最新的 WebAssembly 字节码技术?
- I2S,PCM,IOM-2,I2C,SPI,UART,GPIO
- $.ajax()方法参数详细解释
- JavaBean的概述
- Android Framework:Binder(4)-Native Service的注册
- xampp apache端口占用关闭iis
- 复制a.txt到b.txt,并实现数据排序
- 最小二乘法