C++面向对象程序设计50道编程题(第15题)

版权声明:版权所有,尊重原创。转载请注明出处: https://blog.csdn.net/qq_36426650/article/details/84627402

C++面向对象程序设计50道编程题(第15题)

摘要:C++程序设计实习是为学生提供了一个既动手又动脑,独立实践的机会,将课本上的理论知识和实际有机的结合起来,锻炼学生的分析问题和解决问题的能力,提高学生运用所学知识解决实际问题的能力。
  本专辑为编程入门者、高校计算机软件专业学习或复习提供C++程序设计题库。
  读者请先独立思考哦,再与参考程序进行比对检查。

一、问题描述

在这里插入图片描述

二、考察内容

  基本面向对象概念,如何创建类、对象,对类私有、保护数据成员和公有成员函数的理解,对派生类的定义和应用理解,简单的数学运算。

三、难度等级

难度等级:★★☆☆☆

四、参考程序

#include <iostream.h>
#include <math.h>
class Point{
protected:
	int x1,y1;
public:
	Point(int a=0,int b=0){x1=a;y1=b;}	
};
class Line:public Point{
protected:
	int x2,y2;
public:
	Line(int a,int b,int c,int d):Point(a,b)
	{		x2=c; y2=d;	}
	float dis()
	{
		float t=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
		return sqrt(t);
	}
};
class Triangle:public Line{
	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 Area()
	{
		Line In1(x3,y3,x2,y2),In2(x3,y3,x1,y1);
		float x=In1.dis(),y=In2.dis(),z=dis();
		float s=(x+y+z)/2.0;
		area=sqrt(s*(s-x)*(s-y)*(s-z));
	}
	void print()
	{
		cout<<"( "<<x1<<','<<y1<<" )"<<'\t';
		cout<<"( "<<x2<<','<<y2<<" )"<<'\t';
		cout<<"( "<<x3<<','<<y3<<" )"<<'\n';
		cout<<"\narea="<<area<<endl;
	}
};
void main()
{
	Triangle tr(1,1,4,1,4,5);
	tr.Area();
	tr.print();
}

五、心得感受

可以在评论处写下思考和编程此题的感受

猜你喜欢

转载自blog.csdn.net/qq_36426650/article/details/84627402