C++手冊全面詳解

一、基礎語法

C++是一種基於對象的編程語言,基礎語法主要包括數據類型、控制語句、函數和指針等。

1. 數據類型:

#include <iostream>
using namespace std;

int main() {
   int i = 10;
   float f = 10.5;
   double d = 10.56789;
   char c = 'A';
   bool b = true;

   cout << "Value of i : " << i << endl;
   cout << "Value of f : " << f << endl;
   cout << "Value of d : " << d << endl;
   cout << "Value of c : " << c << endl;
   cout << "Value of b : " << b << endl;

   return 0;
}

2. 控制語句:

#include <iostream>
using namespace std;

int main() {
   int a = 2, b = 3;

   if(a > b) {
      cout << "a is greater than b" << endl;
   } else {
      cout << "b is greater than a" << endl;
   }

   for(int i = 0; i < 5; i++) {
      cout << "The value of i is: " << i << endl;
   }

   return 0;
}

3. 函數:

#include <iostream>
using namespace std;

int add(int x, int y) {
   return x + y;
}

int main() {
   int a = 5, b = 3, c;

   c = add(a, b);

   cout << "The sum of a and b is: " << c << endl;

   return 0;
}

4. 指針:

#include <iostream>
using namespace std;

void swap(int *x, int *y) {
   int temp = *x;
   *x = *y;
   *y = temp;
}

int main() {
   int a = 5, b = 3;

   cout << "Before swapping: a = " << a << " and b = " << b << endl;

   swap(&a, &b);

   cout << "After swapping: a = " << a << " and b = " << b << endl;

   return 0;
}

二、面向對象

C++是一種面向對象的編程語言,實現面向對象編程需要掌握類、繼承、多態等概念。

1. 類:

#include <iostream>
using namespace std;

class Rectangle {
   public:
      int length;
      int width;

      int getArea() {
         return length * width;
      }
};

int main() {
   Rectangle rectangle;

   rectangle.length = 5;
   rectangle.width = 3;

   cout << "The area of rectangle is: " << rectangle.getArea() << endl;

   return 0;
}

2. 繼承:

#include <iostream>
using namespace std;

class Shape {
   public:
      virtual int getArea() = 0;
};

class Rectangle : public Shape {
   public:
      int length;
      int width;

      int getArea() {
         return length * width;
      }
};

int main() {
   Rectangle rectangle;

   rectangle.length = 5;
   rectangle.width = 3;

   cout << "The area of rectangle is: " << rectangle.getArea() << endl;

   return 0;
}

3. 多態:

#include <iostream>
using namespace std;

class Shape {
   public:
      virtual int getArea() = 0;
};

class Rectangle : public Shape {
   public:
      int length;
      int width;

      int getArea() {
         return length * width;
      }
};

class Circle : public Shape {
   public:
      int radius;

      int getArea() {
         return 3.14 * radius * radius;
      }
};

int main() {
   Shape *shape;
   Rectangle rectangle;
   Circle circle;

   shape = &rectangle;
   rectangle.length = 5;
   rectangle.width = 3;

   cout << "The area of rectangle is: " <getArea() << endl;

   shape = &circle;
   circle.radius = 5;

   cout << "The area of circle is: " <getArea() << endl;

   return 0;
}

三、異常處理

C++提供了異常處理機制,異常是程序執行期間可能出現的意外情況。

1. catch和throw語句:

#include <iostream>
using namespace std;

int division(int a, int b) {
   if(b == 0) {
      throw "Division by zero!";
   }

   return a / b;
}

int main() {
   int a = 10, b = 0;

   try {
      int c = division(a, b);
      cout << "The result of division is: " << c << endl;
   } catch(const char* msg) {
      cerr << msg << endl;
   }

   return 0;
}

2. 自定義異常:

#include <iostream>
using namespace std;

class MyException : public exception {
   public:
      const char* what() const throw() {
         return "My Exception Occurred";
      }
};

int division(int a, int b) {
   if(b == 0) {
      throw MyException();
   }

   return a / b;
}

int main() {
   int a = 10, b = 0;

   try {
      int c = division(a, b);
      cout << "The result of division is: " << c << endl;
   } catch(exception& e) {
      cout << e.what() << endl;
   }

   return 0;
}

四、STL庫

C++的STL庫為程序員提供了很多實用的數據結構和演算法。

1. vector容器:

