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

头歌实验--面向对象程序设计

目录

实验五 类的继承与派生

第1关:简易商品系统

任务描述

答案代码

第2关:公司支出计算

任务描述

答案代码

第3关:棱柱体问题

任务描述

答案代码


实验五 类的继承与派生

第1关:简易商品系统

任务描述

答案代码

#include<iostream>
#include<string>
using namespace std;
/***********begin**********/
//此处完成各个类的书写,并实现题目输出
class Shirt {
private:string place;int price;int num;string mar;
public:Shirt(string, int, int, string);void InStorage(int i);void OutStorage(int j);void Calculate();
};
Shirt::Shirt(string a, int b, int c, string d)
{place = a;price = b;num = c;mar = d;
}
void Shirt::InStorage(int i)
{num = num + i;
}
void Shirt::OutStorage(int j)
{if (j > num){cout << "Insufficient  number!" << endl;num = 0;}else {num = num - j;}
}
void Shirt::Calculate()
{int m;m = num * price;cout << "total money=" << m << endl;
} 
class Cap:public Shirt
{
public:Cap(string, int, int, string, string);
private:string shape;
};
Cap::Cap(string a, int b, int c, string d, string e):Shirt(a,b,c,d),shape(e){}class Capboard:public Shirt
{
public:Capboard(string, int, int, string, string);
private:string color;
};
Capboard::Capboard(string a,int b,int c,string d,string f):Shirt(a,b,c,d),color(f){}/**********end***********/int main() {Shirt s1("江西南昌", 235, 150, "纯棉");Cap p1("四川成都", 88, 150, "尼龙", "平顶");Capboard cup1("云南昆明", 3500, 10, "云松木", "原色");int i, j, k, m;cin >> i >> j >> k >> m;s1.InStorage(i);s1.OutStorage(j);p1.OutStorage(k);cup1.OutStorage(m);s1.Calculate();p1.Calculate();cup1.Calculate();
}

第2关:公司支出计算

任务描述

答案代码

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
//请在此处完成YearWork,MonthWorker,WeekWoker及Company类的编码
/***********begin***********/
class Employee{public:virtual int earning()=0;
};
class YearWorker:public Employee{int salary;public:YearWorker(int s){salary=s;}virtual int earning(){return salary;}
};
class MonthWorker:public Employee{int salary;public:MonthWorker(int s){salary=s;}virtual int earning(){return salary*12;}
};
class WeekWorker:public Employee{int salary;public:WeekWorker(int s){salary=s;}virtual int earning(){return salary*52;}
};
class Company{public:Employee *emp[30];Company(){for(int i=0;i<30;i++){emp[i]=NULL;}}int salarypay(){int total=0;for(int i=0;i<30;i++){if(emp[i]!=NULL){total+=emp[i]->earning();}}return total;}  
};
/************end**********/
int main() {Company co;for (int i = 0; i < 30; i++){int r = rand() % 3 + 1;switch (r) {case 1:co.emp[i] = new WeekWorker(580);break;case 2:co.emp[i] = new MonthWorker(2500);break;case 3:co.emp[i] = new YearWorker(22000);break;default:break;}}cout << "company total pay=" << co.salarypay();return 0;
}

第3关:棱柱体问题

任务描述

答案代码

