目录
第23节 逼格提升:不懂Linux的程序员 不是真正的程序员
项目十 智能婚恋交友系统
为看书困难的小伙伴推荐视频教程:百度网盘 提取码: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");
}
总结:
-
“.”的使用
-
调用方法时,方法名后需要带一对圆括号()
-
通过对象,只能调用这个对象的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");
}
小结:
-
-> 的使用(类似C语言的结构体用法)
第5节 项目精讲-“生而不同”之构造函数
千人千面的“兵马俑”
在构造(制造)每个兵马俑的时候,使用了不同的“参数”。
构造函数的作用
在创建一个新的对象时,自动调用的函数,用来进行“初始化”工作:对这个对象内部的数据成员进行初始化。
构造函数的特点
-
自动调用(在创建新对象时,自动调用)
-
构造函数的函数名,和类名相同
-
构造函数没有返回类型
-
可以有多个构造函数(即函数重载形式)
构造函数的种类
默认构造函数
自定义的构造函数
拷贝构造函数
赋值构造函数
默认构造函数
没有参数的构造函数,称为默认构造函数。
第6节 合成的默认构造函数
但没有手动定义默认构造函数时,编译器自动为这个类定义一个构造函数。
-
如果数据成员使用了“类内初始值”,就使用这个值来初始化数据成员。【C++11】
-
否则,就使用默认初始化(实际上,不做任何初始化)
实例: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");
}