C++面向對象練習(一)—— 各種函數執行順序


概覽:C++面向對象練習:構造函數、拷貝構造函數、重載賦值運算符以及析構函數的執行順序。

本文首發於我的個人博客www.colourso.top,歡迎來訪。

代碼全部運行於VS2019

博客后續會持續更新補充。

各個函數執行順序

class Dog
{
public:
    Dog() 
    {
        cout << "默認構造函數" << endl;
    }

    Dog(int age) :age(age) 
    {
        cout << "有參構造函數" << endl;
    }

    Dog(const Dog& dog) :age(dog.age) 
    {
        cout << "拷貝構造函數" << endl;
    }

    Dog& operator=(const Dog& dog)
    {
        cout << "拷貝賦值運算" << endl;
        if(this == &dog)//自賦值
            return *this;

        this->age = dog.age;
        return *this;
    }

    ~Dog() 
    {
        cout << "析構函數" << endl;
    }

    void show()
    {
        cout << "--  "<<this->age <<"  --"<< endl;
    }

private:
    int age;
};

void test0()
{
    cout << "函數test0()開始執行" << endl;
    Dog d0;
    cout << "函數test0()結束執行" << endl;
}

void test1(Dog dd)
{
    cout << "函數test1()開始執行" << endl;
    dd.show();
    cout << "函數test1()結束執行" << endl;
}

Dog test2(Dog& dd)
{
    cout << "函數test2()開始執行" << endl;
    Dog tmp = dd;
    tmp.show();
    cout << "函數test2()結束執行" << endl;
    return tmp;
}

int main()
{
    cout << "主函數main()開始執行" << endl;
    test0();

    Dog d1(12);

    Dog d2 = d1;

    Dog d3;

    d3 = d1;
    
    test1(d3);

    Dog d4;

    d4 = test2(d1);

    cout << "主函數main()結束執行" << endl;

    return 0;
}

執行結果如下:

  • 主函數main()開始執行

  • 函數test0()開始執行

  • 默認構造函數

  • 函數test0()結束執行

  • 析構函數

  • 有參構造函數

  • 拷貝構造函數

  • 默認構造函數

  • 拷貝賦值運算

  • 拷貝構造函數

  • 函數test1()開始執行

  • -- 12 --

  • 函數test1()結束執行

  • 析構函數

  • 默認構造函數

  • 函數test2()開始執行

  • 拷貝構造函數

  • -- 12 --

  • 函數test2()結束執行

  • 拷貝構造函數

  • 析構函數

  • 拷貝賦值運算

  • 主函數main()結束執行

  • 析構函數

  • 析構函數

  • 析構函數

  • 析構函數

d4 = test2(d1)在函數值返回時注意!

Dog test3()
{
    cout << "函數test3()開始執行" << endl;
    Dog tmp(10);
    cout << "函數test3()結束執行" << endl;
    return tmp;
}

int main()
{
    cout << "主函數main()開始執行" << endl;

    Dog d5 = test3();
    
    cout << "--------" << endl;

    test3();

    cout << "主函數main()結束執行" << endl;

    return 0;
}

結果為:

  • 主函數main()開始執行
  • 函數test3()開始執行
  • 有參構造函數
  • 函數test3()結束執行
  • 拷貝構造函數
  • 析構函數
  • --------
  • 函數test3()開始執行
  • 有參構造函數
  • 函數test3()結束執行
  • 拷貝構造函數
  • 析構函數
  • 析構函數
  • 主函數main()結束執行
  • 析構函數

當函數返回一個對象的時候,所作的操作為匿名對象 = return tmp,這樣就會執行拷貝構造函數,然后tmp析構。

而若是函數返回的匿名對象沒有值進行接受的話,匿名對象會立刻進行析構。而若是有值進行接受的話,匿名對象就會轉正,不會觸發任何函數。


C++面向對象練習

試定義一個處理學生信息的類Student。該類包含學號、成績和姓名等數據成員( 學號不能相同)以及
若干成員函數,另外定義全局函數max),返回n個學生成績最高者。

具體要求如下:

(1)私有數據成員

int num,score; num存放學號,score 存放成績

char name[9]; name 存放學生的姓名

(2)公有成員函數

構造函數:將學號、成績設置為0,姓名設置為空

void Set(int id,char *na,int sc );為數據成員賦值。

int get_ score();返 回學生成績。

void print();輸出學生的學號、姓名和成績。

(3) Student max(Student *,int n);全局函數,求得並返回s所指向的n個學生中成績最高者。

int check_num(Student str[],int n,int num);檢查學號是否已經在str中存在返回1,否則返回0。

(4)在主函數中完成對該類的測試,主函數中定義一個長度為3的Student類的對象數組s,依次鍵入學號、成績、姓名;注意,在輸入每-一個學生的信息后,要判斷所輸入的學號是否已存在,如果存在,則要求重新輸入該學生的信息。最后調用全局函數max(,得到成績最高的學生信息並輸出。

參考鏈接: C++面向對象程序設計50道編程題(第21題)

/*測試環境: VS2019*/
#include <iostream>

using namespace std;

class Student
{
public:
    Student();
    ~Student();

    void Set(int id, char* na, int sc); //為數據成員賦值
    int get_score();                    //返回學生成績
    int get_num();                      //返回學生學號
    void print();                       //輸出學生的學號、姓名、成績

private:
    int num;        //學號
    int score;      //成績
    char name[9];   //姓名
};

Student max(Student*, int n);				//求得並返回n個學生中成績最高的
int check_num(Student stu[], int n,int num);//檢查學號是否已經在stu中

int main()
{
    Student stu[3];
    int input_num;
    int input_score;
    char input_name[9];
    int i = 0;
    while(1)
    {
        cout << "請輸入學生的學號" << endl;
        cin >> input_num;
        if (check_num(stu, i, input_num) == 1)
        {
            cout << "已經存在,請重新輸入" << endl;
            continue;
        }
        
        cout << "請輸入學生的成績" << endl;
        cin >> input_score;

        cout << "請輸入學生的姓名" << endl;
        cin >> input_name;

        stu[i].Set(input_num, input_name, input_score);

        cout << "信息錄入成功" << endl;

        i++;

        if (i >= 3) break; 
    }

    cout << "學生信息如下:" << endl;
    for (i = 0; i < 3; i++)
    {
        stu[i].print();
    }

    cout << "成績最高的學生信息如下:" << endl;
    max(stu, 3).print();

    return 0;
}

Student::Student()
{
    this->num = 0;
    this->score = 0;
    strcpy_s(this->name,"");
}

Student::~Student()
{

}

void Student::Set(int id, char* na, int sc)
{
    this->num = id;
    this->score = sc;
    strcpy_s(this->name, na);
}

int Student::get_score()
{
    return this->score;
}

int Student::get_num()
{
    return this->num;
}

void Student::print()
{
    cout << "Name: " << this->name << ": id:" << this->num
        << ", score: " << this->score << endl;
}

Student max(Student* stu, int n)
{
    int max = 0;
    int max_score = stu[0].get_score();

    for (int i = 1; i < n; i++)
    {
        if (max_score < stu[i].get_score())
        {
            max_score = stu[i].get_score();
            max = i;
        }      
    }
    return stu[max];
}

int check_num(Student stu[], int n, int num)
{
    for (int i = 0; i < n; i++)
    {
        if (stu[i].get_num() == num)
        {
            return 1;
        }
    }
    return 0;
}

本文首發於我的個人博客www.colourso.top,歡迎來訪。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM