天天看點

C++經典文法

cin ,cout分别對應C語言中的scanf,printf,但是比scanf,printf靈活,可以自動的去比對輸出格式。

cin要與>> 一起用,cout要與<< 一起用。

endl 相當于C中的"/n"

cerr是C++ 的标準錯誤輸出函數。

#include "stdafx.h"

#include <iostream>

using namespace std;

struct Point

{

    int x;

    int y;

};

void main()

{

    Point pt;

    pt.x = 5;

    pt.y = 5;

    cout<< pt.x << endl<< pt.y<<endl;

}

#include "stdafx.h"

#include <iostream>

using namespace std;

struct Point

{

    int x;

    int y;

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

};

void main()

{

    Point pt;

    pt.x = 5;

    pt.y = 5;

    pt.output();

}

調用結構體本身的函數去列印結構體的成員, 在C語言中結構體中不允許有函數,C++中是可以的。

#include "stdafx.h"

#include <iostream>

using namespace std;

//struct Point

class Point

{

public:

    int x;

    int y;

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

}; //這裡要結束符

void main()

{

    Point pt;

    pt.x = 5;

    pt.y = 5;

    pt.output();

    //cout<< pt.x << endl<< pt.y<<endl;

}

C++中的結構體跟類是可以通用的,差別就是通路的修飾符上。在結構體中所有的成員的預設修飾符是public ,但是類的成員預設修飾符是private.

public:所有的成員在外部可以通路。

private:類内部的成員才可以通路。

protected:子類,父類之間的通路。

#include "stdafx.h"

#include <iostream>

using namespace std;

//struct Point

class Point

{

public:

    int x;

    int y;

    void init()

    {

        x = 0;

        y = 0;

    }

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

};

void main()

{

    Point pt;

    pt.init() ;

    pt.output();

}

構造函數的引入:

1. 構造函數最重要的作用是建立對象本身。

2.C++ 規定,每個類都必須有一個構造函數,沒有構造函數,就不能建立任何對象。

3.隻要類定義了一個構造函數,不管是否帶參數,C++就不在提供預設的構造函數了。還想引用無參的構造函數要自己定義。

#include "stdafx.h"

#include <iostream>

using namespace std;

class Point

{

public:

    int x;

    int y;

    //構1造¨¬函¡¥數ºy,ê?類¤¨¤名?為a函¡¥數ºy名?,ê?無T返¤¦Ì回?值¦Ì

    Point(){

        x = 0;

        y = 0;

    }

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

};

void main()

{

    Point pt;

    //pt.init() ;

    //pt.x = 5;

    //pt.y = 5;

    //pt.output();

    cin >> pt.x;

    //cout<< pt.x << endl<< pt.y<<endl;

}

析構函數的引入:當一個對象的生命周期結束時,調用析構函數來釋放對象的記憶體。

1.當一個對象生命周期結束時,其所占有的記憶體空間就要被回收,這個工作由析構函數來完成。

2.析構函數是“反向”的構造函數,析構函數不允許有傳回值。且不允許帶參數。并且一個類中隻能有一個析構函數。

3. 根據析構函數的這種特點,我們可以在構造函數中初始化對象的某些成員變量,給其配置設定記憶體空間(堆記憶體),在析構函數中釋放對象運作期間所申請的資源。

#include "stdafx.h"

#include <iostream>

using namespace std;

//struct Point

class Point

{

public:

    int x;

    int y;

    //構1造¨¬函¡¥數ºy,ê?類¤¨¤名?為a函¡¥數ºy名?,ê?無T返¤¦Ì回?值¦Ì

    Point(){

        x = 0;

        y = 0;

    }

    ~Point()

    {

    }

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

};

void main()

{

    Point pt;

    //pt.init() ;

    //pt.x = 5;

    //pt.y = 5;

    pt.output();

    //cout<< pt.x << endl<< pt.y<<endl;

}

函數的重載:

重載的說明:

(1) void output();

(2) int output();

不能構成重載,原因:編譯器無法确定去調用那個函數。

(1) void output(int a, int b = 5);

(2) void output(int a );

不能構成重載,原因:編譯器無法确定去調用那個函數。

#include "stdafx.h"

#include <iostream>

using namespace std;

//struct Point

class Point

{

public:

    int x;

    int y;

    //構1造¨¬函¡¥數ºy,ê?類¤¨¤名?為a函¡¥數ºy名?,ê?無T返¤¦Ì回?值¦Ì

    Point(){

        x = 0;

        y = 0;

    }

    Point(int a, int b){

    x = a;

    y = b;

    }

    ~Point()

    {

    }

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

    void output(int x ,int y){

    x = x;

    y = y;

    }

};

void main()

{

    Point pt(3,3);

    //pt.init() ;

    //pt.x = 5;

    //pt.y = 5;

    pt.output(5,5);//此ä?時º¡À隻?是º?給?形?參?賦3值¦Ì,ê?pt對?象¨®的Ì?成¨¦員¡Àx, y并¡é沒?得Ì?到Ì?這a個?5,5.

    pt.output();

    //cout<< pt.x << endl<< pt.y<<endl;

}

//打䨰印®?的Ì?結¨¢果?:êo3  3

this指針的引入:

1.this指針是一個隐含的指針,它指向對象本身,代表了對象的位址。

2.一個類所有的對象調用的成員函數都是同一代碼段。但是在對象調用pt.output(6,6)時,成員函數除了接收到了一個對象s的位址。這個位址被一個隐含的形參this 指針所擷取。它等同于執行this = &pt。所有對資料成員的通路都被隐含的加上了字首this -> 。如:x = 0;等價于:this->x = 0;

#include "stdafx.h"

#include <iostream>

using namespace std;

//struct Point

class Point

{

public:

    int x;

    int y;

    //構1造¨¬函¡¥數ºy,ê?類¤¨¤名?為a函¡¥數ºy名?,ê?無T返¤¦Ì回?值¦Ì

    Point(){

        x = 0;

        y = 0;

    }

    Point(int a, int b){

    x = a;

    y = b;

    }

    ~Point()

    {

    }

    void output()

    {

        cout<< x << endl<< y<<endl;

    }

    //不?換?形?參?時º¡À,ê?改?變À?成¨¦員¡À變À?量¢?x, y的Ì?值¦Ì

    void output(int x ,int y){

    this->x = x;

    this->y = y;

    }

};

void main()

{

    Point pt(3,3);

    //pt.init() ;

    //pt.x = 5;

    //pt.y = 5;

    pt.output(5,5);//此ä?時º¡À隻?是º?給?形?參?賦3值¦Ì,ê?pt對?象¨®的Ì?成¨¦員¡Àx, y并¡é沒?得Ì?到Ì?這a個?5,5.

    pt.output();

    //cout<< pt.x << endl<< pt.y<<endl;

}

//打䨰印®?的Ì?結¨¢果?:êo5  5

繼續閱讀