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

news/2025/2/26 8:25:34

(给出题目描述)

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;

}

运行结果截图


http://www.niftyadmin.cn/n/5868383.html

相关文章

JVM之JVM的组成

Java 虚拟机&#xff08;JVM&#xff09;是 Java 程序的运行核心&#xff0c;它主要由类加载系统、运行时数据区、执行引擎和本地方法接口这几个关键部分组成。 类加载系统&#xff08;Class Loading System&#xff09; 类加载系统负责在程序运行时动态地将 Java 类加载到 J…

375_C++_cloud手机推送,添加人脸告警信息到任务队列中,UploadAlarmPush是典型的工厂模式应用,为什么使用工厂模式完成这部分代码

一:AlarmFaceInfo的应用 让我帮你解析这个lambda表达式的实现: // ...................... .h ...........................// struct RsMsgPushTask_S : public Task{AlarmType_E mainAlarmType;unsigned int subAlarmType;DateTime alarmTime

STM32CUBEIDE FreeRTOS操作教程(十三):task api 任务访问函数

STM32CUBEIDE FreeRTOS操作教程&#xff08;十三&#xff09;&#xff1a;task api 任务访问函数 STM32CUBE开发环境集成了STM32 HAL库进行FreeRTOS配置和开发的组件&#xff0c;不需要用户自己进行FreeRTOS的移植。这里介绍最简化的用户操作类应用教程。以STM32F401RCT6开发板…

Spring Boot集成RocketMQ:真实项目应用场景

第一部分&#xff1a;基础配置与简单示例 1. 项目初始化 使用Spring Boot创建一个项目&#xff0c;添加RocketMQ依赖。 POM依赖&#xff08;Maven&#xff09;&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spr…

DeepSeek回答:AI时代Go语言学习路线

最近有小伙伴经常会问&#xff1a;**该如何学习入门Go语言&#xff1f;怎样提升Go语言Coding水平&#xff1f;**这篇文章我们就使用DeepSeek来梳理下Go语言在AI时代的学习路线。 向DeepSeek提问的问题原文&#xff1a; 你现在是一名资深的Go语言工程师&#xff0c;精通Go语言并…

MySQL索引失效

MySQL索引失效会导致查询性能下降&#xff0c;常见原因及解决方案如下&#xff1a; 一、使用OR条件 原因&#xff1a;当OR条件中有一个列没有索引时&#xff0c;索引可能失效 解决方法&#xff1a;确保OR条件中的所有列都有索引&#xff0c;或使用UNION替代OR -- 不推荐 SE…

进程状态(R|S|D|t|T|X|Z)、僵尸进程及孤儿进程

文章目录 一.进程状态进程排队状态&#xff1a;运行、阻塞、挂起 二.Linux下的进程状态R 运行状态&#xff08;running&#xff09;S 睡眠状态&#xff08;sleeping)D 磁盘休眠状态&#xff08;Disk sleep&#xff09;t 停止、暂停状态(tracing stopped)T 停止、暂停状态(stopp…

【UCB CS 61B SP24】Lecture 14 - Data Structures 1: Disjoint Sets学习笔记

本文内容为数据结构并查集&#xff08;DSU&#xff09;的介绍与实现&#xff0c;详细讲解了并查集这一数据结构所能实现的各种操作&#xff0c;以及如何通过路径压缩与按秩合并大幅优化并查集的效率。 1. 并查集 1.1 介绍及其基础操作 并查集&#xff08;Disjoint Set Union…