欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 面向对象程序设计-实验十一

面向对象程序设计-实验十一

2025/2/24 11:22:27 来源:https://blog.csdn.net/ZYS7078/article/details/145814508  浏览:    关键词:面向对象程序设计-实验十一

(给出题目描述)

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;

}

运行结果截图

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词