当前位置: 首页 > news >正文

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

(给出题目描述)

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;

}

运行结果截图

相关文章:

  • CSS—盒模型(3分钟结合示例精通盒模型)
  • (十七)WebGL中 图像处理的初识
  • docker部署RustDesk自建服务器
  • 数据库(MySQL):使用命令从零开始在Navicat创建一个数据库及其数据表(一).创建基础表
  • Windows 系统常用快捷键指南
  • Linux 命令大全完整版(14)
  • Linux DMA Engine 基础
  • AI2-THOR环境下实现机器人导航、物体定位与抓取
  • C++经典框架案例(六)
  • CentOS停服后的替代选择:openEuler、Rocky Linux及其他系统的未来展望
  • 《离线唤醒+离线Vosk识别+DeepSeek+离线合成,你的第二大脑》
  • RFID涉密载体柜:智能安全,全程守护,提供智能化的安全管控
  • QEMU源码全解析 —— 内存虚拟化(18)
  • DeepSeek绘制UML设计图
  • HTTP实验(ENSP模拟器实现)
  • 【Python量化金融实战】-第1章:Python量化金融概述:1.2 Python在量化金融中的优势与生态
  • 愿在线段树中搁浅
  • ARM指令集
  • Python 元组全解析:与其他数据类型的对比与应用题】
  • 医疗AI领域中GPU集群训练的关键技术与实践经验探究(下)
  • 企业做微网站/百度指数在线查询小程序
  • 网站建设最便宜多少钱/seo常用的优化工具
  • 中国专业室内设计公司排名/专业黑帽seo
  • 六安市城乡建设委员会网站/西安百度seo
  • 做男女的那个视频网站/百度官网首页下载
  • 如何做关于橱柜网站/cdq百度指数