oop练习(第15周)

这里写目录标题

  • 函数:
    • 狗的继承 (10分)
    • 6-2 写出派生类构造方法(C++) (5分)
    • 6-3 派生类的定义和使用 (10分)
    • 6-4 长方形派生 (5分)
    • 6-5 多重继承 (5分)
  • 编程:
    • 7-1 定义基类Point和派生类Circle,求圆的周长. (10分)
    • 7-2 点到原点的距离(继承) (50分)
    • 7-3 动物世界 (15分)
    • 7-4 时间模拟 (50分)
    • 7-5 两点间距离计算 (40分)
    • 7-6 多边形周长计算(继承) (50分)
    • 7-7 日程安排(多重继承+重载) (40分)

函数:

狗的继承 (10分)

完成两个类,一个类Animal,表示动物类,有一个成员表示年龄。一个类Dog,继承自Animal,有一个新的数据成员表示颜色,合理设计这两个类,使得测试程序可以运行并得到正确的结果。

输入样例:

输出样例:
age of ani:5
infor of dog:
age:5
color:black

#include
using namespace std;
class Animal {
     
public:
	Animal(int x = 0) {
     
		age = x;
	}
	int getAge() {
     
		return age;
	}
private:
	int age;
};
class  Dog :public Animal {
     
public:
	Dog(int a, string b) :Animal(a) {
     
		color = b;
	}
	void showInfor() {
     
		cout << "age:" << getAge() << endl;
		cout << "color:" << color << endl;
	}
private:
	string color;
};

int main(){
     
	Animal ani(5);
	cout<<"age of ani:"<<ani.getAge()<<endl;
	Dog dog(5,"black");
	cout<<"infor of dog:"<<endl;
	dog.showInfor();
}

6-2 写出派生类构造方法(C++) (5分)

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:
提示:
观察类的定义和main方法中的测试代码,补全缺失的代码。
裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关函数。

输入样例:
(无)
输出样例:
(Name:Zhang San; id:370202X; sid:1052102; score:96)

#include 
using namespace std;
class People{
     
private:
    string id;
    string name;
public:
    People(string id, string name){
     
        this->id = id;
        this->name = name;
    }
    string getId(){
     
        return this->id;
    }
    string getName(){
     
        return name;
    }
};
class Student : public People{
     
private:
    string sid;
    int score;
public:
    Student(string id, string name, string sid, int score) :People(id,name){
     
        this->sid = sid;
        this->score = score;
    }
    friend ostream& operator <<(ostream &o, Student &s);
};
ostream& operator <<(ostream &o, Student &s){
     
    o << "(Name:" << s.getName() << "; id:"
      << s.getId() << "; sid:" << s.sid
      << "; score:" << s.score << ")";
    return o;
}
int main(){
     
    Student zs("370202X", "Zhang San", "1052102", 96);
    cout << zs  << endl;
    return 0;
}

6-3 派生类的定义和使用 (10分)

按要求完成下面的程序:
1、定义一个Animal类,包含一个void类型的无参的speak方法,输出“animal language!”。
2、定义一个Cat类,公有继承自Animal类,其成员包括:
(1)私有string类型的成员m_strName;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_name,无形参,void类型,功能是输出成员m_strName的值,具体输出格式参见main函数和样例输出。

输入样例:
本题无输入。

输出样例:
cat name: Persian
animal language!

#include 
#include 
using namespace std;

class Animal {
     
public:
    void speak() {
     
        cout << "animal language!";
    }
};
class Cat :public Animal {
     
private:
    string name;
public:    
    Cat(string n = "") {
     
        name = n;
    }
    void print_name() {
     
        cout << "cat name: " << name << endl;
    }
};

int main()
{
     
    Cat cat("Persian"); //定义派生类对象
    cat.print_name();	//派生类对象使用本类成员函数
    cat.speak();	//派生类对象使用基类成员函数
    return 0;
}

6-4 长方形派生 (5分)

如下,现已有一个完整的长方形的类Rectangle, 数据成员有长和宽,成员函数包括一个构造函数和一个计算面积的函数area()。

