C++第二節(1):public和private、初始化列表、函數重載

public和private、初始化列表、函數重載

1、初始化列表:

     1.1 在構造函數函數頭,對類的成員變量進行初始化

     1.2 :成員變量(賦給成員變量的值){}       賦給成員變量的值:形參或常數

2、重載函數:

     2.1函數名一樣,參數列表不同(參數類型或參數個數不同)

     2.2 僅僅返回值不同是不可以的

A、public和private

#include <iostream>    //public和private
using namespace std;
class Student
{
private:
    int num;
public:
    char *name;
    Student();
    void setValue(int a, char *b);
    int getNum();
    char * getName();
    void print();
};
Student::Student()
{
    num = 000;
    name = "小強";
}
void Student::setValue(int a, char *b)
{
    num = a;
    name = b;
}
int Student::getNum()
{
    return num;
}
char * Student::getName()
{
    return name;
}
void Student::print()
{
    cout << name << endl;
    cout << num << endl;
}
int main()
{
    Student a;
    //a.name ="小明";  //公有成員變量,可以使用這種形式訪問。
    //a.num = 123; //私有成員變量,只能通過公有成員函數纔可以訪問
    a.setValue(123456, "小紅");
    a.print();
    cout << a.getNum() << endl;
    cout << a.getName() << endl;
    return 0;
}
B、默認、非默認、初始化列表

Thing.h

#ifndef __C__No727Class__Thing__
#define __C__No727Class__Thing__

#include <iostream>
using namespace std;
class Thing
{
public:
    //Thing();  默認構造函數,不帶默認值,不能傳參
    Thing(int a = 10, int b = 20, int c = 30);  //帶默認值的默認構造函數,可以傳參
    //Thing (int m, int n, int p);  //非默認構造函數,自定義的構造函數,傳3個參數
    ~Thing();
    void printThing();
private:
    int x;
    int y;
    int z;
};
#endif /* defined(__C__No727Class__Thing__) */
Thing.cpp

#include "Thing.h"
//Thing::Thing()  對無參數的默認構造函數進行實現,初始化
//{
//    x = 10;
//    y = 20;
//    z = 30;
//}
//Thing::Thing(int a, int b, int c)  //對帶默認值的默認構造函數進行實現
//{
//    x = a;
//    y = b;
//    z = c;
//}    //與下面使用初始化列表的語句等價
Thing::Thing(int a, int b, int c):x(a), y(b), z(c) {};
void Thing::printThing()
{
    cout << "x = " << x << "y = " << y << "z = " << z << endl;
}
Thing::~Thing()
{
    cout << "~~~" << endl;
}
main.cpp

#include "Thing.h"
int main(int argc, const char * argv[])
{
    Thing t;
    t.printThing();
    Thing s(1);
    s.printThing();
    Thing r(1, 2);
    r.printThing();
    Thing v(1, 2, 3);
    v.printThing();
    
    Thing *y = new Thing(5, 5, 5);  //類類型指針y,指向new的堆空間,堆空間裏面是一個對象
    (*y).printThing();  //訪問方式(*y).和y ->

    delete y;    //手工回收內存,把new的堆空間收回,即析構堆空間的對象,此時調用析構函數
    return 0;
}
C、函數重載

Overload.h

#ifndef __C__No727Class__Overload__
#define __C__No727Class__Overload__

#include <iostream>
using namespace std;
class Overload
{
private:
    int x;
    int y;
    int z;
public:
    Overload();  //無參數的默認構造函數
    Overload(int a, int b, int c);  //自定義函數,下面兩個函數是對該函數的重載
    Overload(int a, int b);
    Overload(int a);
    void print();
};
#endif /* defined(__C__No727Class__Overload__) */
Overload.cpp
#include "Overload.h"
Overload::Overload()
{
    x = 75;
    y = 99;
    z = 66;
}
Overload::Overload(int a, int b, int c)
{
    x = a;
    y = b;
    z = c;
}
Overload::Overload(int a, int b)
{
    x = a;
    y = b;
}
Overload::Overload(int a)
{
    x = a;
}
void Overload::print()
{
    cout << "x = "<< x << "  y = " << y << "  z = " << z <<endl;
}
main.cpp

<pre name="code" class="cpp">#include "Overload.h"
int main()
{
    Overload t;      //默認值
    t.print();
    Overload s(1);   //只傳遞第一個值,其他值自動置爲0
    s.print();
    Overload r(1, 2);
    r.print();
    Overload v(1, 2, 3);
    v.print();
    Overload a;
    a = t;           //對象的賦值
    a.print();
    a = v;
    a.print();
    return 0;
}



發佈了31 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章