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

常见错误总结

错误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加入的成员是拷贝新成员

cpp 复制代码
#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

cpp 复制代码
#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

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

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

cpp 复制代码
#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

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

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;
}
相关推荐
努力努力再努力FFF1 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
Yzzz-F2 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业2 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn2 小时前
Java Set集合相关知识点
java·开发语言·算法
sakiko_3 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
生成论实验室3 小时前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
AI科技星3 小时前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
Alice-YUE4 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
云泽8084 小时前
C++11 核心特性全解:列表初始化、右值引用与移动语义实战
开发语言·c++
科研前沿4 小时前
镜像孪生VS视频孪生核心技术产品核心优势
大数据·人工智能·算法·重构·空间计算