零基础学习C++系列课程(十一) 持续更新中

本课程以智能婚恋交友系统为例,深入讲解C++的面向对象编程思想,包括类的使用、构造函数、拷贝构造函数、析构函数、静态成员和常成员等概念。通过实际项目开发,帮助学习者掌握C++的核心技能,并了解职场及项目开发中的注意事项。
摘要由CSDN通过智能技术生成

目录

项目十 智能婚恋交友系统

第1节 项目需求

第2节 项目精讲-世界观的颠覆:面向对象的思想

第3节 项目精讲-女娲定义“人类”:类的使用

第4节 项目精讲-女娲造“人”:对象的基本使用

第5节 项目精讲-“生而不同”之构造函数

构造函数的作用

构造函数的特点

构造函数的种类

默认构造函数

第6节 合成的默认构造函数

第7节 手动定义的默认构造函数

第8节 自定义的重载构造函数

第9节 拷贝构造函数

手动定义的拷贝构造函数

合成的拷贝构造函数

第10节 什么时候调用拷贝构造函数

第11节 赋值构造函数

第12节 项目精讲-“最后的晚餐”之析构函数

第13节 项目精讲-永不迷失的真爱:this指针

第14节 项目精讲-类文件的分离

第15节 项目精讲-“大众情人”:静态数据成员

第16节 项目精讲-“不能拥有的方法”:静态成员函数

第17节 项目精讲-永葆初心之常成员

const数据成员

const成员函数

第18节 项目精讲-建模的常用手段:组合与聚合

组合

聚合

第19节 项目实现

第20节 常见错误总结

Error1-const

Error2-vector

Error2-const

Error3-static

第21节 英语不是障碍:计算机英语加油站

第22节 职场修炼:要不要加入创业团队?

第23节 逼格提升:不懂Linux的程序员 不是真正的程序员

第24节 项目练习

项目练习1

项目练习2

项目练习3


项目十 智能婚恋交友系统

为看书困难的小伙伴推荐视频教程:百度网盘 提取码:r59a

第1节 项目需求

婚恋交友-相亲

问题: 效率低下,时间成本大。

解决方案: 自动婚恋交友系统。 成功案例:百合网。

项目目标: 使用面向对象思想,开发自动相亲系统的核心框架。

第2节 项目精讲-世界观的颠覆:面向对象的思想

面向过程: 什么是面向过程? 根据程序的执行过程,来设计软件的所有细节。

面向过程的缺点: 开发大型项目时,越来越难以把控,甚至失去控制。 后期维护、更新成本很大。

解决方案: 使用面向对象。

什么是面向对象? 不是面向对象,写代码:

面向对象是一种开发思想,一种全新的开发方式。

面向对象思想的重要性:

开发大型项目必备,是高级程序员的必备技能!

第3节 项目精讲-女娲定义“人类”:类的使用

面向对象编程,最重要的第一个概念:类

“人类”是一个抽象的概念,不是具体的某个人。 “类”,是看不见,摸不着的,是一个纯粹的概念. “类”,是一种特殊的“数据类型”,不是一个具体的数据。

注意:类, 和基本数据类型(char/int/short/long/long long/float/double)不同

类的构成:方法和数据

​类的设计

定义一个“人类”: Demo

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human {
public:  //公有的,对外的
	void eat(); //方法, “成员函数”
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();

private:
	string name;
	int age;
	int salary;
};

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl; 
}

void Human::play() {
	cout << "我在唱歌! " << endl; 
}

void Human::work() {
	cout << "我在工作..." << endl;
}

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

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

int Human::getSalary() {
	return salary;
}

int main(void) {
	Human  zhangshan;

	system("pause");
}

第4节 项目精讲-女娲造“人”:对象的基本使用

什么是对象?

对象,是一个特定“类”的具体实例。

对象和普通变量有什么区别?

一般地,一个对象,就是一个特殊的变量,但是有跟丰富的功能和用法。

什么时候使用对象?

对象的具体使用方法

方式1

Demo1

