课程目录展开/折叠
- 课程直播回放
- 第1课 C++编程快速入门
- 第2课 C++程序设计基础知识
- 第3课 基本输入输出和数据类型
- 第4课 基本输入输出和数据类型
- 第5课 基本输入输出和数据类型
- 第6课 数据类型和运算符
- 第7课 数据类型和运算符
- 第8课 C++中的运算符
- 第9课 C++中的运算符
- 第10课 C++中的运算符
- 第11课 C++中的运算符
- 第12课 条件判断和选择语句
- 第13课 条件判断和选择语句
- 第14课 条件判断和选择语句(刷题课)
- 第15课 循环
- 第16课 循环
- 第17课 循环
- 第18课 循环(刷题课)
- 第19课 循环(刷题课)
- 第20课 循环(刷题课)
- 第21课 循环(刷题课)
- 第22课 循环(刷题课)
- 第23课 循环嵌套
- 第24课 循环嵌套(刷题课)
- 第25课 数组
- 第26课 数组
- 第27课 数组(刷题课)
- 第28课 数组(刷题课)
- 第29课 数组(刷题课)
- 第30课 数组
- 第31课 二维数组
- 第32课 二维数组
- 第33课 数组(刷题课)
- 第34课 数组(刷题课)
- 第35课 字符串
- 第36课 字符串
- 第37课 字符串
- 第38课 字符串
- 第39课 字符串
- 第40课 字符串
- 第41课 字符串
- 第42课 字符串
- 第43课 字符串
- 第44课 指针
- 第45课 指针
- 第46课 指针
- 第47课 指针
- 第48课 指针
- 第49课 指针
- 第50课 指针
- 第51课 指针
- 第52课 指针
- 第53课 指针
- 第54课 函数
- 第55课 函数
- 第56课 函数
- 第57课 函数
- 第58课 函数
- 第59课 递归函数
- 第60课 递归函数
- 第61课 递归函数
- 第62课 递归函数
- 第63课 结构体和共用体
- 第64课 结构体和共用体
- 第65课 结构体和共用体
- 第66课 结构体和共用体
- 第67课 结构体和共用体
- 第68课 文件和文件读写
- 第69课 文件和文件读写
- 第70课 面向对象和类
- 第71课 面向对象和类试学
- 第72课 面向对象和类试学
- 第73课 STL中常用算法函数和类试学
- 第74课 整数编码和位运算试学
- 第75课 C++课程总结以及后续学习规划建议试学
第71课 面向对象和类
播放快捷键
播放/暂停:空格(或鼠标单击) 全屏:F(或鼠标双击) 退出全屏:Esc
快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→
快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←
本节课讲解配套PPT&板书:
