请写出一个表示长方体的派生类Box,继承这个已给出的Rectangle类,满足以下要求:

(1)只能新增一个数据成员:height (高)。

(2)定义一个合适的Box类构造函数,使得main函数中创建对象的初始化可行;

(3)使用合适的继承方式,使得main函数中能通过派生类Box对象直接调用基类中的area()函数输出底面积。

(4)新增一个成员函数 volume() 返回长方体的体积,使得main函数中的调用可行;

输入样例:

输出样例:
200
6000

#include 
using namespace std;

class Rectangle {
      //长方形
	public:
		Rectangle(double l, double w) : length(l), width(w) {
     
		}
		double area() {
     
			return length * width;
		}
	private:
		double length; //长
		double width; //宽
};

class Box :public Rectangle {
     
public:
	Box(double x, double y, double z) :Rectangle(x, y) {
     
		h = z;
	}
	double volume() {
     
		double a = area();
		return a * h;
	}
private:
	double h;
};

int main() {
     
	Box b1(10, 20, 30);
	cout << b1.area() << endl;
	cout << b1.volume() << endl;
	return 0;
}

6-5 多重继承 (5分)

声明一个教师(Teacher)类和一个学生(Student)类,用多重继承的方式声明一个研究生(Graduate)派生类。教师类中包括数据成员name(姓名),age(年龄),title(职称)。学生类中包括数据成员name(姓名),age(年龄),score(成绩),输出这些数据。

输入样例:
无输入
输出样例:
name:Wang-li
age:24
sex:f
score:89.5
title:assistant
wages:1234.5

#include 
using namespace std;
class Teacher {
     
	string name;
	int age;
	char sex;
	string title;
	double wages;
public:
	Teacher(string a, int b, char c, string d, double f){
     
		name = a;
		age = b;
		sex = c;
		title = d;
		wages = f;
	}
	void print() {
     
		cout << "name:" << name << endl;
		cout << "age:" << age << endl;
		cout << "sex:" << sex << endl;
	}
	void print_title() {
     
		cout << "title:" << title << endl;
	}
	void print_wages() {
     
		cout << "wages:" << wages << endl;
	}
};
class Student {
     
	string name;
	int age;
	char sex;
	double score;
public:
	Student(string a, int b, char c, double d) {
     
		name = a;
		age = b;
		sex = c;
		score = d;
	}
	void print_score() {
     
		cout << "score:" << score << endl;
	}
};
class Graduate : public Teacher, public Student {
     
public:
	Graduate(string na, int ag, char se, string ti, double sc, double wa)
		:Teacher(na, ag, se, ti, wa), Student(na, ag, se, sc) {
     };
	void show() {
     
		print();
		print_score();
		print_title();
		print_wages();
	}

};

int main( ) {
     
	Graduate grad1("Wang-li",24,'f',"assistant",89.5,1234.5);
	grad1.show( );
	return 0;
}

编程:

7-1 定义基类Point和派生类Circle,求圆的周长. (10分)

定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。

输入格式:
输入圆心和半径,x y r中间用空格分隔。

输出格式:
输出圆的周长,小数点后保留2位有效数字。

输入样例:
1 2 3
输出样例:
在这里给出相应的输出。例如:

Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called

#define pi 3.14;
#include 
using namespace std;
class Point {
     
    float x, y;
public:
    Point(float a = 0, float b = 0) :x(a), y(b) {
     
        cout << "Point constructor called" << endl;
    };
    ~Point() {
     
        cout << "Point destructor called" << endl;
    }
};
class Circle :public Point {
     
    float r;
public:
    Circle(float a = 0, float b = 0, float c = 0) :Point(a, b), r(c) {
     
        cout << "Circle constructor called" << endl;
    }
    ~Circle() {
     
        cout << "Circle destructor called" << endl;
    }
    float getCircumference() {
     
        return r * 2.0 * pi;
    }
};
int main() {
     
    float x, y, r;
    cin >> x >> y >> r;
    Circle c(x, y, r);
    cout << fixed << setprecision(2) << c.getCircumference() << endl;
    return 0;
}

7-2 点到原点的距离(继承) (50分)

