C++基礎(chǔ)(類和構(gòu)造函數(shù))

C++基礎(chǔ)

類和構(gòu)造函數(shù)

  • 類:類是定義同一類所有對(duì)象得變量和方法的藍(lán)圖或原型。數(shù)據(jù)與算法的統(tǒng)一。

    • 成員
    • 成員函數(shù)
    • 保護(hù)數(shù)據(jù)
    • 封裝
  • 類和對(duì)象就像人類與人的關(guān)系

  • 對(duì)象的初始化

    • 建立對(duì)象,須有一個(gè)有意義的初始值
    • C++建立和初始化對(duì)象的過(guò)程專門由該類的構(gòu)造函數(shù)來(lái)完成。
    • 構(gòu)造函數(shù)給對(duì)象分配空間和初始化。
    • 如果一個(gè)類沒有專門定義構(gòu)造函數(shù),那么C++就僅僅創(chuàng)建對(duì)象而不做任何初始化
#include <iostream>
using namespace std;
class Person
{
    private:
        int id;
        int money;
    public:
        void show()
        {
            cout<<"id="<<id<<"money="<<money<<endl;
        }
        void set(int a,int b)
        {
            if(a<0)
                a=0;
            id=a;
            money=b;
        }
};
int main()
{
    Person a;
    Person b;
    a.set(1,100);
    a.show();
    b.set(2,300);
    b.show();
}
class Person
{
    private:
        int id;
        int money;
    public:
        void show();
        void set(int a,int b);
};
void Person::show()
{
    cout<<"id="<<id<<"money="<<money<<endl;
}
void Person::set(int a,int b)
{
    if(a<0)
        a=0;
    id=a;
    money=b;
}
  • 最終版
//main.cpp
#include "person.h"
int main()
{
    Person a;
    Person b;
    a.set(1,100);
    a.show();
    b.set(2,300);
    b.show();
}
//person.cpp
#include <iostream>
#include "person.h"
using namespace std;
void Person::show()
{
    cout<<"id="<<id<<"money="<<money<<endl;
}
void Person::set(int a,int b)
{
    if(a<0)
        a=0;
    id=a;
    money=b;
}
//person.h
#include "person.h"
class Person
{
    private:
        int id;
        int money;
    public:
        void show();
        void set(int a,int b);
};
  • class默認(rèn)私有。

  • 面向?qū)ο?/p>

    • 在面向?qū)ο笾校惴ㄅc數(shù)據(jù)結(jié)構(gòu)被捆綁為一類
  • 定義成員函數(shù)

    • 類中定義成員函數(shù)一般為頭連函數(shù),即沒有明確用。。。。。。。。。
  • 保護(hù)成員

    • 保護(hù)成員
      • 除了友元和子類的成員函數(shù)之外,從類的外部就不能對(duì)它們?cè)L問(wèn)
    • 私有成員
      • 從類的外部就不能對(duì)它們?cè)L問(wèn)
    • 公共成員
      • 公共成員在任何地方都可以被訪問(wèn)。
  • 不同域?qū)?yīng)不同的函數(shù)內(nèi)容

#include <iostream>
using namespace std;        
class person
{
    private:
        int id;
        int money;
    public:
        void show();
        void show(int a);
};
class test
{
    public:
        void show();
};
void person::show()
{
    cout<<"hello1"<<endl;
}
void person::show(int a)
{
    cout<<"hello2"<<endl;
}
void test::show()
{
    cout<<"hello3"<<endl;
}
void show()
{
    cout<<"hello4"<<endl;
}
int main()
{
    person a;
    test b;
    a.show();
    a.show(1);
    show();
    b.show();
}
  • 對(duì)象與域的調(diào)用方法
