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;
}