给出下面的一个基类框架:

class Point_1D

{ protected:

float x;//1D 点的x坐标

public:

Point_1D(float p = 0.0);

float distance( );//计算当前点到原点的距离

}

以Point_1D为基类建立一个派生类Point_2D,增加一个保护数据成员:

float y;//2D平面上点的y坐标

以Point_2D为直接基类再建立一个派生类Point_3D,增加一个保护数据成员:

float z;//3D立体空间中点的z坐标

生成上述类并编写主函数,根据输入的点的基本信息,建立点对象,并能计算该点到原点的距离。

输入格式:
测试输入包含若干测试用例,每个测试用例占一行(点的类型(1表示1D点,2表示2D点,3表示3D点) 第一个点坐标信息(与点的类型相关) 第二个点坐标信息(与点的类型相关))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 -1

2 3 4

3 1 2 2

0

输出样例:

Distance from Point -1 to original point is 1

Distance from Point(3,4) to original point is 5

Distance from Point(1,2,2) to original point is 3

#include 
using namespace std;
class Point_1D {
     
protected:
    float x;

public:
    Point_1D(float p = 0.0) {
     
        x = p;
    }
    float distance() {
     
        return fabs(x);
    }
};
class Point_2D :public Point_1D {
     
protected:
    float y;
public:
    Point_2D(float p = 0.0, float q = 0.0) :Point_1D(p) {
     
        y = q;
    }
    float distance() {
     
        return sqrt(x * x + y * y);
    }
};
class Point_3D :public Point_2D {
     
protected:
    float z;
public:
    Point_3D(float p = 0.0, float q = 0.0, float r = 0.0) :Point_2D(p, q) {
     
        z = r;
    }
    float distance() {
     
        return sqrt(x * x + y * y + z * z);
    }
};
int main() {
     
    int ty;
    while (cin >> ty && ty != 0) {
     
        if (ty == 1) {
     
            float x;
            cin >> x;
            Point_1D aa(x);
            cout << "Distance from Point " << x << " to original point is " << aa.distance() << endl;
        }
        if (ty == 2) {
     
            float x, y;
            cin >> x >> y;
            Point_2D bb(x, y);
            cout << "Distance from Point(" << x << "," << y << ") to original point is " << bb.distance() << endl;
        }
        if (ty == 3) {
     
            float x, y, z;
            cin >> x >> y >> z;
            Point_3D cc(x, y, z);
            cout << "Distance from Point(" << x << "," << y << "," << z << ") to original point is " << cc.distance() << endl;
        }
    }
    return 0;
}

7-3 动物世界 (15分)

补充程序 :

1、实现Mammal类的方法

2、由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型)

3、Dog类中增加以下方法:

constructors: Dog()、Dog(int age)、Dog(int age, int weight)、Dog(int age, COLOR color)、 Dog(int age, int weight, COLOR color)、~Dog()

accessors: GetColor()、SetColor()

Other methods: WagTail()、BegForFood() ,并实现以上这些方法 。

提示:类似Speak()、WagTail()这些动作,函数体可以是输出一句话。比如:Mammal is spaeking… , The Dog is Wagging its tail…

输入格式:

输出格式:
按照程序格式输出。

输入样例:
在这里给出一组输入。例如:


输出样例:
在这里给出相应的输出。例如:

Mammal is speaking…
The dog is wagging its tail…
Yorkie is 3 years old.
Dobbie weighs 20 pounds.

#include 
using namespace std;
enum COLOR {
      WHITE, RED, BROWN, BLACK, KHAKI };
class Mammal{
     
public:
	//constructors
	Mammal() {
     };
	Mammal(int age) {
     
		itsAge = age;
	}
	~Mammal() {
     };

	//accessors
	int GetAge() const {
     
		return itsAge;
	}
	void SetAge(int age) {
     
		itsAge = age;
	}
	int GetWeight() const {
     
		return itsWeight;
	}
	void SetWeight(int weight) {
     
		itsWeight = weight;
	}

