第六次作业反馈及参考答案

作业总体来说做得很好,符合我的要求,这里我还要强调几点:

1.一定要提交EXE文件。如果不能生成EXE文件,成绩很低的,尤其是期末考试的时候

2.做题速度,大家一定要快啊,尽量不要迟交,我迟交都是会扣分的,期末考试的话自己看着办,不会收迟交的


本次作业中的几个问题我给大家解答一下:

1.关于头文件的概念:

每个C++/C程序通常分为两个文件。一个文件用于保存程序的声明(declaration),称为头文件。

另一个文件用于保存程序的实现(implementation),称为定义(definition)文件。
C++/C程序的头文件以“.h”为后缀,C++程序的定义文件通常以“.cpp”为后缀。

这个是粗略概括,要知道详细的自己去google

很多同学都在头文件里面写函数的实现,这是一个极不好的习惯,因为有错的话,报的错对你排除错误来说根本没用,就好比我问你是哪里人,你回答,我是中国人一样= =你说这么流利的中文,我第一个反应难道是你不是中国人么。。就像很多人模板出错了来问我,你们的错我真没法回答。。

比如这次作业可以把几个类的声明写在一个头文件中(实际开发一定是一个类一个头文件声明),把具体实现写在一个CPP中(一个CPP对应一个头文件),MAIN函数写在另一个CPP中。


2.关于抽象类的作用

我个人理解是:因为有纯虚函数,所以是抽象类;纯虚函数是必须要继承的子类去实现的函数;而虚函数,则子类可实现,可不实现,用来动态调用函数的。

换句话说,纯虚函数是虚函数的升级版。还有一点是,只有虚函数,但没有纯虚函数的类不叫抽象类,因为它可以实例化;而有纯虚函数,这个函数没有被实现,所以这个类不能被实例化。以后你们学设计模式可能就会明白抽象类是什么用的了。有兴趣自己google。


3.关于函数的括号后面有const

这样的函数叫做const函数,就是这个函数里面不能更改当前类的所有字段,但是可以访问,你们可以再去看下你们自己写的函数。


参考答案:

#ifndef GeometricShape_
#define GeometricShape_

#define US_NS using namespace std

#include <string>
#include <iostream>
US_NS;

class GeometricShape
{
public:
	GeometricShape() {}
	virtual ~GeometricShape() {}

protected:
	virtual bool checkIntegrity(double) const;
	virtual const char * who_am_i() const = 0;
	virtual double getCircumference() const = 0;
	virtual double getArea() const = 0;
	virtual string print() const = 0;
};

class Rectangle : public GeometricShape
{
public:
	Rectangle(double, double);
	Rectangle(const Rectangle&);
	virtual ~Rectangle() {}
  
	virtual const char *who_am_i() const;
	virtual double getCircumference() const;
	virtual double getArea() const;
	virtual string print() const;

protected:
    double _length, _width;
};

class Circle : public GeometricShape
{
public:
	Circle(double);
	Circle(const Circle&);
	virtual ~Circle() {}

    virtual const char *who_am_i() const;
	virtual double getCircumference() const;
	virtual double getArea() const;
	virtual string print() const;

protected:
	double _radius;
};

class Triangle : public GeometricShape
{
public:
	Triangle(double, double, double);
	Triangle(const Triangle&);
	virtual ~Triangle() {}

	virtual const char *who_am_i() const;
	virtual double getCircumference() const;
	virtual double getArea() const;
	virtual string print() const;

protected:
	double _edge_1, _edge_2, _edge_3;

	virtual bool IsTriangle() const;
};

#endif

#include "GeometricShape.h"

bool GeometricShape::checkIntegrity(double length) const
{
	if (length < 0 || length > 1024)
		return false;
	return true;
}

#include "GeometricShape.h"
#include <sstream>
US_NS;

Rectangle::Rectangle(double length = 0, double width = 0)
	: _length(length), _width(width)
{}

Rectangle::Rectangle(const Rectangle &rhs)
: _length(rhs._length), _width(rhs._width)
{}

inline const char *Rectangle::who_am_i() const
{
	return "矩形";
}

inline double Rectangle::getCircumference() const
{
	return (_length + _width) * 2;
}

inline double Rectangle::getArea() const
{
	return _length * _width;
}

string Rectangle::print() const
{
	if (!checkIntegrity(_length) || !checkIntegrity(_width))
		return "长或宽不在0-1024范围内!";
	stringstream output;
	output << "长为"     << _length
		   << "宽为"     << _width
		   << "的"       << who_am_i()
		   << ",周长为"  << getCircumference()
		   << "面积为"   << getArea();
	return output.str();
}

#include "GeometricShape.h"
#include <sstream>
US_NS;

Triangle::Triangle(double edge_1 = 0, double edge_2 = 0, double edge_3 = 0)
: _edge_1(edge_1), _edge_2(edge_2), _edge_3(edge_3)
{}

Triangle::Triangle(const Triangle & rhs)
: _edge_1(rhs._edge_1), _edge_2(rhs._edge_2), _edge_3(rhs._edge_3)
{}

inline const char *Triangle::who_am_i() const
{
	return "三角形";
}

inline double Triangle::getCircumference() const
{
	return _edge_1 + _edge_2 + _edge_3;
}

inline double Triangle::getArea() const
{
	double p = getCircumference() / 2;
	return sqrt(p * (p - _edge_1) * (p - _edge_2) * (p - _edge_3));
}

inline bool Triangle::IsTriangle() const
{
	if (_edge_1 + _edge_2 <= _edge_3 || _edge_1 - _edge_2 >= _edge_3 || _edge_2 - _edge_1 >= _edge_3)
		return false;
	return true;
}

string Triangle::print() const
{
	if (!checkIntegrity(_edge_1) || !checkIntegrity(_edge_2) || !checkIntegrity(_edge_3))
		return "边长不在0-1024范围内!";
	if (!IsTriangle())
		return "三边不能构成三角形!";
	stringstream output;
	output << "三边长分别为" << _edge_1 << " " << _edge_2 << " " << _edge_3 
		   << "的"           << who_am_i()
		   << ",周长为"     << getCircumference()
		   << "面积为"       << getArea();
	return output.str();
}

#include "GeometricShape.h"
#include <sstream>
US_NS;

#define PI 3.1415926

Circle::Circle(double radius = 0)
	: _radius(radius)
{}

Circle::Circle(const Circle &rhs)
	: _radius(rhs._radius)
{}

inline const char * Circle::who_am_i() const
{
	return "圆形";
}

inline double Circle::getCircumference() const
{
    return _radius * 2 * PI;
}

inline double Circle::getArea() const
{

	return _radius * _radius * PI;
}

string Circle::print() const
{
	if (! checkIntegrity(_radius)) 
		return "半径不在0-1024范围内!";
	stringstream output;
	output << "半径为"   << _radius
		   << "的"       << who_am_i()
		   << ",周长为"  << getCircumference()
		   << "面积为"   << getArea();
	return output.str();
}

#include <iostream>
#include <string>
#include "GeometricShape.h"
US_NS;

int main()
{
	Rectangle crec(10, 2);
	cout << crec.print() << endl << endl;

	Circle ccir(2);
	cout << ccir.print() << endl << endl;

	Triangle ctri_1(1, 2, 3);
	cout << ctri_1.print() << endl << endl;
	Triangle ctri_2(3, 4, 5);
	cout << ctri_2.print() << endl << endl;

	return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值