(给出题目描述)
6-1 时钟模拟
代码清单:
#include<iostream>
using namespace std;
class MyTime {
private:
int m_h;
int m_m;
int m_s;
public:
MyTime(int h, int m, int s)
{
m_h = h;
m_m = m;
m_s = s;
}
MyTime()
{
this->m_h = this->m_m = this->m_s = 0;
}
MyTime& operator++()
{
m_s++;
if (m_s >= 60)
{
m_s = m_s - 60;
m_m++;
}
if (m_m >= 60)
{
m_m = m_m - 60;
m_h++;
}
if (m_h >= 24)
{
m_h = m_h - 24;
}
return *this;
}
void set(int h, int m, int s)
{
this->m_h = h;
this->m_m = m;
this->m_s = s;
}
void show()
{
cout << m_h << ":";
cout << m_m << ":";
cout << m_s;
}
};
istream& operator>>(istream& cin, MyTime& temp)
{
int h, m, s;
cin >> h >> m >> s;
temp.set(h, m, s);
return cin;
}
ostream& operator<<(ostream& cout, MyTime& temp)
{
temp.show();
return cout;
}
/* 请在这里填写答案 */
int main()
{
MyTime t1, t2(23, 59, 59), t3;
cin >> t3;
++t1;
cout << t1 << endl;
++t2;
cout << t2 << endl;
++t3;
cout << t3 << endl;
return 0;
}
运行结果截图
题目2
(给出题目描述)
7-1 用虚函数计算各种图形的面积
代码清单:
#include<iostream>
using namespace std;
const float PI = 3.14159f;
class Shape {
public:
virtual double area() = 0;
};
class Cicler :public Shape {
public:
Cicler(double r):radius(r){}
double area()
{
return PI * radius * radius;
}
private:
double radius;
};
class Square:public Shape {
private:
double side;
public:
Square(double a):side(a){}
double area()
{
return side * side;
}
};
class Rectangle:public Shape {
double length;
double width;
public:
Rectangle(double a, double b) :length(a), width(b) {};
double area()
{
return length * width;
}
};
class Trapezoid :public Shape{
private:
double top, bottem, height;
public:
Trapezoid(double a, double b, double h) :top(a), bottem(b), height(h) {};
double area()
{
return (top + bottem) * height * 0.5;
}
};
class Triangle :public Shape {
private:
double base, height;
public:
Triangle(double b, double h) :base(b), height(h) {};
double area()
{
return base * height*0.5;
}
};
int main() {
float a, b, c, d, e, f, g, h, i;
scanf("%f %f %f %f %f %f %f %f %f", &a, &b, &c, &d, &e, &f, &g, &h, &i);
Shape* array[5];
Cicler c1(a); array[0] = &c1;
Square s(b); array[1] = &s;
Rectangle r(c, d); array[2] = &r;
Trapezoid t(e, f, g); array[3] = &t;
Triangle tr(h, i); array[4] = &tr;
float sum = 0;
for (int j = 0; j < 5; j++)
sum += array[j]->area();
printf("%.3lf", sum);
}
运行结果截图
题目3
(给出题目描述)
7-2 动物爱吃什么
代码清单:
#include<iostream>
#include<string>
using namespace std;
class animal {
private:
int num;
string name;
public:
animal(int _num, string _name) :num(_num), name(_name) {}
int getnum()
{
return num;
}
string getname()
{
return name;
}
virtual void eat() = 0;
};
class Dog :public animal {
public:
Dog(int _num, string _name) :animal(_num, _name) {}
void eat()
{
cout << "号" << getname() << "啃骨头" << endl;
}
};
class Cat :public animal {
public:
Cat(int _num, string _name) :animal(_num, _name) {}
void eat()
{
cout << "号" << getname() << "吃小鱼" << endl;
}
};
int main()
{
int num1;
string name1;
cin >> num1 >> name1;
animal* p;
Dog dog(num1, name1);
cin >> num1 >> name1;
Cat cat(num1, name1);
p = &dog;
cout << p->getnum();
p->eat();
p = &cat;
cout << p->getnum();
p->eat();
return 0;
}
运行结果截图