C++基礎(類和構造函數)

C++基礎

類和構造函數

  • 類:類是定義同一類所有對象得變量和方法的藍圖或原型。數據與算法的統一。

    • 成員
    • 成員函數
    • 保護數據
    • 封裝
  • 類和對象就像人類與人的關系

  • 對象的初始化

    • 建立對象,須有一個有意義的初始值
    • C++建立和初始化對象的過程專門由該類的構造函數來完成。
    • 構造函數給對象分配空間和初始化。
    • 如果一個類沒有專門定義構造函數,那么C++就僅僅創建對象而不做任何初始化
#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默認私有。

  • 面向對象

    • 在面向對象中,算法與數據結構被捆綁為一類
  • 定義成員函數

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

    • 保護成員
      • 除了友元和子類的成員函數之外,從類的外部就不能對它們訪問
    • 私有成員
      • 從類的外部就不能對它們訪問
    • 公共成員
      • 公共成員在任何地方都可以被訪問。
  • 不同域對應不同的函數內容

#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();
}
  • 對象與域的調用方法
#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的區別
    • 除關鍵字不同外(class,struct)的唯一區別是,結構在默認情況下的成員是公共的,而類在默認情況下的成員是私有的。 在C++中,結構是特殊的類。
  • 類的作用域
    • 一個類的所有成員位于這個類的作用域內
    • 類作用域是類定義和相應類成員定義范圍
  • 對象和類的聯系與區別

  • 類中訪問控制的關鍵字有哪幾個,分別是什么意義

  • 封裝理念
    • 封裝來源于信息隱藏的設計理念, 是通過特性和行為的組合來創建新數據類型讓接口與具體實現相隔離。
    • C++中是通過類來實現的, 為了盡量避免某個模塊的行為干擾同一系統中的其它模塊,應該讓模塊僅僅公開必須讓外界知道的接口。
  • 封裝的優點
    • 重用
    • 不必關心具體的體現
    • 具有安全性

構造函數

  • 定義
    • C++的構造函數和析構,使類對象能輕松的被賦值與撤銷。它們是一種特殊的函數。
    • 構造函數是第一個自動調用的函數
    • 構造函數創建類對象,初始化其成員
    • 析構函數撤銷類對象
    • 構造函數和析構函數是類的特殊成員函數
    • 與類同名
    • 析構函數
    • 重載構造函數
    • 默認參數構造函數
    • 對象創建過程
#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函數依舊保留,以防后續使用。
      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;//一般不用(),會出錯。
//  a.set(12,16);
    b.print();
    //-Fractc[5];會調用5次
}
  • 注意
    • 類名==構造函數名
    • 構造函數沒有返回值類型,函數體也不允許返回值,但可以有返回語句"return".
  • 建立對象的同時,自動調用構造函數
#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;
}
  • 類中成員變量也是類,調用順序:
#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()后調用賦值11出現hello test111+hello test2
//不調用則出現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<<"析構測試1"<<endl;
        }
};
class test2
{
    test1 m_t;
    test1 m_s;
    public:
        test2():m_s(222),m_t(333)
        {
            cout<<"hello test2"<<endl;
        }   
        ~test2()
        {
            cout<<"析構測試2"<<endl;
        }
        
};
int main()
{
    test2 t;
    cout<<"+++"<<endl;
}
//hello test1333
//hello test1222
//hello test2
//+++
//析構測試2
//析構測試1222
//析構測試1333
  • 析構函數

    • 在t消亡的時候自動調用,無返回值。
    • 析構函數不允許重載。
    • 析構函數有且只有一個。
    • 析構函數是特殊的類成員函數,不能隨意調用
  • 析構函數之防止內存泄漏

#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<<"析構測試"<<endl;
        }
        void print()
        {
            cout<<*p<<endl;
        }
};
int main()
{
    test t;
    t.print();
    cout<<"+++++++"<<endl;
}
//test construct
//4
//++++++
//析構測試
  • 手動創建構造函數是創建一個無名函數

  • C++給構造對象的順序做了專門的規定

    • 局部和靜態對象,以聲明順序構造
    • 靜態對象只能被構造一次
    • 所有全局對象都在主函數main()之前被構造
    • 全局對象構造時無特殊順序
    • 成員以其在類聲明的順序構造
  • 構造函數在分配空間之后被調用

bool型變量

#include <iostream>
using namespace std;
int main()
{
    bool bSex=true;
    cout<<"請輸入數字"<<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);
  • 類和構造函數制造時鐘
#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();
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容