C++实验(下)

实验七

 Line.h

#include<iostream>
using namespace std;
class Dot{
	public :
		Dot(){};
		Dot(int, int);
		void show();
		~Dot(){};
		double x;
		double y;
};
class Line : public Dot
{
	public :
		Line(Dot, Dot);
		void display();
		~Line(){};
	private :
		Dot PointOne;
		Dot PointTow;
		double length; 
};

Line.cpp

#include<iostream>
#include<math.h>
#include"Line.h"
using namespace std;

Dot::Dot(int a, int b) : x(a), y(b){};
void Dot::show(){
	cout << "(" << x << ", " << y << ")" << endl;
}

Line::Line(Dot a, Dot b){
	PointOne = a;
	PointTow = b;
	length = sqrt(pow(fabs(a.x - b.x) ,2.0) + pow(fabs(a.y - b.y) , 2.0));
	x = (a.x + b.x) / 2;
	y = (a.y + b.y) / 2;
}
void Line::display(){
	cout << "PointOne : " ;
	PointOne.show();
	cout << "PointTow : ";
	PointTow.show();
	cout << "length = " << length << endl;
	cout << "MidPoint : " ;
	show();
}

Main.cpp

#include <iostream>
#include"Line.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[]) {
	Dot a(6, 5);
	Dot b(5, 8);
	Line segOne(a, b);
	segOne.display();
	return 0;
}

#include<iostream>
using namespace std;
class Birthday{
	public :
		Birthday(int, int, int);
		~Birthday(){}
		void show();
		int year;
		int month;
		int day;
};
Birthday::Birthday(int y, int m, int d){
	year = y;
	month = m;
	day = d;
}
void Birthday::show(){
	cout << "Birthday is:  " << endl;  
	cout << "year: " << year;
	cout << "  month: " << month;
	cout << "  day: " << day;
	cout << endl;   
}
class Staff
{
	public :
		Staff(string, string, string);
		~Staff(){}
		void show();
	protected :
		string num;
		string name;
		string sex;	
};
Staff::Staff(string n, string nam, string s){
	num = n;
	name = nam;
	sex = s;
}
void Staff::show(){
	cout << "Staff is :" << endl;
	cout << "num : " << num << endl;
	cout << "name : " << name << endl;
	cout << "sex : " << sex << endl; 
}
class Teacher
{
	public: 
		Teacher(Birthday, Staff);
		void setBirthday();
		void show();
	protected:
		Birthday birth;
		Staff sta;	
};
Teacher::Teacher(Birthday d, Staff a): birth (d), sta (a){}
void Teacher::setBirthday(){
	cout << "请输入生日,年,月,日" << endl;
	cin >> birth.year >> birth.month >> birth.day;
}
void Teacher::show(){
	birth.show();
	sta.show();
}
int main (){
	Birthday b(2018, 5, 5);
	Staff s("1710", "Mr.流氓", "男");
	Teacher teach(b, s);
	teach.show();
	teach.setBirthday();
	teach.show(); 
	return 0;
} 

实验八

#include<iostream>
using namespace std;
class Mammal{
	public:
		Mammal();
	 	virtual void Speack();
}; 

class Dog : public Mammal{
	public :
		Dog();
		void Speack();
};

Mammal::Mammal(){
	cout << "Base construction" << endl;
}
Dog::Dog(){
	cout << "Derive construction" << endl;
}
void Mammal::Speack(){
	cout << "Base Mammal Speack" << endl;
}

void Dog::Speack(){
	cout << "Derive Dog Speack" << endl;	
}
int main (){
	Mammal *ma = NULL;
	Dog dog1;
	ma = &dog1;
	dog1.Mammal::Speack();
	cout << endl;
	
	ma->Speack();
	ma->Mammal::Speack();
	dog1.Speack();
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Harington/article/details/86480212