int main(void)
{
    Human h1; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”

    // 合法使用
    h1.eat();
    h1.play();
    h1.sleep();

    // 非法使用
    // cout << "年龄" << h1.age << endl;  //直接访问私有成员,将无法通过编译

    //正确使用
    cout << "年龄" << h1.getAge() << endl; //暴露问题,年龄值是一个很大的负数

    system("pause");
}

总结:

  1. “.”的使用

  2. 调用方法时,方法名后需要带一对圆括号()

  3. 通过对象,只能调用这个对象的public方法

分析:

多个不同的对象都有自己的数据,彼此无关。

方式2

Demo2

int main(void)
{
    Human h1; // 通过自定义的特殊数据类型“Human”类, 来创建一个“对象”
    Human *p;

    p = &h1;

    // 合法使用
    p->eat();
    p->play();
    p->sleep();

    // 非法使用
    // cout << "年龄" << p->age << endl;  //直接访问私有成员,将无法通过编译

    //正确使用
    cout << "年龄" << p->getAge() << endl; //暴露问题,年龄值是一个很大的负数

    system("pause");
}

小结:

  1. -> 的使用(类似C语言的结构体用法)

第5节 项目精讲-“生而不同”之构造函数

千人千面的“兵马俑”

在构造(制造)每个兵马俑的时候,使用了不同的“参数”。

构造函数的作用

在创建一个新的对象时,自动调用的函数,用来进行“初始化”工作:对这个对象内部的数据成员进行初始化。

构造函数的特点

  1. 自动调用(在创建新对象时,自动调用)

  2. 构造函数的函数名,和类名相同

  3. 构造函数没有返回类型

  4. 可以有多个构造函数(即函数重载形式)

构造函数的种类

默认构造函数

自定义的构造函数

拷贝构造函数

赋值构造函数

默认构造函数

没有参数的构造函数,称为默认构造函数。

第6节 合成的默认构造函数

但没有手动定义默认构造函数时,编译器自动为这个类定义一个构造函数。

  1. 如果数据成员使用了“类内初始值”,就使用这个值来初始化数据成员。【C++11】

  2. 否则,就使用默认初始化(实际上,不做任何初始化)

实例:demo3

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human
{
public:         //公有的,对外的
    void eat(); //方法, “成员函数”
    void sleep();
    void play();
    void work();

    string getName();
    int getAge();
    int getSalary();

private:
    string name;
    int age = 18;
    int salary;
};

