版权声明:本文为博主原创文章,遵循 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;
}
比较推荐第三种代码风格,既可以保证代码清晰,有能有效地提高代码的执行效率。