注册 登录
编程论坛 C++教室

对象的赋值

hmsabc 发布于 2010-08-14 08:40, 475 次点击
程序代码:
//对象的赋值--谭浩强《C++程序设计》第一版 例9.9 P292页
#include <iostream>
using namespace std;

class Box
{
public:
    Box( int=10,int=10,int=10);                 //声明有默认参数的构造函数
    int volume( );
private:
    int height;
    int width;
    int length;
};

Box::Box(int h,int w,int l)
{
    height = h;
    width = w;
    length = l;
}

int Box::volume( )
{ return( height * width * length);}           //返回体积的值

int main( )
{
    Box box1(15,30,25),box2;                                            //定义两个对象 box1 和 box2
    cout << "The volume of box1 is " << box1.volume( ) << endl;         //输出 box1 的体积
    box2 = box1;                                                        //将对象 box1 的值赋给对象 box2
    cout << "The volume of box2 is " << box2.volume( ) << endl;         //输出 box2 的体积
    system("pause");
    return 0;
}

/*
一、对象的赋值只对其中的数据成员赋值。而不对成员函数赋值;
二、类的数据成员中不能包括动态分配的数据,否则在赋值时出现严重后果。
*/
0 回复
1