#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> v;

   for(int i = 0; i < 5; i++) {
      v.push_back(i);
   }

   cout << "The content of vector is: ";
   for(int i = 0; i < v.size(); i++) {
      cout << v[i] << " ";
   }

   return 0;
}

2. map容器:

#include <iostream>
#include <map>
using namespace std;

int main() {
   map<string, int> m;

   m["apple"] = 5;
   m["banana"] = 3;
   m["cherry"] = 8;

   cout << "The price of apple is: " << m["apple"] << endl;

   return 0;
}

五、文件處理

C++可以用於文件讀寫操作。

1. 寫文件:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
   ofstream file("example.txt");

   if(file.is_open()) {
      file << "Hello World!\n";
      file << "The value of pi is: " << 3.14 << endl;
      file.close();
   } else {
      cout << "Unable to open file for writing!" << endl;
   }

   return 0;
}

2. 讀文件:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
   string line;
   ifstream file("example.txt");

   if(file.is_open()) {
      while(getline(file, line)) {
         cout << line << endl;
      }

      file.close();
   } else {
      cout << "Unable to open file for reading!" << endl;
   }

   return 0;
}

以上就是C++手冊的全面詳解,包括基礎語法、面向對象、異常處理、STL庫和文件處理等方面。通過學習這些基本知識,你可以編寫出自己的C++程序,從而實現各種應用。

原創文章,作者:EUTOV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/370869.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
EUTOV的頭像EUTOV
上一篇 2025-04-23 00:48
下一篇 2025-04-23 00:48

相關推薦

  • Python應用程序的全面指南

    Python是一種功能強大而簡單易學的編程語言,適用於多種應用場景。本篇文章將從多個方面介紹Python如何應用於開發應用程序。 一、Web應用程序 目前,基於Python的Web…

    編程 2025-04-29
  • Python zscore函數全面解析

    本文將介紹什麼是zscore函數,它在數據分析中的作用以及如何使用Python實現zscore函數,為讀者提供全面的指導。 一、zscore函數的概念 zscore函數是一種用於標…

    編程 2025-04-29
  • 全面解讀數據屬性r/w

    數據屬性r/w是指數據屬性的可讀/可寫性,它在程序設計中扮演著非常重要的角色。下面我們從多個方面對數據屬性r/w進行詳細的闡述。 一、r/w的概念 數據屬性r/w即指數據屬性的可讀…

    編程 2025-04-29
  • Python計算機程序代碼全面介紹

    本文將從多個方面對Python計算機程序代碼進行詳細介紹,包括基礎語法、數據類型、控制語句、函數、模塊及面向對象編程等。 一、基礎語法 Python是一種解釋型、面向對象、動態數據…

    編程 2025-04-29
  • Matlab二值圖像全面解析

    本文將全面介紹Matlab二值圖像的相關知識,包括二值圖像的基本原理、如何對二值圖像進行處理、如何從二值圖像中提取信息等等。通過本文的學習,你將能夠掌握Matlab二值圖像的基本操…

    編程 2025-04-28
  • 瘋狂Python講義的全面掌握與實踐

    本文將從多個方面對瘋狂Python講義進行詳細的闡述,幫助讀者全面了解Python編程,掌握瘋狂Python講義的實現方法。 一、Python基礎語法 Python基礎語法是學習P…

    編程 2025-04-28
  • 全面解析Python中的Variable

    Variable是Python中常見的一個概念,是我們在編程中經常用到的一個變數類型。Python是一門強類型語言,即每個變數都有一個對應的類型,不能無限制地進行類型間轉換。在本篇…

    編程 2025-04-28
  • Zookeeper ACL 用戶 anyone 全面解析

    本文將從以下幾個方面對Zookeeper ACL中的用戶anyone進行全面的解析,並為讀者提供相關的示例代碼。 一、anyone 的作用是什麼? 在Zookeeper中,anyo…

    編程 2025-04-28
  • Python合集符號全面解析

    Python是一門非常流行的編程語言,在其語法中有一些特殊的符號被稱作合集符號,這些符號在Python中起到非常重要的作用。本文將從多個方面對Python合集符號進行詳細闡述,幫助…

    編程 2025-04-28
  • Switchlight的全面解析

    Switchlight是一個高效的輕量級Web框架,為開發者提供了簡單易用的API和豐富的工具,可以快速構建Web應用程序。在本文中,我們將從多個方面闡述Switchlight的特…

    編程 2025-04-28

發表回復

登錄後才能評論