C與C++的不同點
- C++在struct,union中定義的函式叫成員函式,在class中定義的資料叫資料成員
- C++引入了三個存取權限的關鍵字:public,protected,private
public:表示資料成員或成員函式是公有的
protected:表示資料是受保護的,不允許其他的程式對其進行直接存取,只能通過他所在的成員函式進行存取
private:表示資料是私有的,與protected一樣
成員訪問控制 public protected private 類自身 yes yes yes 派生類 yes yes no 其他類 yes no no
注意:private與protected的區別:private的資料是不可被繼承的;protected的資料是可以被繼承的基類成員訪問控制 繼承訪問控制 在派生類中的訪問控制 publilc public public protected protected private 不可訪問 publilc protected protected protected protected private 不可訪問 publilc private private protected private private 不可訪問 - C++中可以把結構體名當做資料型別來使用
struct Student{char name[20];} Student stu;//定義Student型別的變數
- struct關鍵字定義的資料成員或資料函式預設時是public;class關鍵字定義的資料成員或成員函式預設是private,用union定義的類其所有的成員都是public,這種安排是不可以改變的,它不能使用存取限定符來進行修飾資料權限
- C++中定義引數可以帶預設引數,帶預設引數的函式呼叫時,如果預設引數中沒有給值,則將預設值賦值給引數變數
//定義了預設引數的函式 int fun(int x,int y,int z = 100);
注意:帶有預設值的引數必須放在引數串列的尾部
作用域限定符
代碼:
View Code

