用c++实现一个基本的类

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38679924/article/details/88931543
————————————————
版权声明:本文为CSDN博主「田野麦子」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38679924/article/details/88931543

1.将类的成员函数的声明与实现分开来写;这种写法会使得代码更加清晰,但此时对成员函数的调用要经过函数压栈操作,对于简单代码段来说,执行效率会比较低。

/*---------------------------------------
功能:定义一个CPU类,并实现其相关功能
运行结果:
Calling argument constructor of CPU...
The CPU is running...
The CPU is stopped...
Calling destruct of CPU...
-----------------------------------------
Author: Zhang Kaizhou
Date: 2019-3-31 16:53:22
----------------------------------------*/

#include <iostream>

using namespace std;

enum CPU_Rank{P1 = 1, P2, P3, P4, P5, P6, P7}; // 声明一个枚举数据类型,名字为CPU_Rank
class CPU{
public:
    CPU(CPU_Rank r, int f, float v); // 含参构造函数
    CPU(); // 默认构造函数
    ~CPU(); // 析构函数
    void setRank(CPU_Rank r);
    void setFrequency(int frequency);
    void setvoltage(float v);
    CPU_Rank getRank();
    int getFrequency();
    float getVoltage();
    void run();
    void stop();
private:
    CPU_Rank rank;
    int frequency;
    float voltage;
};

CPU::CPU(CPU_Rank r, int f, float v):rank(r), frequency(f), voltage(v){ // 含参构造函数的实现
    cout << "Calling argument constructor of CPU..." << endl;
}
CPU::CPU(){ // 默认构造函数的实现
    cout << "Calling default constructor of CPU..." << endl;
}

CPU::~CPU(){ // 析构函数的实现
    cout << "Calling destruct of CPU..." << endl;
}

void CPU::setRank(CPU_Rank r){
    rank = r;
}

void CPU::setFrequency(int f){
    frequency = f;
}

void CPU::setvoltage(float v){
    voltage = v;
}

CPU_Rank CPU::getRank(){
    return rank;
}

int CPU::getFrequency(){
    return frequency;
}

float CPU::getVoltage(){
    return voltage;
}

void CPU::run(){
    cout << "The CPU is running..." << endl;
}

void CPU::stop(){
    cout << "The CPU is stopped..." << endl;
}

int main(){
    CPU a(P6, 300, 2.8);
    a.run();
    a.stop();

    return 0;
}

2.将类的成员函数的声明和实现都在类的内部实现;此时的成员函数为内联成员函数,调用是采用的是代码段覆盖的方式,不需要函数压栈,对于简单代码段来说,效率更高些。

/*---------------------------------------
功能:定义一个CPU类,并实现其相关功能
运行结果:
Calling argument constructor of CPU...
The CPU is running...
The CPU is stopped...
Calling destruct of CPU...
-----------------------------------------
Author: Zhang Kaizhou
Date: 2019-3-31 17:01:20
----------------------------------------*/
#include <iostream>

using namespace std;

enum CPU_Rank{P1 = 1, P2, P3, P4, P5, P6, P7}; // 声明一个枚举数据类型,名字为CPU_Rank
class CPU{
public:
    CPU(CPU_Rank r, int f, float v):rank(r), frequency(f), voltage(v){ // 含参构造函数的实现
        cout << "Calling argument constructor of CPU..." << endl;
    }
    CPU(){ // 默认构造函数的实现
        cout << "Calling default constructor of CPU..." << endl;
    }

    ~CPU(){ // 析构函数的实现
        cout << "Calling destruct of CPU..." << endl;
    }

    void setRank(CPU_Rank r){
        rank = r;
    }

    void setFrequency(int f){
        frequency = f;
    }

    void setvoltage(float v){
        voltage = v;
    }

    CPU_Rank getRank(){
        return rank;
    }

    int getFrequency(){
        return frequency;
    }

    float getVoltage(){
        return voltage;
    }

    void run(){
        cout << "The CPU is running..." << endl;
    }

    void stop(){
        cout << "The CPU is stopped..." << endl;
    }
private:
    CPU_Rank rank;
    int frequency;
    float voltage;
};

int main(){
    CPU a(P6, 300, 2.8);
    a.run();
    a.stop();

    return 0;
}

3.将类的成员函数的声明与实现分开来写,但是对于那些较短的,结构相对简单的(内部不含有循环结构)成员函数可使用"inline"关键字将其定义为内联函数,从而提高代码的执行效率;

/*---------------------------------------
功能:定义一个CPU类,并实现其相关功能
运行结果:
Calling argument constructor of CPU...
The CPU is running...
The CPU is stopped...
Calling destruct of CPU...
-----------------------------------------
Author: Zhang Kaizhou
Date: 2019-3-31 17:07:48
----------------------------------------*/
#include <iostream>

using namespace std;

enum CPU_Rank{P1 = 1, P2, P3, P4, P5, P6, P7}; // 声明一个枚举数据类型,名字为CPU_Rank
class CPU{
public:
    CPU(CPU_Rank r, int f, float v); // 含参构造函数
    CPU(); // 默认构造函数
    ~CPU(); // 析构函数
    void setRank(CPU_Rank r);
    void setFrequency(int frequency);
    void setvoltage(float v);
    CPU_Rank getRank();
    int getFrequency();
    float getVoltage();
    void run();
    void stop();
private:
    CPU_Rank rank;
    int frequency;
    float voltage;
};

inline CPU::CPU(CPU_Rank r, int f, float v):rank(r), frequency(f), voltage(v){ // 含参构造函数的实现
    cout << "Calling argument constructor of CPU..." << endl;
}
inline CPU::CPU(){ // 默认构造函数的实现
    cout << "Calling default constructor of CPU..." << endl;
}

inline CPU::~CPU(){ // 析构函数的实现
    cout << "Calling destruct of CPU..." << endl;
}

inline void CPU::setRank(CPU_Rank r){
    rank = r;
}

inline void CPU::setFrequency(int f){
    frequency = f;
}

inline void CPU::setvoltage(float v){
    voltage = v;
}

inline CPU_Rank CPU::getRank(){
    return rank;
}

inline int CPU::getFrequency(){
    return frequency;
}

inline float CPU::getVoltage(){
    return voltage;
}

inline void CPU::run(){
    cout << "The CPU is running..." << endl;
}

inline void CPU::stop(){
    cout << "The CPU is stopped..." << endl;
}

int main(){
    CPU a(P6, 300, 2.8);
    a.run();
    a.stop();

    return 0;
}

比较推荐第三种代码风格,既可以保证代码清晰,有能有效地提高代码的执行效率。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值