#include <iostream>
using namespace std;
#include<string>
#include"time.h"
#include"math.h"
#define PI 3.14/*********begin**********/
class Plane
{
public:virtual double area() = 0;//求面积函数
};
class Rectangle :public virtual Plane
{
public:double length, width;//长和宽Rectangle(double l, double w) :length(l), width(w) {};virtual double area(){return length * width;//覆盖求面积函数}
};
class Square :public Rectangle{
public:Square(double l) :Rectangle(l,l) {};//析造函数
};
class Circle :public virtual Plane
{
public:double radius;//半径Circle(double r) :radius(r) {};virtual double area(){return 3.14 * radius * radius;}
};
class Triangle :public virtual Plane
{public:double a, b, c;Triangle(double a, double b, double c) :a(a), b(b), c(c) {};virtual double area(){double p;p = (a + b + c) / 2;return sqrt(p * (p - a) * (p - b) * (p - c));}
};
class Body
{
public:virtual double volume() = 0;//体积virtual double superficialarea() = 0;//表面积
};
class Triangularprism :public Triangle,public Body
{
private:double height;
public:Triangularprism( int n,double h, double a, double b, double c) :Triangle(a, b, c), height(h) {};virtual double volume(){return Triangle::area() * height;}virtual double superficialarea() {return (Triangle::area() * 2 + Triangle::a * height + Triangle::b * height + Triangle::c * height);}
};
class Circularcolumn :public Circle,public Body
{
private:double height;
public:Circularcolumn(int n, double h, double r) :Circle(r), height(h) {};virtual double volume(){return Circle::area() * height;}virtual double superficialarea(){return (Circle::radius* 2*3.14*height + Circle::area() * 2 );}
};
class Quadrangular :public Rectangle,public Body
{
private:double height;
public:Quadrangular(int n, double h, double l) :Rectangle(l,l), height(h) {};virtual double volume(){return Rectangle::area() * height;}virtual double superficialarea(){return (Rectangle::area() * 2 + Rectangle::width * height * 2 + Rectangle::length * height*2);}
};/**********end********/
int main() {int n;double height,r,t1,t2,t3,l;cin>>n>>height>>r;//输入n=0,表示圆柱体Circularcolumn c1(n,height,r);cin>>n>>height>>t1>>t2>>t3;//输入n=3,表示三棱柱Triangularprism t(n,height,t1,t2,t3);cin>>n>>height>>l;//输入n=4表示正四棱柱Quadrangular qu(n,height,l);Body *body[3];body[0]=&c1;body[1]=&t;body[2]=&qu;double superficalsum=0;double volumesum=0;for(int i=0;i<3;i++){volumesum+=body[i]->volume();//volume()获取该体的体积superficalsum+=body[i]->superficialarea();//获取该体的表面积}cout<<"all volume="<<volumesum<<endl;cout<<"all superfilarea="<<superficalsum<<endl;
}

相关文章:

头歌实验--面向对象程序设计

目录 实验五 类的继承与派生 第1关&#xff1a;简易商品系统 任务描述 答案代码 第2关&#xff1a;公司支出计算 任务描述 答案代码 第3关&#xff1a;棱柱体问题 任务描述 答案代码 实验五 类的继承与派生 第1关&#xff1a;简易商品系统 任务描述 答案代码 #incl…...

DeepSeek-R1 蒸馏 Qwen 和 Llama 架构 企业级RAG知识库

“DeepSeek-R1的输出&#xff0c;蒸馏了6个小模型”意思是利用DeepSeek-R1这个大模型的输出结果&#xff0c;通过知识蒸馏技术训练出6个参数规模较小的模型&#xff0c;以下是具体解释&#xff1a; - **知识蒸馏技术原理**&#xff1a;知识蒸馏是一种模型压缩技术&#xff0c;核…...

App UI自动化--Appium学习--第二篇

如果第一篇在运行代码的时候出现问题&#xff0c;建议参考我的上一篇文章解决。 1、APP界面信息获取 adb logcat|grep -i displayed代码含义是获取当前应用的包名和界面名。 根据日志信息修改代码当中的包名和界面名&#xff0c;就可以跳转对应的界面。 2、界面元素获取 所…...

【SpringBoot实现全局API限频】 最佳实践

在 Spring Boot 中实现全局 API 限频&#xff08;Rate Limiting&#xff09;可以通过多种方式实现&#xff0c;这里推荐一个结合 拦截器 Redis 的分布式解决方案&#xff0c;适用于生产环境且具备良好的扩展性。 方案设计思路 核心目标&#xff1a;基于客户端标识&#xff08…...

Day1 25/2/14 FRI

【一周刷爆LeetCode&#xff0c;算法大神左神&#xff08;左程云&#xff09;耗时100天打造算法与数据结构基础到高级全家桶教程&#xff0c;直击BTAJ等一线大厂必问算法面试题真题详解&#xff08;马士兵&#xff09;】https://www.bilibili.com/video/BV13g41157hK?p3&v…...

开发板适配之I2C-RTC

rx8010时钟芯片挂载在I2C1总线上&#xff0c;并且集成在主控板上。 硬件原理 IOMUX配置 rx8010时钟芯片挂载在I2C1总线上&#xff0c;I2C1数据IIC1_SDA和时钟IIC1_SCL&#xff0c;分别对应的PAD NAME为&#xff0c;UART4_TX_DATA、UART4_RX_DATA。 在arch/arm/boot/dts/imx6u…...

vuedraggable固定某一item的记录

文章目录 基础用法第一种第二种 限制itemdiaggable重新排序交换移动的两个元素的次序每次都重置item的index 基础用法 第一种 <draggable v-model"list" :options"dragOptions"><div class"item" v-for"item in list" :key…...

我的新书《青少年Python趣学编程(微课视频版)》出版了!