C++繼承
概念:如果B類是由A類繼承而來的,那么把B類叫作派生類,A類叫作基類
單繼承
單繼承語法:
class 派生類名 : 繼承方式{ //定義類物體 }
#include<iostream> using namespace std; //基類 class A{ private: int x; int y; public: void setX(int x){ this->x = x; } void setY(int y){ this->y = y; } int getX(){ return x; } int getY(){ return y; } }; //派生類 class B:public A{ private: int z; public: void setZ(int z){ this->z = z; } int getZ(){ return z; } }; int main(){ B b; b.setX(100); b.setY(200); b.setZ(300); cout << b.getX() << "\n"; cout << b.getY() << "\n"; cout << b.getZ() << "\n"; }View Code
示例圖:

多繼承
多繼承語法:
class 派生類名 : 繼承方式1 基類名1,繼承方式2 基類名2,...{ //定義類物體 };
#include<iostream> using namespace std; //基類1 class A{ protected: int x; public: void setX(int x){ this->x = x; } int getX(){ return x; } }; //基類2 class B{ protected: int y; public: int getY(){ return y; } void setY(int y){ this->y = y; } }; //派生類 class C:public A,public B{ protected: int z; public: void setZ(int z){ this->z = z; } int getZ(){ return z; } }; int main(){ C c; c.setX(100); c.setY(200); c.setZ(300); cout << c.getX() << "\n"; cout << c.getY() << "\n"; cout << c.getZ() << "\n"; }View Code
示例圖:

C++多載
多載運算子
概念:對已有的運算子進行重新定義其功能
| 可多載運算子 | 不可多載運算子 |
| 雙目運算子(+,-,*,/,%) | .(成員訪問運算子) |
| 關系運算子(==,!=,>,<,>=,<=) | .* , ->*(成員指標訪問運算子) |
| 邏輯運算子(||,&&,!) | ::(域運算子) |
| 單目運算子(+(正),-(負),*,&) | sizeof |
| 自增(++),自減(--) | ?: |
| 位運算子(|,&,~,^,<<,>>) | #(預處理符號) |
| 賦值運算子(=,+=,-=,*=,/=,&=,|=,...) | |
| 空間申請與釋放(new,delete,new[],delete[]) | |
| 其他運算子(()(函式呼叫),->(成員訪問),,(逗號),[](下標)) |
#include <iostream> using namespace std; class Box{ private: int width; int height; public: Box(){ width = 0; height = 0; } Box(int width,int height){ this->width = width; this->height = height; } //多載 + 運算子 Box operator + (const Box &b){ Box box; box.width = this->width + b.width; box.height = this->height + b.height; return box; } //多載輸出流 friend ostream &operator<<( ostream &os,const Box &b ){ os << "width:" << b.width << " height:" << b.height << "\n"; return os; } //多載輸入流 friend istream &operator>>(istream &is,Box &b){ is >> b.width >> b.height; return is; } }; int main(){ Box b1(100,200),b2(300,200); Box b3 = b1+b2; //cin >> b3; cout << b3 << endl; return 0; }View Code
示例圖:
多載函式
概念:在一個類中定義多個相同名字的函式,但是引數串列或引數個數不相同的方法
#include <iostream> #include <cstdio> using namespace std; class A{ public: void print(int x){ printf("x=%d\n",x); } void print(int x,int y){ printf("x=%d,y=%d\n",x,y); } void print(double z){ printf("z=%lf\n",z); } }; int main(){ A a; a.print(100); a.print(200,300); a.print(666.6); return 0; }View Code
示例圖:

C++成員函式
成員函式的實作:在類定義的內部或外部實作函式,前者稱為行內成員函式,后者稱為外部成員函式
內部實作:
class A{ private: int x; int y; public:
//定義行內函式 int getX(){return x;} int getY(){return y;} }
外部實作:
class B{ protected: int x; public: void setX(int x); int getX(); } //外部實作 void B::setX(int x){ this->x = x; } int B::getX(){ return this->x; }
C++建構式與解構式
概念:建構式是在建立物件時被呼叫的;解構式是在洗掉物件前被呼叫的
內部定義:
#include<iostream> using namespace std; class Person{ protected: string name; public: //定義無參建構式 Person(){ cout << "Person is being created\n"; name = "Lam"; } //定義有參建構式 Person(string name){ cout << "Person is being created\n"; this->name = name; } //定義解構式 ~Person(){ cout << "Person is being deleted\n"; } }; int main(){ Person p;//呼叫無參建構式 cout << "Next1\n"; Person p2("Lam");//呼叫有參建構式 cout << "Next2\n"; }View Code
外部定義:
#include<iostream> using namespace std; class Person{ protected: string name; public: //定義無參建構式 Person(); //定義有參建構式 Person(string name); //定義解構式 ~Person(); }; Person::Person(){ cout << "Person is being created\n"; name = "Lam"; } Person::Person(string name){ cout << "Person is being created\n"; this->name = name; } Person::~Person(){ cout << "Person is being deleted\n"; } int main(){ Person p;//呼叫無參建構式 cout << "Next1\n"; Person p2("Lam");//呼叫有參建構式 cout << "Next2\n"; }View Code
示例圖:

呼叫基(父)類函式
問題:如果想在派生類中使用基類中的函式(這里的函式是指與派生類的函式名相同,但是是功能不同的函式)我們該怎么做呢??
代碼:
#include <iostream> using namespace std; /** 派生型別別變數.基類類名::參考的函式(); */ class A{ public: A(){ cout<<"execution function A()\n"; } void print(){ cout << "print A\n"; } }; class B:public A{ public: B(){ cout << "execution function B()\n"; } void print(){ cout << "print B\n"; } }; int main(){ B b; b.print();//print B ((A)b).print();//print A b.A::print();//print A return 0; }View Code
示例圖:

派生類的建構式實作及執行程序
代碼:
#include <iostream> using namespace std; class A{ public: int x; public: A(){ cout<<"execution function A()\n"; x = 1; } A(int x){ //不會呼叫無參構造方法A() this->x = x; cout << "x:" << x << endl; } }; class B:public A{ public: int y; public: B(){ //隱式呼叫父類無參建構式A() cout << "execution function B()\n"; y = 1; } B(int x,int y):A(x){//派生類建構式實作 //這里因為呼叫了A(x)建構式,所以不會在隱式呼叫父類無參建構式A() this->y = y; cout << "y:" << y << endl; } }; int main(){ cout << "=========A()============\n"; A a(200); cout << a.x << endl; cout << "=========B()============\n"; B b1; cout << b1.x << endl; cout << b1.y << endl; cout << "=========B(int x,int y)========\n"; B b2(100,200);//首先呼叫B類中的有參構造方法B(int y),然后有參構造方法中隱式呼叫父類無參構造方法 cout << b2.x << endl; cout << b2.y << endl; return 0; }View Code
示例圖:

C++建構式、解構式與普通函式的區別
- 建構式的名字必須與類名相同;解構式的名字是以波浪號(~)開頭加類名組成的,
- 當建立物件時或復制物件時,建構式會被自動呼叫;當洗掉物件前,解構式會被自動呼叫,
- 建構式與建構式都無回傳值,
- 建構式與解構式都無法被繼承,但是派生類可以呼叫基類的建構式
- 建構式允許多載,有自己的默認引數和初始化串列
- 建構式不可以是虛函式;解構式可以是虛函式
- 解構式不接受任何引數,沒有回傳值,沒有任何值要說明
C++動態物件
概念:在C++ 中提供了動態申請和釋放記憶體空間的兩個關鍵字,分別是new(申請),delete(釋放),
語法:
new 資料型別; delete 指標變數;
注意:new申請記憶體地址空間成功時,回傳物件的指標;若申請失敗,則回傳空指標(NULL)
代碼:
#include <iostream> #define N 100 #define row 4 #define col 6 using namespace std; class A{ public: A(){ cout << "execution function A()" << endl; } ~A(){ cout << "execution function ~A()" << endl; } }; int main(){ //new的使用 //1.基本型別的動態記憶體分配 double *p = new double; *p = 10000.999; //2.陣列的動態記憶體分配(一維陣列) char *c = new char[N]; //3.陣列的動態記憶體分配(4*6的二維陣列) int **b = new int*[row]; for(int i = 0; i < N; i++){ b[i] = new int[col]; } //4.物件的動態記憶體分配 A *a = new A(); A *arr = new A[N]; //delete的使用 //1.釋放指標p的記憶體 delete p; //2.釋放指標ch所指向的陣列 delete [] c; //3.釋放二級指標b所指向的陣列 for(int i = 0; i < 4; i++){ delete [] b[i]; } delete[] b; return 0; }View Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71505.html
標籤:C++
上一篇:涼肝的比賽
下一篇:BFS和佇列

