C++面向對象--類

1、實例化的兩種方式

#include <iostream>

#include<stdlib.h>

 

using namespace std;

 

class lio

{

public:

    int a=3;

    int b=4;

    void aa()

    {

        cout<<a;

    }

    int bb()

    {

        return b;

    }

};

 

int main()

{

    //棧的方式來實現

    lio a;

    a.a=33;

    a.b=44;

    cout<<a.a<<a.bb();

 

    //堆的方式實現

    lio *b=new lio();

    if(b){

        b->a=333;

        b->b=222;

        cout<<b->a;

        b->aa();

        b->bb();

    }

    return 0;

}

2、String 類型 和js中的 Array一樣

#include<string>

int main()

{

    string a="qweqwe";

    cout<<a;

    //空值檢測

    cout<<a.empty()<<endl;

    //大小檢測

    cout<<a.size()<<endl;

    //返回第幾個

    cout<<a[3]<<endl;

    //鏈接

cout<<a+a;

//錯誤鏈接方式

//cout<<"hello"+<<"world";

    return 0;

}

//輸入檢測程序

int main()

{

    string name;

    cout<<"please input you name.."<<endl;

    cin>>name;

    if(name.empty()){

        cout<<"please input again..";

        system("pause");

        return 0;

    }else

    {

        cout<<name<<endl;

        cout<<name[0]<<name.size()<<name+"愛三妹";

    }

    return 0;

}

 

3、數據的封裝

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class lio

{

public:

    int a=3;

    int b=4;

    void setname(string m_strname)// c++形參裏邊要定義好數據類型

    {

        name=m_strname;

    }

    string getname()

    {

        return name;

    }

private:

    int age=22;

    string name;

};

 

int main()

{

    lio a;

    a.setname("lio");//注意加引號

cout<<a.getname();

delete a;//勿忘

Str=NULL;

    return 0;

}

4、類內定義和內聯函數與類外定義

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

//內聯函數

inline void lio(string name)

{

    cout<<name+"愛三妹"<<endl;

};

//類內定義

class li

{

public:

    int a=5;

    void san(string name)

    {

        cout<<"我是三妹"+name;

    };

    int ss(int num);

private:

    int b=2;

};

//類外定義

int li::ss(int num)

{

    return num;

};

int main()

{

    li asd;

    asd.ss(123);

    lio("lio");

    return 0;

}

把頭文件放在頭文件的文件夾內,用.h來命名,函數實現部分放到主函數文件的同一個文件夾內,再把頭文件 引進去 include,添加頭部以及命名空間即可

5、構造函數

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class lio

{

public:

    lio()

    {

        cout<<"start"<<endl;

    };

    lio(int a)

    {

        cout<<a;

    }

};

int main()

{

    lio a;

    lio b(12);

    return 0;

}//構造函數支持重載

構造函數有默認參數的構造函數叫做默認構造函數

6、初始化列表

7、拷貝構造函數

Student(const Student &stu){ }

及發生在對象=的時候,就會觸發拷貝構造函數

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class student

{

public:

    student()

    {

        cout<<"gou zao han shu";

    };

    student(const student &stu)

    {

        cout<<"kao bei gou zao han shu ";

    };

};

int main()

{

    student aa;

    student bb(aa);

    student cc=aa;

    return 0;

}

//拷貝構造函數沒有重載

8、析構函數

釋放掉對象裏的指針對象變量,釋放資源,不可能重載

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class student

{

public:

    student()

    {

        cout<<"gou zao han shu";

    };

    student(const student &stu)

    {

        cout<<"kao bei gou zao han shu ";

    };

    ~student()

    {

        cout<<"say goodbye"<<"\n";

        delete []p;

    }

private:

    char *p;

};

int main()

{

student aa;

student nn=aa;

    student *bb=new student();

    delete bb;

 

    return 0;

}


9、對象=成員變量(const  

 

 

 

成員函數

 

10、命名空間

using namespace std;//設置默認命名空間

namespace lio

{

int a=123;

}

cout<<lio::a<<endl;



11、待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章