&#x1f389; 激动人心的时刻来临啦&#xff01; &#x1f389; 小伙伴们久等了&#xff0c;我的第一本新书 《青少年Python趣学编程&#xff08;微课视频版&#xff09;》 正式出版啦&#xff01; &#x1f4da;✨ 在这个AI时代&#xff0c;市面上的Python书籍常常过于枯燥&…...

前端开发入门一

前端开发入门一 已经有若干年没有web相关的代码了&#xff0c;以前主要是用C/C编写传统的GUI程序&#xff0c;涉及界面、多线程、网络等知识点。最近准备开发一个浏览器插件&#xff0c;才发现业界已经换了天地&#xff0c;只得重新开始学习了&#xff0c;好在基本的学习能力还…...

Linux(Centos 7.6)命令详解:head

1.命令作用 将每个文件的前10行打印到标准输出(Print the first 10 lines of each FILE to standard output) 2.命令语法 Usage: head [OPTION]... [FILE]... 3.参数详解 OPTION: -c, --bytes[-]K&#xff0c;打印每个文件的前K字节-n, --lines[-]&#xff0c;打印前K行而…...

HTTP请求X-Forwarded-For注入

场景描述 当你对用户网站进行的爆破或者sql注入的时候,为了防止你影响服务器的正常工作,会限制你访问,当你再次访问时,会提示你的由于你的访问频过快或者您的请求有攻击行为,限制访问几个小时内不能登陆,并且重定向到一个错误友好提示页面。 由此可以发起联想?http是无状…...

《生息之地》入围柏林主竞赛,总制片人蒋浩助力青年导演走向国际

当地时间2月13日&#xff0c;第75届柏林国际电影节正式开幕。凤凰传奇影业出品的电影《生息之地》已入围主竞赛单元&#xff0c;是本届电影节最受瞩目的华语作品之一&#xff0c;电影总制片人蒋浩、导演霍猛、监制姚晨等主创一同亮相开幕红毯。《生息之地》是导演霍猛继《过昭关…...

实践记录--电脑故障的问题定位和处理回顾--磁盘故障已解决

快速回顾 01-关于系统异常启动的展示信息&#xff0c;目前已经可以通过拍照翻译的方式辅助理解&#xff1b; 02-关于固态磁盘的故障定位&#xff0c;可以尝试通过SSD-Z工具查看分区引导记录信息&#xff0c;通过diskgenius工具进行坏道检测和修复&#xff1b; 03-体验了diskge…...

uni-app 学习(一)

一、环境搭建和运行 &#xff08;一&#xff09;创建项目 直接进行创建 &#xff08;二&#xff09;项目结构理解 pages 是页面 静态资源 打包文件&#xff0c;看我们想输出成什么格式 app.vue 页面的入口文件 main.js 是项目的入口文件 存放对打包文件的配置 pages 存放整…...

Ubuntu 22.04 Desktop企业级基础配置操作指南

一、网络配置 cd /etc/netplan vi 00-installer-config.yaml 设置如下所示: network:version: 2ethernets:eth0: # 替换为你的实际网络接口名称,如 ens33, enp0s3 等dhcp4: noaddresses:- 192.168.1.100/24 # 静态IP地址和子网掩码gateway4: 192.168.1.254 # 网关地址n…...

QILSTE H4-105LB/5M高亮蓝光LED灯珠 发光二极管LED

H4-105LB/5M&#xff1a;高亮蓝光LED的复杂特性与突发性挑战 在现代电子设备的复杂世界中&#xff0c;H4-105LB/5M型号的高亮蓝光LED以其独特的参数和复杂的特性脱颖而出。这款LED不仅在尺寸上做到了极致精巧&#xff0c;还在光电参数、可靠性测试和实际应用中展现出令人困惑的…...

【Elasticsearch】Elasticsearch检索方式全解析:从基础到实战(一)

文章目录 引言Elasticsearch检索方式概述两种检索方式介绍方式一&#xff1a;通过REST request uri发送搜索参数方式二&#xff1a;通过REST request body发送搜索参数&#xff08;1&#xff09;基本语法格式&#xff08;2&#xff09;返回部分字段&#xff08;3&#xff09;ma…...

封装neo4j的持久层和服务层

目录 持久层 mp 模仿&#xff1a; 1.抽取出通用的接口类 2.创建自定义的repository接口 服务层 mp 模仿&#xff1a; 1.抽取出一个IService通用服务类 2.创建ServiceImpl类实现IService接口 3.自定义的服务接口 4.创建自定义的服务类 工厂模式 为什么可以使用工厂…...