	//Other methods	
	void Speak() const {
     
		cout << "Mammal is speaking..." << endl;
	}
	void Sleep() const {
     
		cout << "Mammal is sleeping..." << endl;
	}
protected:
	int itsAge;
	int itsWeight;
};
class Dog :public Mammal {
     
public:
	Dog() {
     };
	~Dog() {
     };
	Dog(int age) :Mammal(age) {
     };
	Dog(int age, int weight) :Mammal(age) {
     
		SetWeight(weight);
	}
	Dog(int age, COLOR color) :Mammal(age) {
     
		itsColor = color;
	}
	Dog(int age, int weight, COLOR color) :Mammal(age) {
     
		SetWeight(weight);
		itsColor = color;
	}
	void WagTail() {
     
		cout << "The dog is wagging its tail..." << endl;
	}
private:
	COLOR itsColor;
};
int main()
{
     
	Dog Fido;
	Dog Rover(5);
	Dog Buster(6, 8);
	Dog Yorkie(3, RED);
	Dog Dobbie(4, 20, KHAKI);
	Fido.Speak();
	Rover.WagTail();
	cout << "Yorkie is " << Yorkie.GetAge() << " years old." << endl;
	cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds." << endl;
	return 0;
}

7-4 时间模拟 (50分)

给出下面的基类Time的框架如下:

class Time

{
protected:

    int second;
    int minute;
    int hour;

public:

     void operator++();
     void operator--();
}

建立一个派生类Time_12hours,用于表示十二进制时间,增加以下成员数据:

string type;//标识为12进制时间,type=”12-hours-time”

string interval;//标识为AM或者PM,interval=”AM”或interval=”PM”

增加以下成员函数: void operator++();

 void operator--();

建立一个派生类Time_24hours,用于表示二十四进制时间,增加以下成员数据:

 string type;//标识为24进制时间,type=”24-hours-time”

增加以下成员函数:

 void operator++();

 void operator--();

生成上述类并编写主函数,根据输入的初始时间信息、自增或者自减类型、自增或者自减次数,输出其最后时间信息。

输入格式:
测试输入包含多个测试用例,一个测试用例为一行,每行共五个数字,第一个数字为进制,121表示输入为12进制AM时间,122表示输入为12进制PM时间,输入为24表示输入为24进制时间,第二个数字为hour,第三个数字为minute,第四个数字为second,第五个字符为运算类型,+表示自增,-表示自减,第六个数字为运算次数,0表示测试用例结束。

输入样例:

121 11 59 59 + 3

24 11 59 59 + 3

122 11 59 59 + 3

122 00 00 00 - 3

121 00 00 00 - 5

24 00 00 00 - 1

0

输出样例:

PM 00:00:02

12:00:02

AM 00:00:02

AM 11:59:57

PM 11:59:55

23:59:59

