日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

C++ 八種常見類類型

來(lái)源: 責(zé)編: 時(shí)間:2024-04-02 17:18:09 204觀看
導(dǎo)讀大部分面向?qū)ο箝_發(fā)工作中都應(yīng)用了以下部分或者全部的基本類別的類,每種都有其特定的用途和特征。1.具體類 (Concrete Class)我們可以創(chuàng)建一個(gè)具體類來(lái)表示汽車。具體類Car可能會(huì)包含成員變量如brand(品牌)、model(型號(hào))和

大部分面向?qū)ο箝_發(fā)工作中都應(yīng)用了以下部分或者全部的基本類別的類,每種都有其特定的用途和特征。uV628資訊網(wǎng)——每日最新資訊28at.com

uV628資訊網(wǎng)——每日最新資訊28at.com

1.具體類 (Concrete Class)

我們可以創(chuàng)建一個(gè)具體類來(lái)表示汽車。具體類Car可能會(huì)包含成員變量如brand(品牌)、model(型號(hào))和成員函數(shù)如start()(啟動(dòng))、accelerate()(加速)等。uV628資訊網(wǎng)——每日最新資訊28at.com

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

uV628資訊網(wǎng)——每日最新資訊28at.com

2.抽象類 (Abstract Class)

我們可以創(chuàng)建一個(gè)抽象類Shape來(lái)表示形狀,其中包含一個(gè)純虛函數(shù)calculateArea()用于計(jì)算面積。uV628資訊網(wǎng)——每日最新資訊28at.com

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

uV628資訊網(wǎng)——每日最新資訊28at.com

3.接口類 (Interface Class)

接口類可以用來(lái)定義一組接口,例如Drawable接口可以定義繪制圖形的方法。uV628資訊網(wǎng)——每日最新資訊28at.com

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

uV628資訊網(wǎng)——每日最新資訊28at.com

4.節(jié)點(diǎn)類 (Node Class)

節(jié)點(diǎn)類可以用于實(shí)現(xiàn)鏈表數(shù)據(jù)結(jié)構(gòu)。以下是一個(gè)簡(jiǎn)單的節(jié)點(diǎn)類的示例。uV628資訊網(wǎng)——每日最新資訊28at.com

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

支持類可以包含一些輔助函數(shù),例如數(shù)學(xué)計(jì)算。以下是一個(gè)支持類的示例,用于計(jì)算階乘。uV628資訊網(wǎng)——每日最新資訊28at.com

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

域類用于表示特定領(lǐng)域中的實(shí)體或概念。例如,我們可以創(chuàng)建一個(gè)域類Employee來(lái)表示公司中的雇員。uV628資訊網(wǎng)——每日最新資訊28at.com

#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.應(yīng)用類 (Utility Class)

應(yīng)用類可以提供一組通用的功能或工具函數(shù)。以下是一個(gè)簡(jiǎn)單的應(yīng)用類StringUtils,用于反轉(zhuǎn)字符串。uV628資訊網(wǎng)——每日最新資訊28at.com

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

集合和容器類用于存儲(chǔ)和管理多個(gè)元素的集合。例如,std::vector是C++標(biāo)準(zhǔn)庫(kù)中的一個(gè)容器類,用于存儲(chǔ)動(dòng)態(tài)數(shù)組。uV628資訊網(wǎng)——每日最新資訊28at.com

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

本文鏈接:http://www.www897cc.com/showinfo-26-80835-0.htmlC++ 八種常見類類型

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 一文理解Python的全局解釋器鎖(GIL)

下一篇: 決勝分布式:揭秘Spring框架@Retry注解的智慧重試藝術(shù)

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 错那县| 襄樊市| 南阳市| 南昌市| 八宿县| 繁昌县| 内黄县| 花莲市| 蓬安县| 荆州市| 余庆县| 五峰| 沾化县| 长兴县| 荔波县| 南部县| 曲水县| 万全县| 宾川县| 忻城县| 方山县| 彰武县| 江华| 肃南| 湖北省| 乌海市| 神池县| 固镇县| 板桥市| 翁源县| 凤阳县| 金塔县| 徐水县| 穆棱市| 依安县| 临潭县| 永康市| 滕州市| 九江县| 奉节县| 福泉市|