AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

C++ 八種常見類類型

來源: 責編: 時間:2024-04-02 17:18:09 199觀看
導讀大部分面向對象開發工作中都應用了以下部分或者全部的基本類別的類,每種都有其特定的用途和特征。1.具體類 (Concrete Class)我們可以創建一個具體類來表示汽車。具體類Car可能會包含成員變量如brand(品牌)、model(型號)和

大部分面向對象開發工作中都應用了以下部分或者全部的基本類別的類,每種都有其特定的用途和特征。fTm28資訊網——每日最新資訊28at.com

fTm28資訊網——每日最新資訊28at.com

1.具體類 (Concrete Class)

我們可以創建一個具體類來表示汽車。具體類Car可能會包含成員變量如brand(品牌)、model(型號)和成員函數如start()(啟動)、accelerate()(加速)等。fTm28資訊網——每日最新資訊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;}

fTm28資訊網——每日最新資訊28at.com

2.抽象類 (Abstract Class)

我們可以創建一個抽象類Shape來表示形狀,其中包含一個純虛函數calculateArea()用于計算面積。fTm28資訊網——每日最新資訊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;}

fTm28資訊網——每日最新資訊28at.com

3.接口類 (Interface Class)

接口類可以用來定義一組接口,例如Drawable接口可以定義繪制圖形的方法。fTm28資訊網——每日最新資訊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;}

fTm28資訊網——每日最新資訊28at.com

4.節點類 (Node Class)

節點類可以用于實現鏈表數據結構。以下是一個簡單的節點類的示例。fTm28資訊網——每日最新資訊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)

支持類可以包含一些輔助函數,例如數學計算。以下是一個支持類的示例,用于計算階乘。fTm28資訊網——每日最新資訊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)

域類用于表示特定領域中的實體或概念。例如,我們可以創建一個域類Employee來表示公司中的雇員。fTm28資訊網——每日最新資訊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.應用類 (Utility Class)

應用類可以提供一組通用的功能或工具函數。以下是一個簡單的應用類StringUtils,用于反轉字符串。fTm28資訊網——每日最新資訊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)

集合和容器類用于存儲和管理多個元素的集合。例如,std::vector是C++標準庫中的一個容器類,用于存儲動態數組。fTm28資訊網——每日最新資訊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.tebozhan.com/showinfo-26-80835-0.htmlC++ 八種常見類類型

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

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

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

標簽:
  • 熱門焦點
  • 盧偉冰長文解析K60至尊版 對Redmi有著里程碑式的意義

    在今天的Redmi后性能時代戰略發布會結束之后,Redmi總經理盧偉冰又帶來了一篇長文,詳解了為什么 Redmi 要開啟后性能時代?為什么選擇和 MediaTek、Pixelworks 深度合作?以及后性
  • Mate60手機殼曝光 致敬自己的經典設計

    8月3日消息,今天下午博主數碼閑聊站帶來了華為Mate60的第三方手機殼圖,可以讓我們在真機發布之前看看這款華為全新旗艦的大致輪廓。從曝光的圖片看,Mate 60背后攝像頭面積依然
  • 俄羅斯:將審查iPhone等外國公司設備 保數據安全

    iPhone和特斯拉都屬于在各自領域領頭羊的品牌,推出的產品也也都是數一數二的,但對于一些國家而言,它們的產品可靠性和安全性還是在限制范圍內。近日,俄羅斯聯邦通信、信息技術
  • 6月安卓手機好評榜:魅族20 Pro蟬聯冠軍

    性能榜和性價比榜之后,我們來看最后的安卓手機好評榜,數據來源安兔兔評測,收集時間2023年6月1日至6月30日,僅限國內市場。第一名:魅族20 Pro好評率:95%5月份的時候魅族20 Pro就是
  • Golang 中的 io 包詳解:組合接口

    io.ReadWriter// ReadWriter is the interface that groups the basic Read and Write methods.type ReadWriter interface { Reader Writer}是對Reader和Writer接口的組合,
  • Python異步IO編程的進程/線程通信實現

    這篇文章再講3種方式,同時講4中進程間通信的方式一、 Python 中線程間通信的實現方式共享變量共享變量是多個線程可以共同訪問的變量。在Python中,可以使用threading模塊中的L
  • iQOO Neo8 Pro搶先上架:首發天璣9200+ 安卓性能之王

    經過了一段時間的密集爆料,昨日iQOO官方如期對外宣布:將于5月23日推出全新的iQOO Neo8系列新品,官方稱這是一款擁有旗艦級性能調校的作品。隨著發布時
  • 回歸OPPO兩年,一加贏了銷量,輸了品牌

    成為OPPO旗下主打性能的先鋒品牌后,一加屢創佳績。今年618期間,一加手機全渠道銷量同比增長362%,憑借一加 11、一加 Ace 2、一加 Ace 2V三款爆品,一加
  • “買真退假” 這種“羊毛”不能薅

    □ 法治日報 記者 王春   □ 本報通訊員 胡佳麗  2020年初,還在上大學的小東加入了一個大學生兼職QQ群。群主&ldquo;七王&rdquo;在群里介紹一些刷單賺
Top