// C++ 第三章:类的基础部分
#include<iostream>
using namespace std;
class Rect
{
private:
float width;
float height;
public:
// 构造函数
// 默认构造函数
Rect()
{
width=height=0;
}
// 带参构造函数
Rect(float w,float h)
{
width=w;
height=h;
}
float getWidth()
{
return width;
}
void setHeight(float h);
// 析构函数
~Rect()
{
cout<<"调用析构函数\n";
}
};
void Rect::setHeight(float h)
{
height=h;
}
int main(void)
{
Rect rec1(3.0,4.0),*p;
p=new Rect; // 调用默认构造函数
cout<<rec1.getWidth()<<endl;
p->setHeight(6.0);
cout<<p->getWidth()<<endl;
delete p; // 调用析构函数
// 主函数结束时调用析构函数
return 0;
}