若渴9

C++学习札记(2)

重载构造函数

#include <iostream>

using namespace std;

class rectangle

{

public:

     rectangle(){cout<<"构造一个长方形a!\n";}                                          // 无参数的构造函数

     rectangle(int l,int v){length=l;width=w;cout<<"长方形b的面积为:“

     rectangle(int l,int v,int h){length=l,width=w;height=h;cout<<"长方形的..."}       //  将 l、v、h赋值给私有变量 length、width、height

private:

     int length;

     int width;

     int height;

}

int main()

{

   rectangle a;

   rectangle b(3,4);

   rectangle c(3,4,10);

   return 0;

}

以上说明了构造函数,以及通过公有成员 调用 私有成员的方法。

 

栈和堆的不同:

1.栈由系统自动分配内存,堆要程序员进行申请;

2.栈中空间比较小,只有2M,对的上限是由系统中有效的虚拟内存来定的。因此获得的空间比较大。

 

posted on 2015-10-19 21:57  若渴9  阅读(109)  评论(0编辑  收藏  举报

导航