#include <iostream>
using namespace std;
class test
{
    public:
        void show()
        {
            cout<<"hello"<<endl;
        }
};
void lianxi1(test *p1)
{
    p1->show();
}
void lianxi2(test &p2)
{
    p2.show();
}
void lianxi3(test p3)
{
    p3.show();
}
int main()
{
    test t;
    lianxi1(&t);
    lianxi2(t);
    lianxi3(t);
    cout<<sizeof(test)<<endl;
}
#include <iostream>
using namespace std;        
class test
{
    private:
        int id;
    public:
        void setid(int i)
        {
            id=i;
        }
        void show()
        {
            cout<<"this"<<(this)<<endl;
            cout<<"id= "<<(id)<<endl;
        }       
};
int main()
{
    test t;
    t.setid(222);
    t.show();
    cout<<"&t="<<&t<<endl;

}
  • 類和struct的區(qū)別
    • 除關(guān)鍵字不同外(class,struct)的唯一區(qū)別是,結(jié)構(gòu)在默認(rèn)情況下的成員是公共的,而類在默認(rèn)情況下的成員是私有的。 在C++中,結(jié)構(gòu)是特殊的類。
  • 類的作用域
    • 一個(gè)類的所有成員位于這個(gè)類的作用域內(nèi)
    • 類作用域是類定義和相應(yīng)類成員定義范圍
  • 對(duì)象和類的聯(lián)系與區(qū)別

  • 類中訪問(wèn)控制的關(guān)鍵字有哪幾個(gè),分別是什么意義

  • 封裝理念
    • 封裝來(lái)源于信息隱藏的設(shè)計(jì)理念, 是通過(guò)特性和行為的組合來(lái)創(chuàng)建新數(shù)據(jù)類型讓接口與具體實(shí)現(xiàn)相隔離。
    • C++中是通過(guò)類來(lái)實(shí)現(xiàn)的, 為了盡量避免某個(gè)模塊的行為干擾同一系統(tǒng)中的其它模塊,應(yīng)該讓模塊僅僅公開必須讓外界知道的接口。
  • 封裝的優(yōu)點(diǎn)
    • 重用
    • 不必關(guān)心具體的體現(xiàn)
    • 具有安全性

構(gòu)造函數(shù)

  • 定義
    • C++的構(gòu)造函數(shù)和析構(gòu),使類對(duì)象能輕松的被賦值與撤銷。它們是一種特殊的函數(shù)。
    • 構(gòu)造函數(shù)是第一個(gè)自動(dòng)調(diào)用的函數(shù)
    • 構(gòu)造函數(shù)創(chuàng)建類對(duì)象,初始化其成員
    • 析構(gòu)函數(shù)撤銷類對(duì)象
    • 構(gòu)造函數(shù)和析構(gòu)函數(shù)是類的特殊成員函數(shù)
    • 與類同名
    • 析構(gòu)函數(shù)
    • 重載構(gòu)造函數(shù)
    • 默認(rèn)參數(shù)構(gòu)造函數(shù)
    • 對(duì)象創(chuàng)建過(guò)程
#include<iostream>
using namespace std;
class Fract
{
    int m_m;
    int m_z;
public:
      Fract(int z,int m);
      Fract();
      void set(int z,int m)
      {
        m_m=m;
        m_z=z;
      }//set函數(shù)依舊保留,以防后續(xù)使用。
      void print()
      {
        reduce();
        cout<<m_z<<"/"<<m_m<<endl;
      }
      void reduce()
      {
        for(int i=m_m;i>0;i--)
        {
            if((m_m%i==0)&&(m_z%i==0))
            {
                m_m/=i;
                m_z/=i;
                break;
            }
        }
      }
};
Fract::Fract(int z,int m)
{
    m_m=m;
    m_z=z;
}
Fract::Fract()
{
    m_m=12;
    m_z=16;
    //-cout<<"hello"<<endl;
}
int main()
{
    Fract a(12,16);
//  a.set(12,16);
    a.print();
    Fract b;//一般不用(),會(huì)出錯(cuò)。
//  a.set(12,16);
    b.print();
    //-Fractc[5];會(huì)調(diào)用5次
}
  • 注意
    • 類名==構(gòu)造函數(shù)名
    • 構(gòu)造函數(shù)沒有返回值類型,函數(shù)體也不允許返回值,但可以有返回語(yǔ)句"return".
  • 建立對(duì)象的同時(shí),自動(dòng)調(diào)用構(gòu)造函數(shù)
#include <iostream>
using namespace std;
class test1
{
    public:
        test1()
        {   
            cout<<"hello test1"<<endl;
        }
        
};
class test2
{
    test1 m_t;
    public:
        test2()
        {
            cout<<"hello test2"<<endl;
        }
};
int main()
{
    test2 t;
}
  • 類中成員變量也是類,調(diào)用順序:
