Box box2(box1);
类名 对象2(对象1);
//The copy constructor definition.
Box::Box(const Box& b)
{
height=b.height; width=b.width; length=b.length;
}
Box box2(box1);
Box box2=box1; //用box1初始化box2
类名 对象名1 = 对象名2;
Box box2=box1,box3=box2;
int a=4,b=a;
int main( )
{
Box box1(15,30,25); //定义box1
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2=box1,box3=box2; //按box1来复制box2,box3
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
cout<<"The volume of box3 is "<<box3.volume( )<<endl;
}
Box box1(12,15,16); //实参为整数,调用普通构造函数 Box box2(box1); //实参是对象名,调用复制构造函数
void fun(Box b) //形参是类的对象
{ }
int main( )
{
Box box1(12,15,18);
fun(box1); //实参是类的对象,调用函数时将复制一个新对象b
return 0;
}
Box f( ) //函数f的类型为Box类类型
{
Box box1(12,15,18);
return box1; //返回值是Box类的对象
}
int main( )
{
Box box2; //定义Box类的对象box2
box2=f( ); //调用f函数,返回Box类的临时对象,并将它赋值给box2
}
对象名1 = 对象名2;
Student stud1,stud2; //定义两个同类的对象 stud2=stud1; //将stud1赋给stud2
#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 len)
{
height=h;
width=w;
length=len;
}
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;
box2=box1; //将box1的值赋给box2
cout<<"The volume of box2 is "<<box2.volume( )<<endl; return 0;
}
The volume of box1 is 11250 The volume of box2 is 11250
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有