#include 
using namespace std;
class Time{
     
protected:
	int second;
	int minute;
	int hour;
public:
	int total;
	Time() {
     };
	Time(int h, int m, int s) {
     
		hour = h;
		minute = m;
		second = s;
	}
	void operator++() {
     
		second++;
		while (second >= 60) {
     
			second -= 60;
			minute++;
		}
		while (second < 0) {
     
			second += 60;
			minute--;
		}
		while (minute >= 60) {
     
			minute -= 60;
			hour++;
		}
		while (minute < 0) {
     
			minute += 60;
			hour--;
		}
		while (hour >= 24) {
     
			hour -= 24;
		}
		while (hour < 0) {
     
			hour += 24;
		}
	}
	void operator--() {
     
		second--;
		while (second >= 60) {
     
			second -= 60;
			minute++;
		}
		while (second < 0) {
     
			second += 60;
			minute--;
		}
		while (minute >= 60) {
     
			minute -= 60;
			hour++;
		}
		while (minute < 0) {
     
			minute += 60;
			hour--;
		}
		while (hour >= 24) {
     
			hour -= 24;
		}
		while (hour < 0) {
     
			hour += 24;
		}
	}
	void display() {
     
		cout << setw(2) << setfill('0') << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl;
	}
};
class Time_12hours :public Time {
     
private:
	int type;
public:
	Time_12hours() {
     };
	Time_12hours(int h, int m, int s, int t) :Time(h, m, s) {
     
		type = t;
	}
	void operator++() {
     
		second++;
		while (second >= 60) {
     
			second -= 60;
			minute++;
		}
		while (second < 0) {
     
			second += 60;
			minute--;
		}
		while (minute >= 60) {
     
			minute -= 60;
			hour++;
		}
		while (minute < 0) {
     
			minute += 60;
			hour--;
		}
		while (hour >= 12) {
     
			hour -= 12;
			type = 1 - type;
		}
		while (hour < 0) {
     
			hour += 12;
			type = 1 - type;
		}
	}
	void operator--() {
     
		second--;
		while (second >= 60) {
     
			second -= 60;
			minute++;
		}
		while (second < 0) {
     
			second += 60;
			minute--;
		}
		while (minute >= 60) {
     
			minute -= 60;
			hour++;
		}
		while (minute < 0) {
     
			minute += 60;
			hour--;
		}
		while (hour >= 12) {
     
			hour -= 12;
			type = 1 - type;
		}
		while (hour < 0) {
     
			hour += 12;
			type = 1 - type;
		}
	}
	void display() {
     
		if (type == 0)
			cout << "AM" << " ";
		else 
			cout << "PM" << " ";
		cout << setw(2) << setfill('0') << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl;
	}
};
int main(){
     
	int type, h, m, s;
	while (1) {
     
		cin >> type;
		if (type == 0)
			break;
		cin >> h >> m >> s;
		if (type == 121) {
     
			Time_12hours b(h, m, s, 0);
			char c;
			cin >> c;
			int count = 0;
			cin >> count;
			if (c == '+') {
     
				for (int i = 0; i < count; i++) {
     
					b.operator++();
				}
			}
			else {
     
				for (int i = 0; i < count; i++) {
     
					b.operator--();
				}
			}
			b.display();
		}
		else if (type == 122) {
     
			Time_12hours b(h, m, s, 1);
			char c;
			cin >> c;
			int count = 0;
			cin >> count;
			if (c == '+') {
     
				for (int i = 0; i < count; i++) {
     
					b.operator++();
				}
			}
			else {
     
				for (int i = 0; i < count; i++) {
     
					b.operator--();
				}
			}
			b.display();
		}
		else {
     
			Time b(h, m, s);
			char c;
			cin >> c;
			int count = 0;
			cin >> count;
			if (c == '+') {
     
				for (int i = 0; i < count; i++) {
     
					b.operator++();
				}
			}
			else {
     
				for (int i = 0; i < count; i++) {
     
					b.operator--();
				}
			}
			b.display();
		}
	}
	return 0;
}

7-5 两点间距离计算 (40分)

给出下面的一个基类框架:

class Point_1D

{ protected:

float x;//1D 点的x坐标

public:

Point_1D(float p = 0.0);

float distance(const Point_1D & p2);

}

以Point_1D为基类建立一个派生类Point_2D,增加一个保护数据成员:

float y;//2D平面上点的y坐标

以Point_2D为直接基类再建立一个派生类Point_3D,增加一个保护数据成员:

float z;//3D立体空间中点的z坐标

生成上述类并编写主函数,根据输入的点的基本信息,建立点对象,并能计算该点到原点的距离。

输入格式:
测试输入包含若干测试用例,每个测试用例占一行(点的类型(1表示1D点,2表示2D点,3表示3D点) 第一个点坐标信息(与点的类型相关) 第二个点坐标信息(与点的类型相关))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 -1 0

2 3 4 0 0

3 1 2 2 0 0 0

0

输出样例:

Distance from Point -1 to Point 0 is 1

Distance from Point(3,4) to Point(0,0) is 5

Distance from Point(3,3,3) to Point(0,0,0) is 3

