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

C++核心day3作业

作业:

1.整理思维导图

2.整理课上代码

3.把课上类的三个练习题的构造函数写出来

函数全部类内声明,类外定义

  1. 定义一个矩形类Rec,包含私有属性length、width,包含公有成员方法:

    1. void set_length(int l); //设置长度
    2. void set_width(int w); //设置宽度
    3. int get_length(); //获取长度,将长度的值返回给调用处
    4. int get_width(); //获取宽度,将宽度的值返回给调用处
    5. void show(); //输出周长和面积
#include <iostream>
using namespace std;class Rec {
private:int length;  // 私有属性:长度int width;   // 私有属性:宽度
public:void setLength(int l);  // 设置长度void setWidth(int w);   // 设置宽度int getLength();        // 获取长度int getWidth();         // 获取宽度void show();            // 输出周长和面积
};void Rec::setLength(int l) {length = l;
}void Rec::setWidth(int w) {width = w;
}int Rec::getLength() {return length;
}int Rec::getWidth() {return width;
}void Rec::show() {cout << "周长: " << 2 * (length + width) << endl;cout << "面积: " << length * width << endl;
}int main() {Rec a;int l, w;cout << "输入长度:";cin >> l;a.setLength(l);cout << "输入宽度:";cin >> w;a.setWidth(w);cout << "矩形的周长和面积为:" << endl;a.show();return 0;
}
  1. 定义一个圆类,包含私有属性半径r,公有成员方法:

    1. void set_r(int r); //获取半径
    2. void show //输出周长和面积,show函数中需要一个提供圆周率的参数PI,该参数有默认值3.14
#include <iostream>
#include <iomanip> // 用于设置输出格式
using namespace std;class Circle {
private:double radius; // 私有属性:半径
public:void setR(double &r);       // 设置圆的半径void show(double PI = 3.14); // 输出周长和面积
};void Circle::setR(double &r) {radius = r;
}void Circle::show(double PI) {double circumference = 2 * PI * radius; // 计算周长double area = PI * radius * radius;     // 计算面积cout << "周长: " << fixed << setprecision(2) << circumference << endl;cout << "面积: " << fixed << setprecision(2) << area << endl;
}int main() {Circle c;     // 创建圆类的对象 cdouble r;cout << "输入圆的半径: ";cin >> r;c.setR(r); // 设置半径cout << "圆的周长和面积为:" << endl;c.show();return 0;
}
  1. 定义一个Car类,包含私有属性,颜色color,品牌brand,速度speed,包含公有成员方法:

    1. void display(); //显示汽车的品牌,颜色和速度
    2. void acc(int a); //加速汽车
    3. set函数,设置类中的私有属性
#include <iostream>
#include <string> // 引入字符串库
using namespace std;class Car {
private:string color; // 汽车颜色string brand; // 汽车品牌int speed;    // 汽车速度
public:void set(string c, string b, int s); // 设置汽车的属性void display();                     // 显示汽车信息void acc(int a);                    // 加速汽车
};void Car::set(string c, string b, int s) {color = c;brand = b;speed = s;
}void Car::display() {cout << "品牌: " << brand << ", 颜色: " << color << ", 速度: " << speed << " km/h" << endl;
}void Car::acc(int a) {speed += a; // 增加速度cout << "加速 " << a << " km/h, 当前速度: " << speed << " km/h" << endl;
}int main() {Car myCar; // 创建Car类的对象myCarmyCar.set("红色", "奥迪", 0); // 设置汽车的初始属性cout << "初始状态:" << endl;myCar.display();myCar.acc(50); // 加速50 km/hmyCar.acc(30); // 再加速30 km/hreturn 0;
}

http://www.lryc.cn/news/501453.html

相关文章:

  • socket UDP 环路回显的服务端
  • springboot/ssm车辆违章信息管理系统Java代码web项目汽车违章处罚源码
  • 5G模组AT命令脚本-关闭模组的IP过滤功能
  • STM32:实现ping命令(lwip)
  • nvm安装指定版本显示不存在及nvm ls-remote 列表只出现 iojs 而没有 node.js 解决办法
  • Spring Boot 中 WebClient 的实践详解
  • 在GITHUB上传本地文件指南(详细图文版)
  • 【大模型系列篇】LLaMA-Factory大模型微调实践 - 从零开始
  • 30天学会Go--第7天 GO语言 Redis 学习与实践
  • java 使用JSqlParser和CCJSqlParser 解析sql
  • 基于spring boot的高校专业实习管理系统的设计与实现
  • OpenCV相机标定与3D重建(11)机器人世界手眼标定函数calibrateRobotWorldHandEye()的使用
  • 计算机网络ENSP课设--三层架构企业网络
  • 【openwrt】openwrt-21.02 基于IP地址使用ipset实现策略路由操作说明
  • Git:常用命令
  • 【2025最新版】搭建个人博客教程
  • 微信小程序实现联动删除输入验证码框
  • 数据库中decimal、float 和 double区别
  • 网络编程01
  • el-dialog修改其样式不生效加deep也没用
  • 三天精通一算法之快速排序
  • 互联网、物联网的相关标准
  • Linux题库及答案
  • Android 镜像模式和扩展模式区别探讨-Android14
  • 深度学习笔记之BERT(五)TinyBERT
  • 【时间序列预测】基于PyTorch实现CNN_BiLSTM算法
  • 联想Y7000 2024版本笔记本 RTX4060安装ubuntu22.04双系统及深度学习环境配置
  • VuePress学习
  • 一次“okhttp访问间隔60秒,提示unexpected end of stream“的问题排查过程
  • SQL最佳实践:避免使用COUNT=0