类与对象

类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。

类的定义

定义一个类,本质上是定义一个数据类型的蓝图。这实际上并没有定义任何数据,但它定义了类的名称意味着什么,也就是说,它定义了类的对象包括了什么,以及可以在这个对象上执行哪些操作

类定义是以关键字 class 开头,后跟类的名称。
类的主体是包含在一对花括号中。类定义后必须跟着一个分号或一个声明列表。例如,我们使用关键字 class 定义 Box 数据类型,如下所示:

  1. class Box
  2. {
  3. public:
  4. double length; // 盒子的长度
  5. double breadth; // 盒子的宽度
  6. double height; // 盒子的高度
  7. };

关键字 public 确定了类成员的访问属性。在类对象作用域内,公共成员在类的外部是可访问的。也可以指定类的成员为 privateprotected。

对象

类提供了对象的蓝图,所以基本上,对象是根据类来创建的。声明类的对象,就像声明基本类型的变量一样。下面的语句声明了类 Box 的两个对象:

  1. Box Box1; // 声明 Box1,类型为 Box
  2. Box Box2; // 声明 Box2,类型为 Box

对象 Box1 和 Box2 都有它们各自的数据成员。

访问数据成员

类的对象的公共数据成员可以使用直接成员访问运算符 (.) 来访问。

  1. #include <iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. public:
  6. double length; // 长度
  7. double breadth; // 宽度
  8. double height; // 高度
  9. };
  10. int main( )
  11. {
  12. Box Box1; // 声明 Box1,类型为 Box
  13. Box Box2; // 声明 Box2,类型为 Box
  14. double volume = 0.0; // 用于存储体积
  15. // box 1 详述
  16. Box1.height = 5.0;
  17. Box1.length = 6.0;
  18. Box1.breadth = 7.0;
  19. // box 2 详述
  20. Box2.height = 10.0;
  21. Box2.length = 12.0;
  22. Box2.breadth = 13.0;
  23. // box 1 的体积
  24. volume = Box1.height * Box1.length * Box1.breadth;
  25. cout << "Box1 的体积:" << volume <<endl;
  26. // box 2 的体积
  27. volume = Box2.height * Box2.length * Box2.breadth;
  28. cout << "Box2 的体积:" << volume <<endl;
  29. return 0;
  30. }

当上面的代码被编译和执行时,它会产生下列结果:

  1. Box1 的体积:210
  2. Box2 的体积:1560

需要注意

私有的成员和受保护的成员不能使用直接成员访问运算符 (.) 来直接访问。我们将在后续的教程中学习如何访问私有成员和受保护的成员。

类成员函数

类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。

类访问修饰符

类成员可以被定义为 public、private 或 protected。默认情况下是定义为 private。

类的构造函数与解析函数

  • 类的构造函数是一种特殊的函数,在创建一个新的对象时调用。
  • 类的析构函数也是一种特殊的函数,在删除所创建的对象时调用。

类的构造函数

构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(); // 这是构造函数
  9. private:
  10. double length;
  11. };
  12. // 成员函数定义,包括构造函数
  13. Line::Line(void)
  14. {
  15. cout << "Object is being created" << endl;
  16. }
  17. void Line::setLength( double len )
  18. {
  19. length = len;
  20. }
  21. double Line::getLength( void )
  22. {
  23. return length;
  24. }
  25. // 程序的主函数
  26. int main( )
  27. {
  28. Line line;
  29. // 设置长度
  30. line.setLength(6.0);
  31. cout << "Length of line : " << line.getLength() <<endl;
  32. return 0;
  33. }

当上面的代码被编译和执行时,它会产生下列结果:

  1. Object is being created
  2. Length of line : 6

带参数的构造函数

默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数这样在创建对象时就会给对象赋初始值,如下面的例子所示:

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(double len); // 这是构造函数
  9. private:
  10. double length;
  11. };
  12. // 成员函数定义,包括构造函数
  13. Line::Line( double len)
  14. {
  15. cout << "Object is being created, length = " << len << endl;
  16. length = len;
  17. }
  18. void Line::setLength( double len )
  19. {
  20. length = len;
  21. }
  22. double Line::getLength( void )
  23. {
  24. return length;
  25. }
  26. // 程序的主函数
  27. int main( )
  28. {
  29. Line line(10.0);
  30. // 获取默认设置的长度
  31. cout << "Length of line : " << line.getLength() <<endl;
  32. // 再次设置长度
  33. line.setLength(6.0);
  34. cout << "Length of line : " << line.getLength() <<endl;
  35. return 0;
  36. }

当上面的代码被编译和执行时,它会产生下列结果:

  1. Object is being created, length = 10
  2. Length of line : 10
  3. Length of line : 6

类的解析函数

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(); // 这是构造函数声明
  9. ~Line(); // 这是析构函数声明
  10. private:
  11. double length;
  12. };
  13. // 成员函数定义,包括构造函数
  14. // 定义构造函数
  15. Line::Line(void)
  16. {
  17. cout << "Object is being created" << endl;
  18. }
  19. // 定义解析函数
  20. Line::~Line(void)
  21. {
  22. cout << "Object is being deleted" << endl;
  23. }
  24. void Line::setLength( double len )
  25. {
  26. length = len;
  27. }
  28. double Line::getLength( void )
  29. {
  30. return length;
  31. }
  32. // 程序的主函数
  33. int main( )
  34. {
  35. Line line;
  36. // 设置长度
  37. line.setLength(6.0);
  38. cout << "Length of line : " << line.getLength() <<endl;
  39. return 0;
  40. }
  1. Object is being created
  2. Length of line : 6
  3. Object is being deleted

拷贝构造函数

友元函数

内联函数

this指针

指向类的指针

类的静态成员