C++学习路线(二十五)

news2024/10/27 16:00:21

常见错误总结

错误1:对象const问题

#include <iostream>

class Man {
public:
	void walk() {
		std::cout << "I am walking." << std::endl;
	}
};

int main() {
	const Man man;
	man.walk();
	return 0;
}

原因是Man man是const对象 但是调用了非const的成员函数

解决办法1:去掉man前面的const

解决办法2:walk后面加上const

错误2:vector加入的成员是拷贝新成员

#include <iostream>
#include <vector>
using namespace std;

class Man {
public:
	Man(){}
	void play() {
		count += 10;
		cout << "Man is playing" << endl;
	}
	int getCount() const {
		return count;
	}
private:
	int count = 0;
};

int main() {
	vector<Man> mans;
	Man man;
	man.play();
	mans.push_back(man);
	man.play();
	cout << "Total count: " << man.getCount() << endl;
	cout << "vector Total count: " << mans[0].getCount() << endl;
	return 0;
}

错误3:const引用问题

原因:非const引用,不能对const变量进行引用

注意:const引用,可以对非const变量进行引用

错误4:static错误

static方法不能访问实例方法和实例数据成员

Linux学习

1..需要研究 Linux的那些方面?

1)基本命令操作(<50 条命令)

2)Linux 系统编程-服务器开发

2.应该选择 Linux 的哪些发行版本?

Ubuntu

Centos (Redhat 的社区版)
3.使用虚拟机方式还是硬盘安装?

VMware workstation (vmware)

项目练习一

创建一个类,用来表示“玩具
文具,有以下数据:
名称,价格,产地
在使用中,需要获取它的名称,价格,产地。

注意:根据自己当前的优惠情况,有一个对外的价格

Toy.h

#pragma once
#include <string>

class Toy {
public:
	Toy();
	Toy(std::string name, double price, std::string manufacturerLocation);
	~Toy();
	std::string getName() const;
	double getPrice() const;
	std::string getManufacturerLocation() const;
	void setDisCount(double discount);
private:
	std::string name;
	double price;
	std::string manufacturerLocation;
	double discount = 1.0;
};

Toy.cpp

#include "toy.h"

Toy::Toy() {

}

Toy::~Toy() {

}
Toy::Toy(std::string name, double price, std::string maufacturerLocation) {
	this->name = name;
	this->price = price;
	this->manufacturerLocation = maufacturerLocation;
}

std::string Toy::getName() const {
	return this->name;
}

double Toy::getPrice() const {
	return this->price * this->discount;
}

std::string Toy::getManufacturerLocation() const {
	return this->manufacturerLocation;
}

void Toy::setDisCount(double disCount) {
	this->discount = disCount;
}

main.cpp

#include <iostream>
#include "toy.h"
using namespace std;

int main() {
	Toy t("car", 1002.123, "中国");
	cout << t.getName() << t.getPrice() << t.getManufacturerLocation() << endl;
	t.setDisCount(0.5);
	cout << t.getPrice();
	return 0;
}

项目练习二

定义一个或多个类,来描述以下需求:
定义一个类,来表示某模拟养成游戏中人物:

每个人物,有昵称,年龄,性别,配偶,朋友

支持的活动有:结婚,离婚,交友,断交,询问昵称,询问性别,询问年龄,简介

Human.h

#pragma once
#include <string>
#include <iostream>
#include <vector>
enum Gender {
	Male,
	Female
};

class Human {
public:
	int getAge() const;
	std::string Description() const;
	Gender getGender() const;
	std::string getName() const;
	Human* getMarried() const;
	std::vector<Human*> getFriend() const;
	void marry(Human& partner);
	void divorce();
	void addFriend(Human& friend_);
	void removeFriend(Human& friend_);
	Human();
	Human(std::string, int, Gender);
	~Human();

private:
	std::string name;
	int age;
	Gender gender;
	Human* lover = nullptr;
	std::vector<Human*> friends;
};

Human.cpp

#include "Human.h"
#include <sstream>
Human::Human(){}

Human::Human(std::string name , int age , Gender gender) : age(age), name(name), gender(gender) {}
Human::~Human(){}

int Human::getAge() const{
    return age;
}

std::string Human::Description() const {
    std::stringstream ss;
    ss << "Age: " << age;
    ss << " Name: " << name;
    ss << " Gender: " << (gender == Male)? "男" : "女";
    return ss.str();
}

Gender Human::getGender() const{
    return gender;
}

