1、类的组合
点,线,三角形。
期望输入:
请输入点1的x的值:12.5
请输入点1的y的值:53.3
请输入点2的x的值:223.7
请输入点2的y的值:62.9
请输入点3的x的值:11.4
请输入点3的y的值:56.3
期望输出:
点1的坐标为:(12.5,53.3)
点2的坐标为:(223.7,62.9)
点3的坐标为:(11.4,56.3)
线1长度:211.418
线2长度:212.403
线3长度:3.19531
该三角形的周长为:427.016
该三角形的面积为:322.08
#include<iostream>
#include<math.h>
using namespace std;
class myPoint
{
public:
myPoint();
myPoint(double x,double y);
double getX();
double getY();
private:
double mX,mY;
};
myPoint::myPoint(double x,double y):mX(x),mY(y){}
double myPoint::getX()
{
return mX;
}
double myPoint::getY()
{
return mY;
}
class Line
{
public:
Line(myPoint& p,myPoint& p0):m_p(p),m_p0(p0){}
double GetDistance()
{
return sqrt(pow(m_p.getX()-m_p0.getX(),2)+pow(m_p.getY()-m_p0.getY(),2));
}
private:
myPoint m_p,m_p0;
};
class Triangle
{
public:
Triangle(myPoint& p1,myPoint& p2,myPoint& p3,double a=0,double b=0,double c=0):m_p1(p1),m_p2(p2),m_p3(p3),m_a(a),m_b(b),m_c(c){}
double getGirth()
{
m_a=sqrt(pow(m_p1.getX()-m_p2.getX(),2)+pow(m_p1.getY()-m_p2.getY(),2));
m_b=sqrt(pow(m_p2.getX()-m_p3.getX(),2)+pow(m_p2.getY()-m_p3.getY(),2));
m_c=sqrt(pow(m_p3.getX()-m_p1.getX(),2)+pow(m_p3.getY()-m_p1.getY(),2));
return m_a+m_b+m_c;
}
double getArea()
{
return sqrt(getGirth()/2.0*(getGirth()/2.0-m_a)*(getGirth()/2.0-m_b)*(getGirth()/2.0-m_c));
}
private:
myPoint m_p1,m_p2,m_p3;
double m_a,m_b,m_c;
};
int main()
{
double x1,x2,x3,y1,y2,y3;
cout<<"请输入点1的x的值:";
cin>>x1;
cout<<"请输入点1的y的值:";
cin>>y1;
cout<<"请输入点2的x的值:";
cin>>x2;
cout<<"请输入点2的y的值:";
cin>>y2;
cout<<"请输入点3的x的值:";
cin>>x3;
cout<<"请输入点3的y的值:";
cin>>y3;
cout<<"点1的坐标为:("<<x1<<","<<y1<<")"<<endl;
cout<<"点2的坐标为:("<<x2<<","<<y2<<")"<<endl;
cout<<"点3的坐标为:("<<x3<<","<<y3<<")"<<endl;
myPoint p1(x1,y1),p2(x2,y2),p3(x3,y3);
Line line1(p1,p2);
Line line2(p2,p3);
Line line3(p3,p1);
cout<<"线1长度:"<<line1.GetDistance()<<endl;
cout<<"线2长度:"<<line2.GetDistance()<<endl;
cout<<"线3长度:"<<line3.GetDistance()<<endl;
Triangle t(p1,p2,p3);
cout<<"该三角形的周长为:"<<t.getGirth()<<endl;
cout<<"该三角形的面积为:"<<t.getArea()<<endl;
return 0;
}
2、虚基类与继承
以学校职工为基类,派生出教师类和管理人员类,又从教师类和管理人员类共同派生出教师管理人员类。
期望输出:
Zhao is a staff, 20 yeas old.
Zhang is a teacher, 30 yeas old, Lecturer.
Wang is a manager, 50 yeas old, Dean.
Li is a teacher manager, 40 yeas old, Peofessor, Department head.
#include <iostream>
using namespace std;
#include<string>
class Staff
{
protected:
int number;
string name;
int age;
public:
Staff(int num,const string na,int a)
{
number = num;
name = na;
age = a;
}
void Display()
{
cout<<name<<" is a staff, "<<age<<" yeas old."<< endl;
}
};
class Teacher:virtual public Staff
{
protected:
string title; //职称
public:
Teacher(int num,const string na,int a,const string ttl);
void Display()
{
cout<<name<<" is a teacher, "<<age<<" yeas old, "<<title<<". "<<endl;
}
};
Teacher::Teacher(int num, const string na, int a,const string ttl):Staff(num,na,a)
{
title = ttl;
}
class Management:virtual public Staff
{
protected:
string post; //职务
public:
Management(int num,const string na, int a,const string pt);
void Display()
{
cout<<name<<" is a manager, "<<age<<" yeas old, "<<post<<". "<<endl;
}
};
Management::Management(int num,const string na, int a,const string pt):Staff(num,na,a)
{
post = pt;
}
class TeacherManagement:public Teacher,public Management
{
public:
TeacherManagement(int num,const string na, int a, const string ttl,const string pt);
void Display()
{
cout<<name<<" is a teacher manager, "<<age<<" yeas old, " << title <<", "<<post<<". "<<endl;
}
};
TeacherManagement::TeacherManagement(int num,const string na, int a, const string ttl,const string pt):
Staff(num,na,a),Teacher(num,na,a,ttl),Management(num,na,a,pt)
{
number = num;
age = a;
name = na;
title = ttl;
post = pt;
}
int main()
{
Staff s1(1,"Zhao",20);
Teacher t1(2,"Zhang",30,"Lecturer");
Management m1(3,"Wang",50,"Dean");
TeacherManagement tm1(4,"Li",40,"Peofessor","Department head");
s1.Display();
t1.Display();
m1.Display();
tm1.Display();
return 0;
}