第五周任务一 构造函数 三角形

 

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: class Triangle                            
* 作    者:      姜雅明                       
* 完成日期:     2012    年   03    月    19    日
* 版 本 号:       1.0  

* 对任务及求解方法的描述部分
* 输入描述:三角形的三边
* 问题描述: 计算周长和面积
* 程序输出: 三角形的周长和面积
* 程序头部的注释结束
*/

(1)使用带参数构造函数,即Triangle(float x, float y, float z),三边长在调用时由实参直接给出;

#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{public:
float perimeter(void);//计算三角形的周长
float area(void);//计算并返回三角形的面积
void showMessage();
Triangle(float x, float y, float z);
private:
	float a,b,c; //三边为私有成员数据
};

void Triangle:: showMessage()
{
	cout<<"三角形的三边长分别为:"<<a<<"   "<<b<<"   "<<c<<endl;
	cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl<<endl;
}

float Triangle:: perimeter(void)
{
	return a+b+c;
}

float Triangle:: area(void)
{
	float p, i;
	p = (a + b + c) / 2;
    i = sqrt(p * (p - a) * (p - b) * (p - c));
	return i;
}

Triangle:: Triangle(float x, float y, float z): a(x), b(y), c(z){}

void main(void)
{
	Triangle Tri2(7,8,9);	//定义三角形类的一个实例(对象)
	Tri2.showMessage();
	
}


(2)设计默认构造函数,即不指定参数时,默认各边长为1;

#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{public:
float perimeter(void);//计算三角形的周长
float area(void);//计算并返回三角形的面积
void showMessage();
Triangle();
private:
	float a,b,c; //三边为私有成员数据
};

void Triangle:: showMessage()
{
	cout<<"三角形的三边长分别为:"<<a<<"   "<<b<<"   "<<c<<endl;
	cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl<<endl;
}

float Triangle:: perimeter(void)
{
	return a+b+c;
}

float Triangle:: area(void)
{
	float p, i;
	p = (a + b + c) / 2;
    i = sqrt(p * (p - a) * (p - b) * (p - c));
	return i;
}

Triangle:: Triangle()
{
	a = 1;
	b = 1;
	c = 1;
}

void main(void)
{
	Triangle Tri1;	//定义三角形类的一个实例(对象)
	Tri1.showMessage();	
}

(3)使用默认参数的构造函数,不给定实参时,默认边长为1;

#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{public:
	float perimeter(void);//计算三角形的周长
	float area(void);//计算并返回三角形的面积
	void showMessage();
    Triangle(float x, float y, float z = 1);
private:
	float a,b,c; //三边为私有成员数据
};

void Triangle:: showMessage()
{
	cout<<"三角形的三边长分别为:"<<a<<"   "<<b<<"   "<<c<<endl;
 	cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl<<endl;
}

float Triangle:: perimeter(void)
{
	return a+b+c;
}

float Triangle:: area(void)
{
	float p, i;
	p = (a + b + c) / 2;
    i = sqrt(p * (p - a) * (p - b) * (p - c));
	return i;
}

Triangle:: Triangle(float x, float y, float z)
{
	a = x;
	b = y;
	c = z;
}

void main(void)
{
	Triangle Tri1(1,1);	//定义三角形类的一个实例(对象)
	Tri1.showMessage();
}


(4)在构造函数中使用参数初始化表对数据成员初始化。

#include<iostream>
using namespace std;
class Triangle
{public:
	float perimeter(void);//计算三角形的周长
	float area(void);//计算并返回三角形的面积
	void showMessage();
    Triangle(float x=1, float y=1, float z=1);
private:
	float a,b,c; //三边为私有成员数据
};

void Triangle:: showMessage()
{
	cout<<"三角形的三边长分别为:"<<a<<"   "<<b<<"   "<<c<<endl;
 	cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl<<endl;
}

float Triangle:: perimeter(void)
{
	return a+b+c;
}

float Triangle:: area(void)
{
	return 0;
}

Triangle:: Triangle(float x, float y, float z): a(x), b(y), c(z){}

void main(void)
{
	Triangle Tri1;	//定义三角形类的一个实例(对象)
	Tri1.showMessage();
	Triangle Tri2(7,8,9);	//定义三角形类的一个实例(对象)
	Tri2.showMessage();

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值