#include <iostream>
using namespace std;
class Box
{
public :
Box(int h=10,int w=10,int len=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; //没有给实参
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15); //只给定一个实参
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
Box box3(15,30); //只给定2个实参
cout<<"The volume of box3 is "<<box3.volume( )<<endl;
Box box4(15,30,20); //给定3个实参
cout<<"The volume of box4 is "<<box4.volume( )<<endl;
return 0;
}
The volume of box1 is 1000 The volume of box2 is 1500 The volume of box3 is 4500 The volume of box4 is 9000
Box::Box(int h,int w,int len):height(h),width(w),length(len){ }
#include <iostream>
using namespace std;
class Box
{
public : Box( ); //声明一个无参的构造函数
//声明一个有参的构造函数,用参数的初始化表对数据成员初始化
Box(int h,int w,int len):height(h),width(w),length(len){ }
int volume( );
private :
int height;
int width;
int length;
};
Box::Box( ) //定义一个无参的构造函数
{
height=10; width=10; length=10;
}
int Box::volume( ){
return (height*width*length);
}
int main( )
{
Box box1; //建立对象box1,不指定实参
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15,30,25); //建立对象box2,指定3个实参
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return 0;
}
Box::Box(int h); //有1个参数的构造函数 Box::Box(int h,int w); //有两个参数的构造函数
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有