C++中的构造函数与析构函数及组合类的调用

// 构造函数与析构函数及类的组合

#include "stdafx.h"
#include <iostream>
using namespace std;

//枚举
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};

class CPU
{
private:
    CPU_Rank rank;
    int frequency;
    float voltage;
public:
    CPU(CPU_Rank r, int f, float v)
    {
        rank = r;
        frequency = f;
        voltage = v;

        cout << "构造了一个CPU!" << endl;
    }
    
    //CPU拷贝构造函数
    CPU(const CPU &c){
        rank = c.rank;
        frequency = c.frequency;
        voltage = c.voltage;

        cout << "拷贝构造了一个CPU!" << endl;
    }

    //CPU析构函数
    ~CPU(){
        cout << "析构了一个CPU!" << endl;
    }

    //函数API接口
    CPU_Rank GetRank() const {return rank;}
    int GetFrequency() const {return frequency;}
    float GetVoltage() const {return voltage;}

    void SetRank(CPU_Rank r) {rank = r;}
    void SetFrequnecy(int f) {frequency = f;}
    void SetVoltage(float v) {voltage = v;}

    void Run() {cout << "CPU开始运行!" << endl;}
    void Stop() {cout << "CPU停止运行!" << endl;}
};

//枚举
enum RAM_Type{DDR2 = 2, DDR3, DDR4};

class RAM{
private:
    enum RAM_Type type;
    unsigned int frequency;
    unsigned int size;
public:
    RAM(RAM_Type t, unsigned int f, unsigned int s){
        type = t;
        frequency = f;
        size = s;
        cout << "构造一个RAM!" << endl;
    }

    //RAM拷贝构造函数
    RAM(const RAM &c){
        type = c.type;
        frequency = c.frequency;
        size = c.size;

        cout << "拷贝构造了一个RAM!" << endl;
    }

    //RAM析构函数
    ~RAM(){cout << "析构一个RAM!" << endl;}

    //函数API接口
    RAM_Type GetType() const {return type;}
    unsigned int GetFrequency() const {return frequency;}
    unsigned int GetSize() const {return size;}

    void SetRank(RAM_Type t) {type = t;}
    void SetFrequnecy(unsigned int f) {frequency = f;}
    void SetSize(unsigned int s) {size = s;}

    void Run() {cout << "RAM开始运行!" << endl;}
    void Stop() {cout << "RAM停止运行!" << endl;}

};

//枚举
enum CDROM_Interface{SATA, USB};
enum CDROM_Install_type{exteral, built_in};

class CD_ROM
{
private:
    enum CDROM_Interface interface_type;
    unsigned int cache_size;
    CDROM_Install_type install_type;

public:
    CD_ROM(CDROM_Interface i, unsigned int s, CDROM_Install_type it){
        interface_type = i;
        cache_size = s;
        install_type = it;
        cout << "构造了一个CD_ROM!" << endl;
    }

    //CD_ROM拷贝构造函数
    CD_ROM(const CD_ROM &c){
        interface_type = c.interface_type;
        cache_size = c.cache_size;
        install_type = c.install_type;

        cout << "拷贝构造了一个CD_ROM!" << endl;
    }

    //CD_ROM析构函数
    ~CD_ROM(){
        cout << "析构了一个CD_ROM!" << endl;
    }

    //函数API接口
    CDROM_Interface GetInterfaceType() const {return interface_type;}
    unsigned int GetSize() const {return cache_size;}
    CDROM_Install_type GetInstallType() const {return install_type;}

    void SetInterfaceType(CDROM_Interface i) {interface_type = i;}
    void SetSize(unsigned int s) {cache_size = s;}
    void SetInstallType(CDROM_Install_type it) {install_type = it;}

    void Run() {cout << "CD_ROM开始运行!" << endl;}
    void Stop() {cout << "CD_ROM停止运行!" << endl;}
};

class COMPUTER
{
private:
    CPU my_cpu;
    RAM my_ram;
    CD_ROM my_cdrom;
    unsigned int storage_size;
    unsigned int bandwidth;
public:
    COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b);
    
    //COMPUTER析构函数
    ~COMPUTER(){cout << "析构一个COMPUTER!" << endl;}

    void Run(){
        my_cpu.Run();
        my_ram.Run();
        my_cdrom.Run();
        cout << "COMPUTER开始运行!" << endl;
    }

    void Stop(){
        my_cpu.Stop();
        my_ram.Stop();
        my_cdrom.Stop();
        cout << "COMPUTER停止运行!" << endl;
    }
};

//COMPUTER构造函数的函数体   {类名::函数名:初始化列表}  此处需要调用两次拷贝构造函数分别是:CPU c, RAM r, CD_ROM cd
COMPUTER::COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b):my_cpu(c),my_ram(r),my_cdrom(cd),storage_size(s),bandwidth(b){
    //storage_size = s;
    //bandwidth = b;

    cout << "构造了一个COMPUTER!" <<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    CPU a(P6,300,2.8);
    a.Run();
    a.Stop();
    cout << "*********************************************" << endl;

    RAM b(DDR3, 1600, 8);
    b.Run();
    b.Stop();
    cout << "*********************************************" << endl;

    CD_ROM c(SATA, 2, built_in);
    c.Run();
    c.Stop();
    cout << "*********************************************" << endl;
    
    COMPUTER my_computer(a, b, c, 128, 10);
    cout << "*********************************************" << endl;
    my_computer.Run();
    my_computer.Stop();
    cout << "*********************************************" << endl;

    //system("pause");  //此语句会提前中止程序,会导致程序不能顺利的调用析构函数
    return 0;
    //在执行完return 0; 语句后调用析构函数
}


原文地址:https://www.cnblogs.com/goahead--linux/p/11039681.html