本节课讲解到的源代码
源代码下载:第71课 面向对象和类-源代码下载
1. Circle类-1
#include <bits/stdc++.h>
using namespace std;
// const double PI = 3.1415;
class Circle {
private:
double r;
const double PI;
public:
/*
Cirlce()
{}
*/
Circle(double _r) : r(_r), PI(3.1415)
{
// PI = 3.14;
}
double getArea()
{
return PI * r * r;
}
};
/*
void getArea(double r)
{
cout << PI * r * r;
}
*/
int main()
{
Circle c1(5);
cout << c1.getArea() << endl;
return 0;
}
2. Circle类-2
#include <bits/stdc++.h>
using namespace std;
// const double PI = 3.1415;
class Circle {
private:
double r;
const double PI;
public:
Circle(double _r) : r(_r), PI(3.1415)
{
// PI = 3.14;
}
double getArea(); // 声明下成员函数原型
};
// Circle::double getArea()
// 成员函数的具体实现
double Circle::getArea()
{
return PI * r * r;
}
int main()
{
Circle c1(5);
cout << c1.getArea() << endl;
return 0;
}
3. Point类-1
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() // 默认构造函数
{}
Point(int _x, int _y)
{
x = _x;
y = _y;
}
void setX(int _x)
{
x = _x;
}
int getX()
{
return x;
}
void setY(int _y)
{
y = _y;
}
int getY()
{
return y;
}
void print()
{
cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1(1, 2);
p1.print();
Point p2; // 调用默认构造函数
Point p3; // 想用p1初始化p3
p3.setX(p1.getX());
p3.setY(p1.getY());
p3.print();
return 0;
}
4. Point类-2
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() // 默认构造函数
{}
Point(int _x, int _y)
{
x = _x;
y = _y;
}
void print()
{
cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1(1, 2);
p1.print();
// Point p3 = p1; // 想用p1初始化p3 调用的是拷贝构造函数
Point p3(p1); // 与 Point p3 = p1;一样,调用默认的拷贝构造函数
p3.print();
return 0;
}
5. Point类-3
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() // 默认构造函数
{}
Point(int _x, int _y)
{
x = _x;
y = _y;
}
Point(const Point& p) // 自己实现的拷贝构造函数
{
cout << "调用拷贝构造函数" << endl;
x = p.x;
y = p.y;
}
void print()
{
cout << x << ' ' << y << endl;
}
};
// 值传递
void print(Point p) // 会调用拷贝构造函数 Point &p 不会调用
{
p.print();
}
Point getPoint() // 值返回 值传递
{
Point p(5, 6);
return p;
}
int main()
{
/*
Point p1(1, 2);
p1.print();
Point p3 = p1; // 想用p1初始化p3 调用的是拷贝构造函数
// Point p3(p1); // 与 Point p3 = p1;一样,调用默认的拷贝构造函数
p3.print();
Point p4;
p4 = p1;
*/
Point p5(1, 2);
print(p5);
Point p6 = getPoint();
p6.print();
return 0;
}
6. 对象指针-1
#include <bits/stdc++.h>
using namespace std;
class Student {
private:
int id;
char name[30]; // string name;
double h;
public:
string addr;
Student()
{}
Student(int _id, const char _name[], double _h)
{
id = _id;
strcpy(name, _name);
h = _h;
}
void print()
{
cout << id << ' ' << name << ' ' << h << endl;
}
};
int main()
{
/*
Student s1(1, "xiaoming", 189.5); // s1是对象 栈内存中
s1.print();
s1.addr = "Nanjing";
cout << &s1 << endl;
Student *ps1 = NULL; // ps1对象指针,指向Student对象的指针
ps1 = &s1;
ps1->print();
cout << ps1->addr;
*/
/*
Student *ps2 = new Student(2, "lier", 190.2);
ps2->print();
ps2->addr = "Shanghai";
cout << ps2->addr << endl;
(*ps2).print();
cout << (*ps2).addr << endl;
delete ps2;
*/
// Student *ps3 = new Student;
Student *ps3 = new Student();
ps3->print();
return 0;
}
7. this指针-1
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
void setX(int x)
{
x = x;
}
void setY(int y)
{
y = y;
}
void print()
{
cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1;
p1.setX(1);
p1.setY(2);
p1.print();
return 0;
}
8. this指针-2
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
void setX(int x, Point *p)
{
p->x = x;
}
void setY(int y, Point *p)
{
p->y = y;
}
void print()
{
cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1;
p1.setX(1, &p1);
p1.setY(2, &p1);
p1.print();
return 0;
}
9. this指针-3
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
void setX(int x) // void setX(int x, Point *this)
{
cout << "this:" << this << endl;
this->x = x;
}
void setY(int y)
{
this->y = y;
}
void print()
{
cout << this->x << ' ' << this->y << endl;
// cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1;
cout << "main, &p1:" << &p1 << endl;
p1.setX(1); // p1.setX(1, &p1)
p1.setY(2);
p1.print();
return 0;
}
10. this指针-4
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
void setX(int x) // void setX(int x, Point *this)
{
// this->x = x;
(*this).x = x;
}
void setY(int y)
{
// this->y = y;
(*this).y = y;
}
void print()
{
cout << this->x << ' ' << this->y << endl;
// cout << x << ' ' << y << endl;
}
};
int main()
{
Point p1;
cout << "main, &p1:" << &p1 << endl;
p1.setX(1); // p1.setX(1, &p1)
p1.setY(2);
p1.print();
return 0;
}
11. this指针-5
#include <bits/stdc++.h>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point& setX(int x) // void setX(int x, Point *this)
{
this->x = x;
return *this;
}
Point& setY(int y)
{
this->y = y;
return *this;
}
Point* setX2(int x)
{
this->x = x;
return this;
}
Point* setY2(int y)
{
this->y = y;
return this;
}
void print()
{
cout << this->x << ' ' << this->y << endl;
}
};
int main()
{
Point p1;
p1.setX(1).setY(2).print(); // 链式的调用
p1.print();
Point p2;
p2.setX2(1)->setY2(2)->print();
p2.print();
return 0;
}
本节课无课后练习
本节课答疑
建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习
陈远龙老师视频讲解:如何使用DeepSeek进行答疑?
通过AI未能获得满意解答的,可以联系陈远龙老师答疑
目录