C++(类与对象2)
2022/4/27 14:12:49
本文主要是介绍C++(类与对象2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
(1)内联成员函数
(2)成员函数地重载及其缺参数
(3)类与结构体
(4)隐含地this指针
1、内联成员函数
¥回顾:
内联函数---------提高效率
编译的时候将代码直接嵌入到调用的地方,从而减少了函数调用的开销。(程序体积增大,以空间换时间)
内联函数一般短小,内联函数仅仅只是给编译器一个提示。如果函数中有switch,for,可能不会以内联函数解析。
¥类的内联函数一般有两种方法:
1、在类的实现(.cpp)中加inline关键字,如一个加法内联函数:
inline int Test::Add(int a, int b) { return a + b; }
2、在类的声明(.h)中完成函数的定义,如一个加法内联函数:
#ifndef _TEST_H_ #define _TEST_H_ class Test { public: int Add(int a,int b) { return a + b; } }; #endif //_TEST_H_
2、成员函数的重载及其缺省参数
¥成员函数的重载例子:
//.h
#ifndef _TEST_H_ #define _TEST_H_ class Test { public: void Init(); void Init(int x); void Init(int x,int y); void Init(int x,int y,int z); void Display(); private: int x_; int y_; int z_; }; #endif //_TEST_H_
//.cpp
#include "Test.h" #include <iostream> using namespace std; void Test::Init() { x_ = 0; y_ = 0; z_ = 0; } void Test::Init(int x) { x_ = x; y_ = 0; z_ = 0; } void Test::Init(int x, int y) { x_ = x; y_ = y; z_ = 0; } void Test::Init(int x, int y, int z) { x_ = x; y_ = y; z_ = z; } void Test::Display() { cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl; }
//测试文件
#include <iostream> #include <cstdio> #include"Test.h" using namespace std; int main(void) { Test t; t.Init(); t.Display(); t.Init(10); t.Display(); return 0; } /* 输出: x = 0 y = 0 z = 0 x = 10 y = 0 z = 0 */
¥缺省参数
//.h
#ifndef _TEST_H_ #define _TEST_H_ class Test { public: void Init(int x = 0, int y = 0, int z = 0); void Display(); private: int x_; int y_; int z_; }; #endif //_TEST_H_
//.cpp
#include "Test.h" #include <iostream> using namespace std; void Test::Init(int x, int y, int z) { x_ = x; y_ = y; z_ = z; } void Test::Display() { cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl; }
//测试代码:
#include <iostream> #include <cstdio> #include"Test.h" using namespace std; int main(void) { Test t; t.Init(); t.Display(); t.Init(10); t.Display(); return 0; } /* 输出: x = 0 y = 0 z = 0 x = 10 y = 0 z = 0 */
3、类与结构体
¥补充:在C++中结构体也可以看成一种类,区别如下。C++中结构体可以有成员函数,在C中结构体不能有成员函数。
¥class与struct的区别:在未指定访问权限时,class默认的是私有,struct默认是公有。
#include <iostream>
using namespace std;
struct Test2
{
int x_;
int y_;
int z_;
void Init(int x, int y, int z)
{
x_ = x;
y_ = y;
z_ = z;
}
void Display()
{
cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}
};
class Test3
{
int x_;
int y_;
int z_;
void Init(int x, int y, int z)
{
x_ = x;
y_ = y;
z_ = z;
}
void Display()
{
cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}
};
int main(void)
{
//Test2 t;
//t.Init(10,20,30);
//结构体、类可以还这样初始化:
/*Test2 t2 = {10,20,30};
t2.Display();*/
/*Test3 t3;
t3.Init(10,20,30);//Error,默认私有
t3.Disply();*/
return 0;
}
4、隐含的this指针
¥成员函数有一个隐含的附加形参,即指向该对象的指针,这个隐含的形参叫做this指针。
¥使用this指针保证了每个对象可以拥有不同的数据成员,但处理这些成员的代码可以被所有对象共享。
这篇关于C++(类与对象2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升