基于Spring Boot的宠物爱心组织管理系统的设计与实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…...

error: conflicting types for ‘SSL_SESSION_get_master_key’

$ make make all-am make[1]: Entering directory ‘/home/linuxuser/tor’ CC src/lib/tls/libtor_tls_a-tortls_openssl.o In file included from src/lib/tls/tortls_openssl.c:61: ./src/lib/tls/tortls_internal.h:55:8: error: conflicting types for ‘SSL_SESSION_get_…...

分布式会话管理实战:Session共享与状态管理的完整方案

分布式会话管理实战&#xff1a;Session共享与状态管理的完整方案 大家好&#xff0c;我是迪哥。分布式系统中&#xff0c;会话管理是一个经典问题。从传统的 Session 复制到 Redis 共享&#xff0c;从 JWT Token 到 OAuth2&#xff0c;我们经历了多种方案的演进。今天就聊聊分…...

跨工具技能同步:构建统一操作习惯的中间层架构与实践

1. 项目概述&#xff1a;一个跨工具技能同步的构想在数字工具爆炸式增长的今天&#xff0c;我们每个人几乎都活在一个“工具丛林”里。作为一名长期与各种生产力工具、开发环境、设计软件打交道的从业者&#xff0c;我深刻体会到一种割裂感&#xff1a;在A工具里熟练无比的快捷…...

PyTorch深度学习资源大全:如何快速找到最佳教程和项目库的终极指南

PyTorch深度学习资源大全&#xff1a;如何快速找到最佳教程和项目库的终极指南 【免费下载链接】the-incredible-pytorch The Incredible PyTorch: a curated list of tutorials, papers, projects, communities and more relating to PyTorch. 项目地址: https://gitcode.c…...

PyODBC:如何用Python一站式连接所有主流数据库?

PyODBC&#xff1a;如何用Python一站式连接所有主流数据库&#xff1f; 【免费下载链接】pyodbc Python ODBC bridge 项目地址: https://gitcode.com/gh_mirrors/py/pyodbc 你是否遇到过这样的困境&#xff1a;公司项目需要连接SQL Server&#xff0c;个人项目要用MySQL…...

Windows 11终极性能调优指南:一键告别卡顿,重获流畅体验 [特殊字符]

Windows 11终极性能调优指南&#xff1a;一键告别卡顿&#xff0c;重获流畅体验 &#x1f680; 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other …...

Windows 平台 OpenClaw 2.7.1 可视化安装避坑技巧与高效配置方法

OpenClaw 2.7.1 Windows 一键部署教程&#xff5c;3 分钟快速搭建本地 AI 智能助手OpenClaw&#xff08;小龙虾&#xff09;是一款实用性极强的本地 AI 智能体工具&#xff0c;适配全系 Windows 系统。软件依托自然语言交互逻辑&#xff0c;可智能完成电脑操控、文件分类管理、…...

2025届学术党必备的五大AI学术助手实测分析

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek AI论文工具在当代学术领域&#xff0c;已然成为了极为关键的辅助支撑力量&#xff0c;它可全…...

2026 年 5 月 CERT 发布 dnsmasq 六个严重安全漏洞,2.93 版本或一周左右发布

消息基本信息西蒙凯利在 2026 年 5 月 11 日&#xff0c;周一&#xff0c;协调世界时 17:18:25 发布消息&#xff0c;上一条消息&#xff08;按线程&#xff09;是[[Dnsmasq 讨论组] DHCP 请求中电路 ID 匹配问题]&#xff0c;下一条消息&#xff08;按线程&#xff09;是[[Dns…...

3分钟掌握缠论可视化:通达信智能技术分析插件终极指南

3分钟掌握缠论可视化&#xff1a;通达信智能技术分析插件终极指南 【免费下载链接】Indicator 通达信缠论可视化分析插件 项目地址: https://gitcode.com/gh_mirrors/ind/Indicator 还在为复杂的缠论理论头疼吗&#xff1f;还在手工画线分析K线图吗&#xff1f;CZSC缠论…...

书匠策AI:论文写作小白也能一键“搞定“毕业论文?深度拆解这个AI神器到底有多香!

微信公众号搜一搜&#xff1a;书匠策AI &#xff5c; 官网直达&#xff1a;www.shujiangce.com 各位同学、各位在论文苦海里挣扎的"秃头星人"们&#xff0c;今天咱们来聊一个让我最近疯狂安利的东西——书匠策AI。 别急着划走&#xff0c;这不是广告&#xff0c;这…...