std::string Human::getName() const{
    return name;
}

Human* Human::getMarried() const {
    return lover;
}

std::vector<Human*> Human::getFriend() const {
    return friends;
}

void Human::marry(Human& partner) {
    if (gender == partner.gender) return;
    this->lover = &partner;
    partner.lover = this;
}

void Human::divorce() {
    if (this->lover!= nullptr) {
        this->lover->lover = nullptr;
        this->lover = nullptr;
    }
}

void Human::addFriend(Human& friend_) {
    friends.push_back(&friend_);
}

void Human::removeFriend(Human& friend_) {
    for (auto it = friends.begin(); it != friends.end(); ++it) {
        if (*it == &friend_) {
            friends.erase(it);
        }
        else {
            it++;
        }
    }
}





main.cpp

#include <iostream>
#include "Human.h"
using namespace std;

int main() {
	Human man1("John", 25, Male);
	Human women1("Mary", 30, Female);
	Human man2("Tom", 20, Male);
	Human women2("Lily", 25, Female);

	man1.marry(women1);
	Human* who = man1.getMarried();
	if (who == NULL) {
		cout << "Man1 is not married" << endl;
	}
	else {
		cout << "Man1 is married to " << who->getName() << endl;
	}
	man1.divorce();
	who = man1.getMarried();
	if (who == NULL) {
		cout << "Man1 is not married" << endl;
	}
	else {
		cout << "Man1 is married to " << who->getName() << endl;
	}
	man1.addFriend(man2);
	man1.addFriend(women2);

	/*cout << "Woman1 is married to " << who->getMarried()->Description() << endl;
	*/return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2224776.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

大语言模型的Scaling Law【Power Low】

NLP-大语言模型学习系列目录 一、注意力机制基础——RNN,Seq2Seq等基础知识 二、注意力机制【Self-Attention,自注意力模型】 三、Transformer图文详解【Attention is all you need】 四、大语言模型的Scaling Law【Power Low】 文章目录 NLP-大语言模型学习系列目录一、什么是…

Stable Diffusion视频插件Ebsynth Utility安装方法

一、Ebsynth Utility制作视频的优势&#xff1a; 相比其他视频制作插件&#xff0c;Ebsynth Utility生成的视频&#xff0c;画面顺滑无闪烁&#xff0c;对显存要求相对不高。渲染速度也还可以接受。其基本过程为&#xff1a; 1、将参考视频分解为单个帧&#xff0c;并同时生成…

模型训练识别手写数字(二)

模型训练识别手写数字&#xff08;一&#xff09;使用手写数字图像进行模型测试 一、生成手写数字图像 1. 导入所需库 import cv2 import numpy as np import oscv2用于计算机视觉操作。 numpy用于处理数组和图像数据。 os用于文件和目录操作。 2. 初始化画布 canvas np.z…

GitHub下载参考

1.Git下载 Git下载https://blog.csdn.net/mengxiang_/article/details/128193219 注意&#xff1a;根据电脑的系统配置选择合适的版本&#xff0c;我安装的是64.exe的版本 2.Git右键不出现问题&#xff1a; Git右键不出现https://blog.csdn.net/ling1998/article/details/1…

Java项目实战II基于微信小程序的马拉松报名系统(开发文档+数据库+源码)

目录 一、前言 二、技术介绍 三、系统实现 四、文档参考 五、核心代码 六、源码获取 全栈码农以及毕业设计实战开发&#xff0c;CSDN平台Java领域新星创作者&#xff0c;专注于大学生项目实战开发、讲解和毕业答疑辅导。获取源码联系方式请查看文末 一、前言 马拉松运动…

[SWPUCTF 2022 新生赛]py1的write up

开启靶场&#xff0c;下载附件&#xff0c;解压后得到&#xff1a; 双击exe文件&#xff0c;出现弹窗&#xff1a; 问的是异或&#xff0c;写个python文件来计算结果&#xff1a; # 获取用户输入的两个整数 num1 int(input("Enter the first number: ")) num2 int…

云渲染主要是分布式(分机)渲染,如何使用blender云渲染呢?

云渲染主要是分布式&#xff08;分机&#xff09;渲染&#xff0c;比如一个镜头同时开20-100张3090显卡的机器渲染&#xff0c;就能同时渲染20-100帧&#xff0c;渲染不仅不占用自己电脑&#xff0c;效率也将增加几十上百倍&#xff01; blender使用教程如下&#xff1a; 第一…

基于Django+python的车牌识别系统设计与实现(带文档)

项目运行 需要先安装Python的相关依赖&#xff1a;pymysql&#xff0c;Django3.2.8&#xff0c;pillow 使用pip install 安装 第一步&#xff1a;创建数据库 第二步&#xff1a;执行SQL语句&#xff0c;.sql文件&#xff0c;运行该文件中的SQL语句 第三步&#xff1a;修改源…

软件架构设计学习总结

概述&#xff1b; 如何描述软件架构&#xff1b; 架构的层次结构&#xff1b; 架构设计技能&#xff1a; 需求分析、业务架构、数据架构、应用架构、技术架构、开发架构设计&#xff1b; 层次框架设计&#xff1b; 集成与接口设计&#xff1b; 性能优化&#xff1b; 设计…

C语言程序设计:现代设计方法习题笔记《chapter5》下篇

第七题 题目分析&#xff1a;求最大最小值转换为条件判断问题&#xff0c;最大值有四种可能&#xff0c;最小值相应有三种情况&#xff0c;给出下列代码。 示例代码&#xff1a; #include <stdio.h>int main() {int num1, num2, num3, num4; // 定义四个变量来存储输入…

Linux安装部署数据库:MongoDB

Linux安装部署数据库&#xff1a;MongoDB 一、虚拟机环境说明1、安装前准备2、数据库软件3、数据库工具 二、源码安装 MongoDB1、安装配置环境2、服务启动方式3、设置开机自启 三、管理使用 MongoDB1、登录使用2、常用命令 四、安全优化 MongoDB1、创建普通用户启动服务2、编写…

机器学习 - 树结构1 - 随机森林

算法原理 随机森林由多个决策树构成&#xff0c;每棵树在训练时使用随机选择的样本和特征子集。在分类任务中&#xff0c;每棵树对新的输入样本进行分类&#xff0c;最终的分类结果由多数树的分类结果决定。这种方法可以提高预测的准确性&#xff0c;并且通过平均或投票机制减少…

【C++】动态库动态加载实例详解

动态库动态加载&#xff1a;LoadLibrary与函数地址获取详解 一、概述三、加载失败的原因及解决方案DLL文件不存在或路径不正确&#xff1a;依赖的其他DLL未找到&#xff1a;权限问题&#xff1a;DLL版本不兼容&#xff1a; 四、总结 在软件开发中&#xff0c;模块化设计是一种非…

基于Spring Boot的学生宿舍信息资源整合

3系统分析 3.1可行性分析 通过对本学生宿舍信息管理系统实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本学生宿舍信息管理系统采用Spring Boot框架&#xff0…

【C++笔记】内存管理

前言 各位读者朋友们大家好&#xff0c;上期我们讲了类和对象下的内容&#xff0c;类和对象整体的内容我们就讲完了&#xff0c;接下来我们开启新的部分内存管理的讲解。 目录 前言一. C/C内存分布二. C语言中内存管理的方式三. C内存管理方式3.1 new/delete操作内置类型3.2…

时间序列预测(九)——门控循环单元网络(GRU)

目录 一、GRU结构 二、GRU核心思想 1、更新门&#xff08;Update Gate&#xff09;&#xff1a;决定了当前时刻隐藏状态中旧状态和新候选状态的混合比例。 2、重置门&#xff08;Reset Gate&#xff09;&#xff1a;用于控制前一时刻隐藏状态对当前候选隐藏状态的影响程度。…

idea 无法输入中文 快速解决

idea在某些情况会出现无法输入中文的情况&#xff0c;我们不去深究内部原因&#xff0c;直接上解决方案&#xff1a; 1、点击菜单help->Edit Custom VM Options 2、最后一行&#xff0c;追加&#xff1a; -Drecreate.x11.input.methodtrue 、 3、重启

计算机毕业设计Java连锁超市销售与分析系统 销售数据管理 超市运营分析 数据可视化 (源码+定制+开发)

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…

10月27日

取P为A 秩1矩阵只有1个特征值为正&#xff0c;其余为1

YOLO11 目标检测 | 自动标注 | 预标注 | 标签格式转换 | 手动校正标签

本文分享使用YOLO11进行目标检测时&#xff0c;实现模型推理预标注、自动标注、标签格式转换、以及使用Labelme手动校正标签等功能。 目录 1、预训练权重 2、生成预标注 3、分析YOLO11的目标检测格式 4、分析Labelme标注工具的格式 5、生成json标注文件 6、手动校正标签 …