#include 
using namespace std;
class Point_1D {
     
protected:
    float x;

public:
    Point_1D(float p = 0.0) {
     
        x = p;
    }
    float distance(Point_1D& op) {
     
        return fabs(x - op.x);
    }
};
class Point_2D :public Point_1D {
     
protected:
    float y;
public:
    Point_2D(float p = 0.0, float q = 0.0) :Point_1D(p) {
     
        y = q;
    }
    float distance(Point_2D& op) {
     
        return sqrt((x - op.x) * (x - op.x) + (y - op.y) * (y - op.y));
    }
};
class Point_3D :public Point_2D {
     
protected:
    float z;
public:
    Point_3D(float p = 0.0, float q = 0.0, float r = 0.0) :Point_2D(p, q) {
     
        z = r;
    }
    float distance(Point_3D& op) {
     
        return sqrt((x - op.x) * (x - op.x) + (y - op.y) * (y - op.y) + (z - op.z) * (z - op.z));
    }
};
int main() {
     
    int ty;
    while (cin >> ty && ty != 0) {
     
        if (ty == 1) {
     
            float x1, x2;
            cin >> x1 >> x2;
            Point_1D a1(x1), a2(x2);
            cout << "Distance from Point " << x1 << " to Point " << x2 << " is " << a1.distance(a2) << endl;
        }
        if (ty == 2) {
     
            float x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            Point_2D b1(x1, y1), b2(x2, y2);
            cout << "Distance from Point(" << x1 << "," << y1 << ") to Point("<<x2<<","<<y2<<") is " << b1.distance(b2) << endl;
        }
        if (ty == 3) {
     
            float x1, y1, z1, x2, y2, z2;
            cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
            Point_3D c1(x1, y1, z1), c2(x2, y2, z2);
            cout << "Distance from Point(" << x1 << "," << y1 << "," << z1 << ") to Point(" << x2 << "," << y2 << "," << z2 << ") is " << c1.distance(c2) << endl;
        }
    }
    return 0;
}

7-6 多边形周长计算(继承) (50分)

给出下面的多边形基类框架:

class polygon

{ protected:

   int number;//边数,最多不超过100条边
private:

   int side_length[100];//边长数组
public:

   polygon();//构造函数根据需要重载
   int perimeter();//计算多边形边长
   void display();//输出多边形边数和周长
}

建立一个派生类rectangle(矩形),增加以下数据成员:
int height;
int width;

增加以下成员函数:
rectangle类的无参和参数化构造函数
int perimeter();//计算矩形边长
void display();//输出多边形边数和周长

建立一个派生类equal_polygon(等边多边形),增加以下数据成员:
int side_len;

增加以下成员函数:
equal_polygon类的无参和参数化构造函数
int perimeter();//计算等边多边形边长
void display();//输出多边形边数和周长

生成上述类并编写主函数,根据输入的多边形信息,相应建立一个多边形类对象或矩形类对象或等边多边形类对象,计算每一个多边形的周长并且输出其边数和周长。

输入格式:
测试输入包含一个测试用例,该测试用例的第一行输入多边形的个数n,接下来n行每一行给出一个多边形的基本信息,每行的第一个数字为当前多边形的类型,0为一般多边形,后面跟随m个数字为m条边的边长,-1为一般多边形边长输入结束标志,1为矩形,后面跟随两个数字,分别为height和width,2为等边多边形,后面跟随两个数字为等边多边形的边数和边长。

输入样例:
3

0 32 54 76 88 24 -1

1 32 54

2 3 32

输出样例:

5 274

4 172

3 96

