聲明一個類,則類就是一個類型,類不能直接訪問其屬性,這是與Python最大區別ios
#include "pch.h" #include <iostream> using namespace std; struct Book { char title[40]; char author[40]; char subject[40]; int book_id; }; class Box { public: //聲明公共屬性 double length; double height ; double weight = 30; void setlength(double let); //聲明函數不定義 void setheight(double het) //聲明函數並定義 { height = het; } double getvolumn(void) { return length * height * weight; } }; void Box::setlength(double let) //使用 :: 範圍解析符定義函數 { length = let; } int main() { double v; Box my_first_box; my_first_box.setheight(10); my_first_box.setlength(10); v = my_first_box.getvolumn(); cout << v << endl; }
class Base { public: // 公有成員 類與實例、繼承類都能訪問 protected: // 受保護成員 實例不能直接訪問,可經過公有函數調用,可是派生類能訪問 private: // 私有成員 實例不能直接訪問,但能經過公有函數調用,派生類不能訪問 };
派生類能夠訪問基類中全部的非私有成員。所以基類成員若是不想被派生類的成員函數訪問,則應在基類中聲明爲 private。框架
咱們能夠根據訪問權限總結出不一樣的訪問類型,以下所示:ide
訪問 | public | protected | private |
---|---|---|---|
同一個類 | yes | yes | yes |
派生類 | yes | yes | no |
外部的類 | yes | no | no |
當一個類派生自基類,該基類能夠被繼承爲 public、protected 或 private 幾種類型。繼承類型是經過上面講解的訪問修飾符 access-specifier 來指定的。函數
咱們幾乎不使用 protected 或 private 繼承,一般使用 public 繼承。當使用不一樣類型的繼承時,遵循如下幾個規則:spa
在同一個做用域內,能夠聲明幾個功能相似的同名函數,可是這些同名函數的形式參數(指參數的個數、類型或者順序)必須不一樣。您不能僅經過返回類型的不一樣來重載函數。code
下面的實例中,同名函數 print() 被用於輸出不一樣的數據類型:對象
#include <iostream> using namespace std; class printData { public:
//--------------------------------------------三個print同名函數,但參數不一樣則是不一樣的函數 void print(int i) { cout << "整數爲: " << i << endl; } void print(double f) { cout << "浮點數爲: " << f << endl; } void print(char c[]) { cout << "字符串爲: " << c << endl; } }; int main(void) { printData pd; // 輸出整數 pd.print(5); // 輸出浮點數 pd.print(500.263); // 輸出字符串 char c[] = "Hello C++"; pd.print(c); return 0; }
接口描述了類的行爲和功能,而不須要完成類的特定實現。blog
C++ 接口是使用抽象類來實現的,抽象類與數據抽象互不混淆,數據抽象是一個把實現細節與相關的數據分離開的概念。繼承
若是類中至少有一個函數被聲明爲純虛函數,則這個類就是抽象類。純虛函數是經過在聲明中使用 "= 0" 來指定的,以下所示:接口
class Box { public: // 有純虛函數則該類爲抽象類 virtual double getVolume() = 0; private: double length; // 長度 double breadth; // 寬度 double height; // 高度 };
以下實例:
#include <iostream> using namespace std; // 基類 class Shape { public: // 提供接口框架的純虛函數 virtual int getArea() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // 派生類 class Rectangle: public Shape { public: int getArea() { return (width * height); } }; class Triangle: public Shape { public: int getArea() { return (width * height)/2; } }; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // 輸出對象的面積 cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // 輸出對象的面積 cout << "Total Triangle area: " << Tri.getArea() << endl; return 0; }
#include<iostream>usingnamespacestd; // 基類classShape{public: // 提供接口框架的純虛函數virtualintgetArea() = 0; voidsetWidth(intw){width = w; }voidsetHeight(inth){height = h; }protected: intwidth; intheight; }; // 派生類classRectangle: publicShape{public: intgetArea(){return(width * height); }}; classTriangle: publicShape{public: intgetArea(){return(width * height)/2; }}; intmain(void){RectangleRect; TriangleTri; Rect.setWidth(5); Rect.setHeight(7); // 輸出對象的面積cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // 輸出對象的面積cout << "Total Triangle area: " << Tri.getArea() << endl; return0; }