亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 类和对象

C++ 类和对象

在本教程中,我们将通过示例了解对象和类以及如何在 C++ 中使用它们。

在之前的教程中,我们学习了函数和变量。有时希望将相关的函数和数据放在一个地方,这样既合乎逻辑又更易于使用。

假设我们需要存储一个长方形房间的长、宽、高,并计算它的面积和体积。

为了处理这个任务,我们可以创建三个变量,比如 length , 宽度 , 和 高度 连同函数 calculateArea()calculateVolume() .

但是,在 C++ 中,除了创建单独的变量和函数,我们还可以将这些相关的数据和函数包装在一个地方(通过创建 objects )。这种编程范式被称为面向对象的编程。

但在我们创建对象之前 并在 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 , 宽度 , 和 高度 在类中声明的称为数据成员 .并且,函数 calculateArea()calculateVolume() 被称为成员函数 一类的。


C++ 对象

定义类时,只定义对象的规范;没有分配内存或存储空间。

要使用类中定义的数据和访问函数,我们需要创建对象。


在 C++ 中定义对象的语法

className objectVariableName;

我们可以创建 Room 的对象 类(在上面的例子中定义)如下:

// sample function
void sampleFunction() {
    // create objects
    Room room1, room2;
}

int main(){
    // create objects 
    Room room3, room4;
}

这里,两个对象 room1room2 Room 类在 sampleFunction() 中创建 .同样,对象 room3room4main() 中创建 .

正如我们所见,我们可以在程序的任何函数中创建一个类的对象。我们还可以在类本身或其他类中创建类的对象。

此外,我们可以从一个类中创建任意数量的对象。


C++ 访问数据成员和成员函数

我们可以使用 . 访问类的数据成员和成员函数 (点)运算符。例如,

room2.calculateArea();

这将调用 calculateArea() Room 内的函数 对象 room2 的类 .

类似地,数据成员可以这样访问:

room1.length = 5.5;

在这种情况下,它初始化 length room1 的变量 到 5.5 .


示例 1:C++ 编程中的对象和类

// Program to illustrate the working of
// objects and class in C++ Programming

#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() {

    // create object of Room class
    Room room1;

    // assign values to data members
    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 类及其对象 room1 计算房间的面积和体积。

main() ,我们分配了 length 的值 , 宽度 , 和 高度 用代码:

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() { }
}

这里,一个function1() 是私人的。因此不能从类外部访问它们。

另一方面,bfunction2() 可以从程序中的任何地方访问。

要了解有关公共和私有关键字的更多信息,请访问我们的 C++ 类访问修饰符教程。


示例2:在C++类中使用public和private

// Program to illustrate the working of
// public and private in C++ Class

#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 .

要了解有关对象和类的更多信息,请访问以下主题:


C语言

  1. C# 类和对象
  2. C#抽象类和方法
  3. C# 部分类和部分方法
  4. C# 密封类和方法
  5. C++朋友函数和朋友类
  6. C++ 类模板
  7. Java 类和对象
  8. C++ 中的结构和类
  9. 结构和类的区别:用 C++ 示例解释
  10. C++ 中的存储类
  11. C++ 日期和时间
  12. C++中的多态性