#include 
using namespace std;
class polygon {
     
protected:
	int number;
private:
	int side_length[100];
public:
	polygon() {
     };
	polygon(int n, int* s) {
     
		number = n;
		for (int i = 0; i < n; i++)
			side_length[i] = s[i];
	}
	int perimeter() {
     
		int sum = 0;
		for (int i = 0; i < number; i++) {
     
			sum += side_length[i];
		}
		return sum;
	}
	void display() {
     
		cout << number << " " << perimeter() << endl;
	}
};
class rectangle :public polygon{
     
private:
	int height;
	int width;
public:
	rectangle() {
     };
	rectangle(int h, int w) {
     
		height = h;
		width = w;
		number = 4;
	}
	int perimeter() {
     
		return (height + width) * 2;
	}
	void display() {
     
		cout << number << " " << perimeter() << endl;
	}
};
class equal_polygon : public polygon {
     
private:
	int side_len;
public:
	equal_polygon() {
     };
	equal_polygon(int n, int x) {
     
		side_len = x;
		number = n;
	}
	int perimeter() {
     
		return side_len * number;
	}
	void display() {
     
		cout << number << " " << perimeter() << endl;
	}
};
int main() {
     
	int n;
	cin >> n;
	while (n--) {
     
		int ty;
		cin >> ty;
		if (ty == 0) {
     
			int s[100];
			int len;
			int i = 0;
			while (1) {
     
				cin >> len;
				if (len == -1) {
     
					break;
				}
				s[i] = len;
				i++;
			}
			polygon a(i, s);
			a.display();
		}
		if (ty == 1) {
     
			int h, w;
			cin >> h >> w;
			rectangle b(h, w);
			b.display();
		}
		if (ty == 2) {
     
			int n, x;
			cin >> n >> x;
			equal_polygon c(n, x);
			c.display();
		}
	}
	return 0;
}

7-7 日程安排(多重继承+重载) (40分)

已有一个日期类Date,包括三个protected成员数据

int year;

int month;

int day;

另有一个时间类Time,包括三个protected成员数据

int hour;

int minute;

int second;

现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:

int ID;//日程的ID

bool operator < (const Schedule & s2);//判断当前日程时间是否早于s2

生成以上类,并编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程,并输出该日程对象的信息。

输入格式:
测试输入包含若干日程,每个日程占一行(日程编号ID 日程日期(**//)日程时间(::))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 2014/06/27 08:00:01

2 2014/06/28 08:00:01

0

输出样例:

The urgent schedule is No.1: 2014/6/27 8:0:1

#include 
using namespace std;
class Date {
     
protected:
	int year;
	int month;
	int day;
public:
	Date(int y, int m, int d) {
     
		year = y;
		month = m;
		day = d;
	}
	void disp_date() {
     
		cout << year << "/" << month << "/" << day;
	}
};
class Time {
     
protected:
	int hour;
	int minute;
	int second;
public:
	Time(int h, int m, int s) {
     
		hour = h;
		minute = m;
		second = s;
	}
	void disp_time() {
      
		cout << " " << hour << ":" << minute << ":" << second; 
	}
};
class Schedule :public Time, public Date {
     
private:
	int ID;
public:
	Schedule(int id, int y, int mon, int d, int h, int min, int s) :Date(y, mon, d), Time(h, min, s) {
     
		ID = id;
	}
	bool operator<(const Schedule& s2) {
     
		if (year < s2.year) {
     
			return 1;
		}
		else if (year > s2.year) {
     
			return 0;
		}
		else {
     
			if (month < s2.month) {
     
				return 1;
			}
			else if (month > s2.month) {
     
				return 0;
			}
			else {
     
				if (day < s2.day) {
     
					return 1;
				}
				else if (day > s2.day) {
     
					return 0;
				}
				else {
     
					if (hour < s2.hour) {
     
						return 1;
					}
					else if (hour > s2.hour) {
     
						return 0;
					}
					else {
     
						if (minute < s2.minute) {
     
							return 1;
						}
						else if (minute > s2.minute) {
     
							return 0;
						}
						else {
     
							if (second < s2.second) {
     
								return 1;
							}
							else {
     
								return 0;
							}
						}
					}
				}
			}
		}
	}
	void display() {
     
		cout << "The urgent schedule is No." << ID << ": ";
		Date::disp_date();
		Time::disp_time();
	}
};
int main() {
     
	int id, y, mon, d, h, min, s;
	Schedule minn(0, 99999, 99, 99, 99, 99, 99);
	int flag = 0;
	while (1) {
     
		cin >> id;
		if (id == 0) {
     
			break;
		}
		char c;
		cin >> y >> c >> mon >> c >> d;
		cin >> h >> c >> min >> c >> s;
		flag = 1;
		Schedule aa(id, y, mon, d, h, min, s);
		if (aa < minn) {
     
			minn = aa;
		}
	}
	if (flag) {
     
		minn.display();
	}
	return 0;
}

你可能感兴趣的:(大一下,程序综合设计)