C++ 面向对象
# C++ 类 & 对象
C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计,类是 C++ 的核心特性,通常被称为用户定义的类型。类用于指定对象的形式,是一种用户自定义的数据类型,它是一种封装了数据和函数的组合。类中的数据称为成员变量,函数称为成员函数。类可以看作是一种模板,可以用来创建具有相同属性和行为的多个对象。
# C++ 类定义
定义一个类需要使用关键字class
,然后指定类的名称,类的主体是包含在一对花括号中,主体包含类的成员变量和函数。定义一个类,本质上是定义一个数据类型的蓝图,它定义了类的对象包括了什么,以及可以在这个对象上执行哪些操作。
下面实例使用关键字class
定义Box
数据类型,包含了三个成员变量length
、breadth
和height
。
class Box {
public:
double length; // 盒子的长度
double breadth; // 盒子的宽度
double height; // 盒子的高度
}
2
3
4
5
6
关键字public
确定了类成员的访问属性,在类对象作用域内,公共成员在类的外部是可以访问的,也可以指定类的成员为private
或protected
。
# 定义 C++ 对象
类提供了对象的蓝图,所以基本上,对象是根据类来创建的,声明类的对象,就像声明基本类型的变量一样,下面的语句声明了类Box
的两个对象:
Box Box1; // 声明 Box1, 类型为 Box
Box Box2; // 声明 Box2, 类型为 Box
2
对象 Box1 和 Box2 都有它们各自的数据成员。
# 访问数据成员
类的对象的公共数据成员可以使用直接成员访问运算符.
来访问。
#include <iostream>
using namespace std;
class Box {
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
double get(void);
void set(double len, double bre, double hei);
};
// 成员函数定义
double Box::get(void) {
return length * breadth * height;
}
void Box::set(double len, double bre, double hei) {
length = len;
breadth = bre;
height = hei;
}
int main() {
Box Box1; // 声明 Box1, 类型为 Box
Box Box2; // 声明 Box2, 类型为 Box
Box Box3; // 声明 Box3, 类型为 Box
double volume = 0.0; // 用于存储体积
// box1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box1 体积
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 的体积: " << volume << endl;
// box2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// box2 体积
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Box2 的体积: " << volume << endl;
// box3 详述
Box3.set(16.0, 8.0, 12.0);
volume = Box3.get();
cout << "Box3 的体积: " << volume << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
需要注意的是,私有的成员和受保护的成员不能使用直接成员访问运算符(.
)来直接访问。
# 类 & 对象详解
目前为止,已经对C++的类和对象有基本的了解,下面的列表中还列出了其他一些C++类和对象相关的概念。
# 类成员函数
类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。类成员函数是类的一个成员,可以操作类的任意对象,可以访问对象中的所有成员。
看看之前定义的类Box
,现在要使用成员函数来访问类的成员,而不是直接访问这些类的成员:
class Box {
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
double getVolume(void); // 返回体积
}
2
3
4
5
6
7
成员函数可以定义在类定义内部,或者单独使用范围解析运算符::
来定义。在类定义中定义的成员函数将把函数声明为内联的,即便没有使用inline
标识符,所以可以按照如下方式定义getVolume()
函数:
class Box {
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
double getVolume(void) {
return length * breadth * height;
}
}
2
3
4
5
6
7
8
9
10
也可以在类的外部使用**范围解析运算符::
**定义该函数,如下:
double Box::getVolume(void) {
return length * breadth * height;
}
2
3
在这里,需要强调一点,在::
运算符之前必须使用类名。调用成员函数是在对象上使用点运算符(.
),这样它就能操作与该对象相关的数据,如下:
Box myBox; // 创建一个对象
myBox.getVolume(); // 调用该对象的成员函数
2
#include <iostream>
using namespace std;
class Box
{
public:
double length; // 长度
double breadth; // 宽度
double height; // 高度
// 成员函数声明
double getVolume(void);
void setLength(double len);
void setBreadth(double bre);
void setHeight(double hei);
};
double Box::getVolume(void)
{
return length * breadth * height;
}
void Box::setLength(double len)
{
length = len;
}
void Box::setBreadth(double bre)
{
breadth = bre;
}
void Box::setHeight(double hei)
{
height = hei;
}
// 程序的主函数
int main()
{
Box box1; // 声明 box1, 类型为 Box
Box box2; // 声明 box2, 类型为 Box
double volume = 0.0; // 用于存储体积
// box1 详述
box1.setLength(6.0);
box1.setBreadth(7.0);
box1.setHeight(5.0);
// box2 详述
box2.setLength(12.0);
box2.setBreadth(13.0);
box2.setHeight(10.0);
// box1 体积
volume = box1.getVolume();
cout << "box1 的体积: " << volume << endl;
// box2 体积
volume = box2.getVolume();
cout << "box2 的体积: " << volume << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 类访问修饰符
数据封装是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员。类成员的访问限制是通过类主体内部对各个区域标记public
、private
、protected
来指定的,关键字public
、private
、protected
称为访问修饰符。一个类可以有多个public
、private
、protected
标记区域,每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的,成员和类的默认访问修饰符是private
。
class Base
{
public:
// 共有成员
protected:
// 受保护成员
private:
// 私有成员
}
2
3
4
5
6
7
8
9
10
11
# 公有(public)成员
公有成员在程序中类的外部是可访问的,可以不使用任何成员函数来设置和获取公有变量的值,如下:
#include <iostream>
using namespace std;
class Line
{
public:
double length;
void setLength(double len);
double getLength(void);
};
// 成员函数定义
double Line::getLength(void)
{
return length;
}
void Line::setLength(double len)
{
length = len;
}
// 程序的主函数
int main()
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line: " << line.getLength() << endl;
// 不使用成员函数设置长度
line.length = 10.0; // OK: 因为 length 是公有的
cout << "Length of line : " << line.length << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 私有(private)成员
私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的,只有类和友元函数可以访问私有成员。默认情况下,类的所有成员都是私有的,例如在下面的类中,width
是一个私有成员,这意味着,如果没有使用任何访问修饰符,类的成员将假定为私有成员:
class Box
{
double width;
public:
double length;
void setWidth(double wid);
double getWidth(void);
}
2
3
4
5
6
7
8
实际操作中,一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数,如下:
#include <iostream>
using namespace std;
class Box
{
public:
double length;
void setWidth(double wid);
double getWidth(void);
private:
double width;
};
// 成员函数定义
double Box::getWidth(void)
{
return width;
}
void Box::setWidth(double wid)
{
width = wid;
}
// 程序的主函数
int main()
{
Box box;
// 不使用成员函数设置长度
box.length = 10.0; // OK: 因为 length 是公有的
cout << "Length of box: " << box.length << endl;
// 不使用成员函数设置宽度
// box.width = 10.0; // Error: 因为 width 是私有的
box.setWidth(10.0); // 使用成员函数设置宽度
cout << "Width of box: " << box.getWidth() << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# protected(受保护)成员
**protected(受保护)**成员变量或函数与私有成员十分相似,但有一点不同,protected
成员在派生类(即子类)中是可访问的。
#include <iostream>
using namespace std;
class Box
{
protected:
double width;
};
class SmallBox:Box // SmallBox 是派生类
{
public:
void setSmallWidth(double wid);
double getSmallWidth(void);
};
// 子类的成员函数
double SmallBox::getSmallWidth(void)
{
return width;
}
void SmallBox::setSmallWidth(double wid)
{
width = wid;
}
// 程序的主函数
int main()
{
SmallBox box;
// 使用成员函数设置宽度
box.setSmallWidth(5.0);
cout << "width of box: " << box.getSmallWidth() << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 继承中的特点
有public
、protected
、private
三种继承方式,它们相应地改变了基类成员的访问属性
public
继承: 基类public
成员、protected
成员、private
成员的访问属性在派生类中分别变成:public
、protected
、private
protected
继承: 基类public
成员、protected
成员、private
成员的访问属性在派生类中分别变成:protected
、protected
、private
private
继承: 基类public
成员、protected
成员、private
成员的访问属性在派生类中分别变成:private
、private
、private
但无论哪种继承方式,下面两点都没有改变:
private
成员只能被本类成员(类内)和友元访问,不能被派生类访问protected
成员可以被派生类访问
# public 继承
#include <iostream>
#include <assert.h>
using namespace std;
class A
{
public:
int a;
A()
{
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun()
{
cout << a << endl; // 正确
cout << a1 << endl; // 正确
cout << a2 << endl; // 正确
cout << a3 << endl; // 正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : public A
{
public:
int a;
B(int i)
{
A();
a = i;
}
void fun()
{
cout << a << endl; // 正确,public成员
cout << a1 << endl; // 正确,基类的public成员,在派生类中仍是public成员
cout << a2 << endl; // 正确,基类的protected成员,在派生类中仍是protected可以被派生类访问
cout << a3 << endl; // 错误,基类的private成员不能被派生类访问
}
};
int main()
{
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; // 正确
cout << b.a2 << endl; // 错误,类外不能访问protected成员
cout << b.a3 << endl; // 错误,类外不能访问private成员
system("pause");
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# protected 继承
#include <iostream>
#include <assert.h>
using namespace std;
class A
{
public:
int a;
A()
{
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun()
{
cout << a << endl; // 正确
cout << a1 << endl; // 正确
cout << a2 << endl; // 正确
cout << a3 << endl; // 正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : protected A
{
public:
int a;
B(int i)
{
A();
a = i;
}
void fun()
{
cout << a << endl; // 正确,public成员
cout << a1 << endl; // 正确,基类的public成员,在派生类中是protected成员,可以被派生类访问
cout << a2 << endl; // 正确,基类的protected成员,在派生类中仍是protected可以被派生类访问
cout << a3 << endl; // 错误,基类的private成员不能被派生类访问
}
};
int main()
{
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; // 错误,protected成员不能在类外访问
cout << b.a2 << endl; // 错误,类外不能访问protected成员
cout << b.a3 << endl; // 错误,类外不能访问private成员
system("pause");
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# private 继承
#include <iostream>
#include <assert.h>
using namespace std;
class A
{
public:
int a;
A()
{
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun()
{
cout << a << endl; // 正确
cout << a1 << endl; // 正确
cout << a2 << endl; // 正确
cout << a3 << endl; // 正确
}
public:
int a1;
protected:
int a2;
private:
int a3;
};
class B : private A
{
public:
int a;
B(int i)
{
A();
a = i;
}
void fun()
{
cout << a << endl; // 正确,public成员
cout << a1 << endl; // 正确,基类的public成员,在派生类中是private成员,可以被派生类访问
cout << a2 << endl; // 正确,基类的protected成员,在派生类中仍是private可以被派生类访问
cout << a3 << endl; // 错误,基类的private成员不能被派生类访问
}
};
int main()
{
B b(10);
cout << b.a << endl;
cout << b.a1 << endl; // 错误,private成员不能在类外访问
cout << b.a2 << endl; // 错误,类外不能访问private成员
cout << b.a3 << endl; // 错误,类外不能访问private成员
system("pause");
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 构造函数 & 析构函数
# 类的构造函数
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。构造函数的名称与类的名称完全相同,并且不会返回任何类型,也不会返回void
。构造函数可用于为某些成员变量设置初始值,下面的实例有助于更好地理解构造函数的概念:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line: " << line.getLength() << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 带参数的构造函数
默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数,这样在创建对象时就会给对象赋初始值,如下:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(double len); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line: " << line.getLength() << endl;
// 再次设置长度
line.setLength(6.0);
cout << "Length of line: " << line.getLength() << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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
# 使用初始化列表来初始化字段
使用初始化列表来初始化字段
Line::Line(double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
2
3
4
上面的语法等同于如下语法:
Line::Line(double len)
{
length = len;
cout << "Object is being created, length = " << len << endl;
}
2
3
4
5
假设有一个类C,具有多个字段X、Y、Z等需要进行初始化,同理,可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下:
C::C(double a, double b, double c): X(a), Y(b), Z(c)
{
// code
}
2
3
4
# 类的析构函数
类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行,析构函数的名称与类的名称完全相同,只是在前面加了个波浪号(~
)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
Line(); // 这是构造函数
~Line(); // 这是析构函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
void Line::setLength(double len) {
length = len;
}
double Line::getLength(void)
{
return length;
}
// 程序的主函数
int main()
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line: " << line.getLength() << endl;
return 0;
}
2
3
4
5
6
7
8
9
10
11
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