标题:对象的赋值
取消只看楼主
hmsabc
Rank: 2
来 自:贵州省兴义市
等 级:论坛游民
帖 子:97
专家分:19
注 册:2010-8-2
结帖率:100%
 问题点数:0 回复次数:0 
对象的赋值
程序代码:
//对象的赋值--谭浩强《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;
}

/*
一、对象的赋值只对其中的数据成员赋值。而不对成员函数赋值;
二、类的数据成员中不能包括动态分配的数据,否则在赋值时出现严重后果。
*/
搜索更多相关主题的帖子: 赋值 对象 
2010-08-14 08:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-315649-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.163329 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved