当前位置: 首页 > 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_…...

Prompt Tuning、P-Tuning、Prefix Tuning的区别

一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...

iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享

平时用 iPhone 的时候&#xff0c;难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵&#xff0c;或者买了二手 iPhone 却被原来的 iCloud 账号锁住&#xff0c;这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

使用van-uploader 的UI组件,结合vue2如何实现图片上传组件的封装

以下是基于 vant-ui&#xff08;适配 Vue2 版本 &#xff09;实现截图中照片上传预览、删除功能&#xff0c;并封装成可复用组件的完整代码&#xff0c;包含样式和逻辑实现&#xff0c;可直接在 Vue2 项目中使用&#xff1a; 1. 封装的图片上传组件 ImageUploader.vue <te…...

spring:实例工厂方法获取bean

spring处理使用静态工厂方法获取bean实例&#xff0c;也可以通过实例工厂方法获取bean实例。 实例工厂方法步骤如下&#xff1a; 定义实例工厂类&#xff08;Java代码&#xff09;&#xff0c;定义实例工厂&#xff08;xml&#xff09;&#xff0c;定义调用实例工厂&#xff…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...

华为OD机试-最短木板长度-二分法(A卷,100分)

此题是一个最大化最小值的典型例题&#xff0c; 因为搜索范围是有界的&#xff0c;上界最大木板长度补充的全部木料长度&#xff0c;下界最小木板长度&#xff1b; 即left0,right10^6; 我们可以设置一个候选值x(mid)&#xff0c;将木板的长度全部都补充到x&#xff0c;如果成功…...

在 Spring Boot 项目里,MYSQL中json类型字段使用

前言&#xff1a; 因为程序特殊需求导致&#xff0c;需要mysql数据库存储json类型数据&#xff0c;因此记录一下使用流程 1.java实体中新增字段 private List<User> users 2.增加mybatis-plus注解 TableField(typeHandler FastjsonTypeHandler.class) private Lis…...

GraphQL 实战篇:Apollo Client 配置与缓存

GraphQL 实战篇&#xff1a;Apollo Client 配置与缓存 上一篇&#xff1a;GraphQL 入门篇&#xff1a;基础查询语法 依旧和上一篇的笔记一样&#xff0c;主实操&#xff0c;没啥过多的细节讲解&#xff0c;代码具体在&#xff1a; https://github.com/GoldenaArcher/graphql…...

绕过 Xcode?使用 Appuploader和主流工具实现 iOS 上架自动化

iOS 应用的发布流程一直是开发链路中最“苹果味”的环节&#xff1a;强依赖 Xcode、必须使用 macOS、各种证书和描述文件配置……对很多跨平台开发者来说&#xff0c;这一套流程并不友好。 特别是当你的项目主要在 Windows 或 Linux 下开发&#xff08;例如 Flutter、React Na…...