void Human::eat()
{
    cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep()
{
    cout << "我正在睡觉!" << endl;
}

void Human::play()
{
    cout << "我在唱歌! " << endl;
}

void Human::work()
{
    cout << "我在工作..." << endl;
}

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

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

int Human::getSalary()
{
    return salary;
}

int main(void)
{
    Human h1;                                   // 使用合成的默认初始化构造函数
    cout << "年龄: " << h1.getAge() << endl;    //使用了类内初始值
    cout << "薪资:" << h1.getSalary() << endl; //没有类内初始值

    system("pause");
    return 0;
}

​注意:

只要手动定义了任何一个构造函数,编译器就不会生成“合成的默认构造函数”一般情况下,都应该定义自己的构造函数,不要使用“合成的默认构造函数”

【仅当数据成员全部使用了“类内初始值”,才宜使用“合成的默认构造函数”】

第7节 手动定义的默认构造函数

常称为“默认构造函数”

实例:demo4

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human
{
public:         //公有的,对外的
    Human();    //手动定义的“默认构造函数”
    void eat(); //方法, “成员函数”
    void sleep();
    void play();
    void work();

    string getName();
    int getAge();
    int getSalary();

private:
    string name = "Unknown";
    int age = 28;
    int salary;
};

Human::Human()
{
    name = "无名氏";
    age = 18;
    salary = 30000;
}

void Human::eat()
{
    cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep()
{
    cout << "我正在睡觉!" << endl;
}

void Human::play()
{
    cout << "我在唱歌! " << endl;
}

void Human::work()
{
    cout << "我在工作..." << endl;
}

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

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

int Human::getSalary()
{
    return salary;
}

int main(void)
{
    Human h1; // 使用自定义的默认构造函数
    cout << "姓名:" << h1.getName() << endl;
    cout << "年龄: " << h1.getAge() << endl;
    cout << "薪资:" << h1.getSalary() << endl;

    system("pause");
    return 0;
}

说明:如果某数据成员使用类内初始值,同时又在构造函数中进行了初始化,那么以构造函数中的初始化为准。相 当于构造函数中的初始化,会覆盖对应的类内初始值。

第8节 自定义的重载构造函数

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human
{
public:
    Human();
    Human(int age, int salary);

    void eat();
    void sleep();
    void play();
    void work();

    string getName();
    int getAge();
    int getSalary();

private:
    string name = "Unknown";
    int age = 28;
    int salary;
};

Human::Human()
{
    name = "无名氏";
    age = 18;
    salary = 30000;
}

Human::Human(int age, int salary)
{
    cout << "调用自定义的构造函数" << endl;
    this->age = age; // this是一个特殊的指针,指向这个对象本身
    this->salary = salary;
    name = "无名";
}

void Human::eat()
{
    cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep()
{
    cout << "我正在睡觉!" << endl;
}

void Human::play()
{
    cout << "我在唱歌! " << endl;
}

void Human::work()
{
    cout << "我在工作..." << endl;
}

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

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

int Human::getSalary()
{
    return salary;
}

int main(void)
{
    Human h1(25, 35000); // 使用自定义的默认构造函数

    cout << "姓名:" << h1.getName() << endl;
    cout << "年龄: " << h1.getAge() << endl;
    cout << "薪资:" << h1.getSalary() << endl;

    system("pause");
    return 0;
}

第9节 拷贝构造函数

手动定义的拷贝构造函数

Demo.

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个“人类”
class Human
{
public:
    Human();
    Human(int age, int salary);
    Human(const Human &);

    void eat();
    void sleep();
    void play();
    void work();

    string getName();
    int getAge();
    int getSalary();

private:
    string name = "Unknown";
    int age = 28;
    int salary;
};

Human::Human()
{
    name = "无名氏";
    age = 18;
    salary = 30000;
}

Human::Human(int age, int salary)
{
    cout << "调用自定义的构造函数" << endl;
    this->age = age; // this是一个特殊的指针,指向这个对象本身
    this->salary = salary;
    name = "无名";
}

Human::Human(const Human &man)
{
    cout << "调用自定义的拷贝构造函数" << endl;
    name = man.name;
    age = man.age;
    salary = man.salary;
}

void Human::eat()
{
    cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep()
{
    cout << "我正在睡觉!" << endl;
}

void Human::play()
{
    cout << "我在唱歌! " << endl;
}

void Human::work()
{
    cout << "我在工作..." << endl;
}

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

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

int Human::getSalary()
{
    return salary;
}

int main(void)
{
    Human h1(25, 35000); // 使用自定义的默认构造函数
    Human h2(h1);        // 使用自定义的拷贝构造函数

    cout << "姓名:" << h2.getName() << endl;
    cout << "年龄: " << h2.getAge() << endl;
    cout << "薪资:" << h2.getSalary() << endl;

    system("pause");
    return 0;
}

合成的拷贝构造函数

Demo.

#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human
{
public:
    Human();
    Human(int age, int salary);
    // Human(const Human&);  //不定义拷贝构造函数,编译器会生成“合成的拷贝构造函数”

    void eat();
    void sleep();
    void play();
    void work();

    string getName();
    int getAge();
    int getSalary();
    void setAddr(const char *newAddr);
    const char *getAddr();

private:
    string name = "Unknown";
    int age = 28;
    int salary;
    char *addr;
};

Human::Human()
{
    name = "无名氏";
    age = 18;
    salary = 30000;
}

Human::Human(int age, int salary)
{
    cout << "调用自定义的构造函数" << endl;
    this->age = age; // this是一个特殊的指针,指向这个对象本身
    this->salary = salary;
    name = "无名";

    addr = new char[64];
    strcpy_s(addr, 64, "China");
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐成书院

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值