#include <iostream>
using namespace std;
class test1
{
    int m_a;
    public:
        test1(int a)
        {   
            m_a=a;
            cout<<"hello test1"<<m_a<<endl;
        }
        test1(){}
};
class test2
{
    test1 m_t;
    test1 m_s;
    public:
        test2()
        {
            cout<<"hello test2"<<endl;
        }
};
int main()
{
    test2 t;
}
//test2()后調(diào)用賦值11出現(xiàn)hello test111+hello test2
//不調(diào)用則出現(xiàn)hello test2
#include <iostream>
using namespace std;
class test1
{
    int m_a;
    public:
        test1(int a)//
        {   
            m_a=a;
            cout<<"hello test1"<<m_a<<endl;
        }
        test1(){}
        ~test()
        {
            cout<<"析構(gòu)測(cè)試1"<<endl;
        }
};
class test2
{
    test1 m_t;
    test1 m_s;
    public:
        test2():m_s(222),m_t(333)
        {
            cout<<"hello test2"<<endl;
        }   
        ~test2()
        {
            cout<<"析構(gòu)測(cè)試2"<<endl;
        }
        
};
int main()
{
    test2 t;
    cout<<"+++"<<endl;
}
//hello test1333
//hello test1222
//hello test2
//+++
//析構(gòu)測(cè)試2
//析構(gòu)測(cè)試1222
//析構(gòu)測(cè)試1333
  • 析構(gòu)函數(shù)

    • 在t消亡的時(shí)候自動(dòng)調(diào)用,無(wú)返回值。
    • 析構(gòu)函數(shù)不允許重載。
    • 析構(gòu)函數(shù)有且只有一個(gè)。
    • 析構(gòu)函數(shù)是特殊的類成員函數(shù),不能隨意調(diào)用
  • 析構(gòu)函數(shù)之防止內(nèi)存泄漏

#include <iostream>
#include <malloc.h>
using namespace std;
class test
{
    int *p;
    public:
        test()
        {
            p=(int *)malloc(sizeof(int));
            *p=4;
            cout<<"test construct"<<endl;
        }
        ~test()
        {
            free(p);
            cout<<"析構(gòu)測(cè)試"<<endl;
        }
        void print()
        {
            cout<<*p<<endl;
        }
};
int main()
{
    test t;
    t.print();
    cout<<"+++++++"<<endl;
}
//test construct
//4
//++++++
//析構(gòu)測(cè)試
  • 手動(dòng)創(chuàng)建構(gòu)造函數(shù)是創(chuàng)建一個(gè)無(wú)名函數(shù)

  • C++給構(gòu)造對(duì)象的順序做了專門的規(guī)定

    • 局部和靜態(tài)對(duì)象,以聲明順序構(gòu)造
    • 靜態(tài)對(duì)象只能被構(gòu)造一次
    • 所有全局對(duì)象都在主函數(shù)main()之前被構(gòu)造
    • 全局對(duì)象構(gòu)造時(shí)無(wú)特殊順序
    • 成員以其在類聲明的順序構(gòu)造
  • 構(gòu)造函數(shù)在分配空間之后被調(diào)用

bool型變量

#include <iostream>
using namespace std;
int main()
{
    bool bSex=true;
    cout<<"請(qǐng)輸入數(shù)字"<<endl;
    cin>>bSex;
    cout<<"我叫SB,是一位"<<(bSex?"丑逼":"帥逼")<<endl;
    cout<<"true="<<true<<endl;
    cout<<"false="<<false<<endl;
}
  • 分配與釋放順序
#include <iostream>
using namespace std;
class test
{
    int m_data;
    public:
        test(int data):m_data(data)
        {
            cout<<"test("<<m_data<<")"<<endl;
        }
        ~test()
        {
            cout<<"~test("<<m_data<<")"<<endl;
        }
};
test t1(10);
void fun()
{
    test t4(40);
    return;
}
void show()
{
    static test t5(50);
    return;
}
int main(int argc,char **argv)
{
    test t2(20);
    test t3(30);
    fun();
    show();
}
test t6(60);
  • 類和構(gòu)造函數(shù)制造時(shí)鐘
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
class myClock
{
    private:
        int myHour;
        int myMin;
        int mySecond;
    public:
        myClock();
    //  void set(int hour,int min,int second);
        void Time();
        void show();
        void run();
};
myClock::myClock():myHour(23),myMin(59),mySecond(55)
{
}
/*
void myClock::set(int hour,int min,int second)
{
    myHour=hour;
    myMin=min;
    mySecond=second;
}*/
void myClock::Time()
{

        sleep(1);
        mySecond++;
        if(mySecond==60)
        {   
            myMin++;
            if(myMin==60)
            {
                myHour++;
                if(myHour==24)
                {
                    myHour=0;
                }
                myMin=0;
            }
            mySecond=0;
        }
    
}
void myClock::show()
{
    cout<<myHour<<":"<<myMin<<":"<<mySecond<<endl;
}
void myClock::run()
{
    while(1)
    {
        system("clear");
        show();
        Time();
    }
}
int main()
{
    myClock c;
//  c.set(23,59,55);
    c.run();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容