059、060、061-C++
来源:互联网 发布:w7内外网转换软件 编辑:程序博客网 时间:2024/06/11 11:53
059
#include <iostream> using namespace std; class Line{ public: int getLength( void ); Line( int len ); // 简单的构造函数 Line( const Line &obj); // 拷贝构造函数 ~Line(); // 析构函数 private: int *ptr;}; // 成员函数定义,包括构造函数Line::Line(int len){ cout << "调用构造函数" << endl; // 为指针分配内存 ptr = new int; *ptr = len;} Line::Line(const Line &obj){ cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl; ptr = new int; *ptr = *obj.ptr; // 拷贝值} Line::~Line(void){ cout << "释放内存" << endl; delete ptr;}int Line::getLength( void ){ return *ptr;} void display(Line obj){ cout << "line 大小 : " << obj.getLength() <<endl;} // 程序的主函数int main( ){ Line line1(10); Line line2 = line1; // 这里也调用了拷贝构造函数 display(line1); display(line2); return 0;}
060
#include <iostream> using namespace std; class Box{ double width;public: friend void printWidth( Box box ); void setWidth( double wid );};// 成员函数定义void Box::setWidth( double wid ){ width = wid;}// 请注意:printWidth() 不是任何类的成员函数void printWidth( Box box ){ /* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */ cout << "Width of box : " << box.width <<endl;} // 程序的主函数int main( ){ Box box; // 使用成员函数设置宽度 box.setWidth(10.0); // 使用友元函数输出宽度 printWidth( box ); return 0;}
061
#include <iostream> using namespace std;inline int Max(int x, int y){ return (x > y)? x : y;}// 程序的主函数int main( ){ cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0;}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- Netty异步IO和回调函数
- Android---------HttpURLConnection的 get 请求和 post 请求(更容易理解)
- HDU 1532 Drainage Ditches——最大流EK算法
- phpcms v9网站搬家更换域名的方法
- VS调试网站时分析器错误
- 059、060、061-C++
- mac效率工具之moom
- [Leetcode] 198. House Robber 解题报告
- OpenCV学习笔记(23)findContours与drawContours
- java中通过反射获取成员方法并使用
- Ubuntu 14.04 LTS 安装 Virtualenvwrapper
- 使用环信出现的问题
- 单项链表的实现
- HashMap源代码分析