C++面向对象的编程-习题10(继承习题)

1·题目介绍

在这里插入图片描述

2·参考程序

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
protected:
	int x1;
	int y1;
public:
	Point(int a, int b)
	{
		x1 = a;
		y1 = b;
	}
};
class Line:public Point 
{
protected:
	int x2;
	int y2;
public:
	Line(int a, int b, int c, int d) :Point(a, b)
	{
		x2 = c;
		y2 = d;
	}
	double fa()
	{
		double s;
		s = sqrt((x2 - x1) * (x2 - x1)+(y2 - y1) * (y2 - y1));
		return s;
	}
};
class Triangle:public Line
{
private:
	int x3, y3;
	double area;
public:
	Triangle(int a, int b, int c, int d, int e, int f) :Line(a, b, c, d)
	{
		x3 = e;
		y3 = f;
	}
	void f()
	{
		Line l1(x2, y2, x3, y3);
		Line l2(x1, y1, x3, y3);
		Line l3(x1, y1, x2, y2);
		double  x, y, z;
		x = l1.fa();
		y = l2.fa();
		z = l3.fa();
		double s;
		s = (x + y + z) / 2.0;
		area = sqrt(s * (s - x) * (s - y) * (s - z));
	}
	void print()
	{
		cout << x1 << '\t' << y1 << endl;
		cout << x2 << '\t' << y2 << endl;
		cout << x3 << '\t' << y3 << endl;
		cout << "面积是:" << area << endl;
	}
};
int main()
{
	Triangle tri(1, 1, 4, 1, 4, 5);
	tri.f();
	tri.print();
	return 0;
}

3·心得收获

a·若在派生类中使用与基类相同的功能时,尽可能的去调用函数,不要再写同样的函数
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值