C++ 八种常见类类型

开发
大部分面向对象开发工作中都应用了以下部分或者全部的基本类别的类,每种都有其特定的用途和特征。

大部分面向对象开发工作中都应用了以下部分或者全部的基本类别的类,每种都有其特定的用途和特征。

1.具体类 (Concrete Class)

我们可以创建一个具体类来表示汽车。具体类Car可能会包含成员变量如brand(品牌)、model(型号)和成员函数如start()(启动)、accelerate()(加速)等。

#include <iostream>
#include <string>

class Car {
private:
    std::string brand;
    std::string model;
public:
    Car(std::string brand, std::string model) : brand(brand), model(model) {}

    void start() {
        std::cout << "Starting the " << brand << " " << model << "...\n";
    }

    void accelerate() {
        std::cout << "Accelerating the " << brand << " " << model << "...\n";
    }
};

int main() {
    Car myCar("Toyota", "Camry");
    myCar.start();
    myCar.accelerate();
    return 0;
}

2.抽象类 (Abstract Class)

我们可以创建一个抽象类Shape来表示形状,其中包含一个纯虚函数calculateArea()用于计算面积。

#include <iostream>

class Shape {
public:
    virtual double calculateArea() const = 0;
};

class Circle : public Shape {
private:
    double radius;
public:
    Circle(double radius) : radius(radius) {}

    double calculateArea() const override {
        return 3.14 * radius * radius;
    }
};

int main() {
    Circle circle(5);
    std::cout << "Area of the circle: " << circle.calculateArea() << std::endl;
    return 0;
}

3.接口类 (Interface Class)

接口类可以用来定义一组接口,例如Drawable接口可以定义绘制图形的方法。

#include <iostream>

class Drawable {
public:
    virtual void draw() const = 0;
};

class Circle : public Drawable {
public:
    void draw() const override {
        std::cout << "Drawing a circle\n";
    }
};

int main() {
    Circle circle;
    circle.draw();
    return 0;
}

4.节点类 (Node Class)

节点类可以用于实现链表数据结构。以下是一个简单的节点类的示例。

#include <iostream>

template<typename T>
class Node {
public:
    T data;
    Node<T>* next;

    Node(T data) : data(data), next(nullptr) {}
};

int main() {
    Node<int>* node1 = new Node<int>(1);
    Node<int>* node2 = new Node<int>(2);
    node1->next = node2;

    std::cout << "Node 1 data: " << node1->data << std::endl;
    std::cout << "Node 2 data: " << node1->next->data << std::endl;

    delete node1;
    delete node2;
    return 0;
}

5.支持类 (Support Class)

支持类可以包含一些辅助函数,例如数学计算。以下是一个支持类的示例,用于计算阶乘。

#include <iostream>

class MathUtils {
public:
    static int factorial(int n) {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }
};

int main() {
    int result = MathUtils::factorial(5);
    std::cout << "Factorial of 5: " << result << std::endl;
    return 0;
}

6.域类 (Domain Class)

域类用于表示特定领域中的实体或概念。例如,我们可以创建一个域类Employee来表示公司中的雇员。

#include <iostream>
#include <string>

class Employee {
private:
    std::string name;
    int employeeId;
public:
    Employee(std::string name, int employeeId) : name(name), employeeId(employeeId) {}

    void display() const {
        std::cout << "Name: " << name << ", Employee ID: " << employeeId << std::endl;
    }
};

int main() {
    Employee emp("John Doe", 12345);
    emp.display();
    return 0;
}

7.应用类 (Utility Class)

应用类可以提供一组通用的功能或工具函数。以下是一个简单的应用类StringUtils,用于反转字符串。

#include <iostream>
#include <string>

class StringUtils {
public:
    static std::string reverseString(const std::string& str) {
        std::string reversedStr = str;
        std::reverse(reversedStr.begin(), reversedStr.end());
        return reversedStr;
    }
};

int main() {
    std::string original = "hello";
    std::string reversed = StringUtils::reverseString(original);
    std::cout << "Reversed string: " << reversed << std::endl;
    return 0;
}

8.集合和容器类 (Collection and Container Class)

集合和容器类用于存储和管理多个元素的集合。例如,std::vector是C++标准库中的一个容器类,用于存储动态数组。

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    std::cout << "Elements in the vector:";
    for (int num : numbers) {
        std::cout << " " << num;
    }
    std::cout << std::endl;
    return 0;
}
责任编辑:赵宁宁 来源: AI让生活更美好
相关推荐

2021-08-02 10:46:02

云计算用途

2011-07-14 17:45:06

CC++

2010-02-01 10:22:51

C++数据指针

2010-01-28 13:45:06

C++数组

2024-02-20 14:55:51

2010-01-28 16:31:54

C++类型

2024-04-03 12:30:00

C++开发

2010-01-21 13:33:44

C++基类

2011-03-14 10:46:03

2010-10-13 15:33:38

MySQL日志

2021-10-19 14:04:28

C++类型数字

2020-12-29 10:28:29

物联网安全物联网IoT

2010-01-28 13:27:12

C++类定义

2010-01-20 09:54:27

C++数据类型

2010-01-15 18:35:25

C++的类

2024-02-21 14:55:19

C++语言编程

2023-10-28 16:25:17

滤波C++

2023-03-01 15:39:50

JavaScrip对象属性ES6

2023-06-05 07:14:25

2023-05-28 23:49:38

JavaScrip开发
点赞
收藏

51CTO技术栈公众号