C++ 类和对象
在本教程中,我们将借助示例了解对象和类以及如何在 C++ 中使用它们。
在之前的教程中,我们学习了函数和变量。有时需要将相关的功能和数据放在一个地方,以便合乎逻辑且更易于使用。
假设,我们需要存储一个矩形房间的长、宽和高,并计算它的面积和体积。
为了处理这个任务,我们可以创建三个变量,比如, length, breadth, height
以及函数 calculateArea()
和 calculateVolume()
。
在 C++ 中,我们可以将这些相关的数据和函数包装在一个地方(通过创建对象)。这种编程范式被称为面向对象编程。
在创建和使用对象之前,让我们首先需要了解类。
C++类
类是对象的蓝图。
我们可以将类视为房屋的草图(原型)。它包含有关地板、门、窗等的所有细节。我们根据这些描述建造子。而房子是对象。
创建一个类
在 C++ 中,类是使用关键字 class
后跟类名定义的。
类的主体在大括号内定义,并以分号结尾。
class className {
// some data
// some functions
};
例如,
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
return length * breadth;
}
double calculateVolume(){
return length * breadth * height;
}
};
在这里,我们定义了一个名为 Room
的类。
在类中声明的变量 length
, breadth
和 height
称为数据成员。在类中声明的函数 calculateArea()
和 calculateVolume()
被称为类的成员函数。
C++ 对象
定义类时,只定义对象的规范;没有分配内存或存储。
要使用类中定义的数据和访问函数,我们需要创建对象。
在 C++ 中定义对象的语法
className objectVariableName;
我们可以通过如下语句创建 Room
类的对象:
void sampleFunction() {
Room room1, room2;
}
int main(){
Room room3, room4;
}
这里有两个在 sampleFunction()
中创建的 Room
类的对象 room1
和 room2
。同样,有两个在 main()
中创建的对象 room3
和 room4
。
正如我们所见,我们可以在程序的任何函数中创建类的对象。我们还可以在类本身或其他类中创建类的对象。
我们可以从单个类中创建任意数量的对象。
C++ 访问数据成员和成员函数
我们可以使用 .
(点)运算符访问类的数据成员和成员函数。例如,
room2.calculateArea();
这将调用 Room
类对象 room2
的内部函数 calculateArea()
.
同样,数据成员可以通过以下方式访问:
room1.length = 5.5;
在这种情况下,它初始化 room1
的 length
变量为 5.5
。
示例 1:C++ 编程中的对象和类
此程序演示了 C++ 中的类和对象的工作方式。
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
// 创建 Room 类的对象 room1
Room room1;
// 给成员变量赋值
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
// calculate and display the area and volume of the room
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
输出
Area of Room = 1309
Volume of Room = 25132.8
在这个程序中,我们使用了 Room
类及其对象房间 1 计算房间的面积和体积。
在 中 main()
,我们使用代码给 length
, breadth
, 和 height
分配了值:
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
然后我们调用函数 calculateArea()
并 calculateVolume()
执行必要的计算。
注意,程序中 public
关键字的使用。这意味着成员是公开的,可以从程序的任何地方访问。
根据我们的需要,我们还可以使用 private
关键字创建私有成员。类的私有成员只能从类内部访问。例如,
class Test {
private:
int a;
void function1() { }
public:
int b;
void function2() { }
}
这里, a
和 function1()
是私有的。因此无法从类外部访问它们。
另一方面, b
和 function2()
可以从程序中的任何地方访问。
要了解有关 public 和 private 关键字的更多信息,请访问我们的C++ 类访问修饰符教程。
示例 2:在 C++ 类中使用 public 和 private
此程序演示了 C++ 中的 public
和 private
的用法。
#include <iostream>
using namespace std;
class Room {
private:
double length;
double breadth;
double height;
public:
// function to initialize private variables
void initData(double len, double brth, double hgt) {
length = len;
breadth = brth;
height = hgt;
}
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
// create object of Room class
Room room1;
// pass the values of private variables as arguments
room1.initData(42.5, 30.8, 19.2);
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
输出
Area of Room = 1309
Volume of Room = 25132.8
上面的示例几乎与第一个示例相同,只是类变量现在是私有的。
由于变量现在是私有的,我们不能直接从 main()
中访问他们. 因此,使用以下代码将是无效的:
// invalid code
obj.length = 42.5;
obj.breadth = 30.8;
obj.height = 19.2;
相反,我们使用的公共方法 initData()
通过函数的参数 double len
, double brth
和 double hgt
初始化私有变量。
要了解有关对象和类的更多信息,请访问以下主题: