某同学的 C++ 作业 - 花式构造函数

构造函数

题目

构造函数

  1. 默认构造函数
  2. 用构造函数实现成员和初始化
  3. 带参数的构造函数
  4. 用参数初始化表对Data成员初始化
  5. 带默认参数的构造函数
  6. 复制构造函数

作业:

以Box类为例,分别使用构造函数的2.3.4.5.6实现长方体对象的初始化,及编写主函数求体积。

Answer

2

#include<iostream>
#include<cstdio>
using namespace std;
class Box
{
private:
	double length,width,high;
public:
	Box()
	{
		length=10.0;
		width=10.0;
		high=10.0;
	}//使用构造函数实现成员初始化
	void vol();
	void vol(double l,double w,double h);
	void print();
};
void Box::vol()
{
	cout<<"vol="<<length*width*high<<endl;
}//使用构造函数实现
void Box::print()
{
	cout<<"length="<<length<<endl;
	cout<<"width="<<width<<endl;
	cout<<"high="<<high<<endl;
}//输出
int main()
{
	Box a;
	a.print();
	a.vol();
	return 0;
}

3

#include<iostream>
#include<cstdio>
using namespace std;
class Box
{
	private:
		double length,width,high;
	public:
		Box(double l,double w,double h)
		{
			length=l;width=w;high=h;
		}//带参数的构造函数
		void vol();
		void vol(double l,double w,double h);
		void print();
};
void Box::vol(double l,double w,double h)
{
	length=l;
	width=w;
	high=h;
	cout<<"vol="<<length*width*high<<endl;
}//带参数的构造函数
void Box::print()
{
	cout<<"length="<<length<<endl;
	cout<<"width="<<width<<endl;
	cout<<"high="<<high<<endl;
}//输出
int main()
{
	Box b(1.0,2.0,3.0);
	b.print();
	b.vol(1.0,2.0,3.0);
	return 0;
}

4

#include<iostream>
#include<cstdio>
using namespace std;
class Box
{
private:
	double length,width,high;
public:
    Box():length(5.0),width(6.0),high(7.0)
    {}
	//使用参数初始化表数实现成员初始化
	void vol();
	void vol(double l,double w,double h);
	void print();
};
void Box::vol()
{
	cout<<"vol="<<length*width*high<<endl;
}//使用构造函数实现
void Box::print()
{
	cout<<"length="<<length<<endl;
	cout<<"width="<<width<<endl;
	cout<<"high="<<high<<endl;
}//输出
int main()
{
	Box a;
	a.print();
	a.vol();
	return 0;
}

5

#include<iostream>
#include<cstdio>
using namespace std;
class Box
{
private:
	double length,width,high;
public:
	Box(double l=10.0,double w=10.0,double h=10.0)
	{
		length=l;
		width=w;
		high=h;
	}//使用构造函数实现成员初始化
	void vol();
	void vol(double l,double w,double h);
	void print();
};
void Box::vol()
{
	cout<<"vol="<<length*width*high<<endl;
}//使用构造函数实现
void Box::print()
{
	cout<<"length="<<length<<endl;
	cout<<"width="<<width<<endl;
	cout<<"high="<<high<<endl;
}//输出
int main()
{
	Box a;
	a.print();
	a.vol();
	return 0;
}

6

#include<iostream>
#include<cstdio>
using namespace std;
class Box
{
	private:
		double length,width,high;
	public:
		Box(double l,double w,double h)
		{
			length=l;width=w;high=h;
		}//带参数的构造函数
		void vol();
		void vol(double l,double w,double h);
		void print();
};
void Box::vol()
{
	cout<<"vol="<<length*width*high<<endl;
}//使用构造函数实现
void Box::vol(double l,double w,double h)
{
	length=l;
	width=w;
	high=h;
	cout<<"vol="<<length*width*high<<endl;
}//带参数的构造函数
void Box::print()
{
	cout<<"length="<<length<<endl;
	cout<<"width="<<width<<endl;
	cout<<"high="<<high<<endl;
}//输出
int main()
{
	Box b(1.0,2.0,3.0);
	Box c(b);//拷贝构造函数
	c.print();
	c.vol();
	return 0;
}
posted @ 2017-11-04 15:32  VanLion  阅读(178)  评论(0编辑  收藏  举报