C++类与对象

内联函数

内联函数 提高效率
编译的时候将代码直接嵌入到调用的地方,从而减少了函数调用的开销,但是程序的体积会增大 以空间换时间。
内联函数一般比较短小,只是给编译器一个提示,如果函数中有switch for 可能不会将至解析为内联函数。
例如:
Test.H
#ifndef _TEST_H
#define _TEST_H
class Test
{
public:
int Add(int a , int b);
// void Init();
void Init(int x);
void Init(int x , int y);
void Init(int x = 0 , int y = 0, int z = 0);
void Display();

private:
int x_, y_, z_;
};
#endif // _TEST_H

Test.cpp
  
  
#include "Test.h"
#include <iostream>
using namespace std;
//内联成员函数有两种表现形式
//第一种为如下例子;第二种为直接在函数定义时实现方法
/*
class Test
{
public:
void Add(int a , int b)
{
return a+b;
}
};
*/
inline int Test::Add(int a, int b)
{
return a+b;
}
/*
void Test::Init()
{
x_ = y_ = 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_ << " " << y_ << " " << z_;
}

01.cpp

#include "Test.h"
int main()
{
Test t;
t.Init();
t.Display();
return 0;
}

class与struct的区别

相同的作用域
class 与 struct 的区别:在为指定权限时,class默认为私有,而struct默认为公开的。
类作用域中说明的标识符只在类中可见
#include <iostream>
using namespace std;

class test
{
public:
int num_;
};

//num_ = 20;
/*num_的作用域在类的内部
int num_ = 20; num_的作用域是文件作用域,与类中的num_的作用域不同
*/

int add(int a , int b);
//a与b两个标识符称为函数原型作用域

int main()
{
int num = 30;
{
int num = 1000; //块作用域
}
cout <<num; //30
cout << ::num ; //20
return 0;
}

int add(int a , int b)
{

return a+b;
}

int test1()
{
LABLE:
cout << "lable" << endl;
goto LABLE3;//标签做为函数作用域
LABLE2:
cout << "leble2" << endl;
LABLE3:
cout << "leble3" << endl;
}

类的前向定义

C++类必须先定义,才能够实例化。
两个类需要相互引用形成一个环形引用时,无法先定义使用,这时需要使用向前定义。
前向定义的类不能实例化。
例如:
A.h:
#ifndef _A_H
#define _A_H
#include "B.h"
class A
{
public:
A(void)
{
}
~A(void)
{
}
B b;
};
#endif
B.h:
#ifndef _B_H
#define _B_H
#include "A.h"

class A;
class B
{
public:
B(void)
{
}
~B(void)
{
}
A* a; //前向定义的类不能实例化。只能定义指针
};
#endif

嵌套类与局部类

嵌套类
外围类需要使用嵌套类作为底层实现,并且该嵌套类只用于外围类的实现,且同时可以对用户隐藏该底层实现。
嵌套类被隐藏在外围类之中,该类名只能在外围类中使用,如果在外围类的作用域使用该类名是,需要加名字限定。
嵌套类中的成员函数可以在类体外定义。
嵌套类与外围类之间相互不能访问私有成员。
嵌套类仅仅是语法上的嵌套。
局部类
类可以定义在函数体内,这样的类被称为局部类。局部类只能定义在它的局部域内可见。
局部类的成员函数必须被定义在类体中。
局部类中不能有静态成员。
例如:
#include <iostream>
using namespace std;

class Outer
{
public :
class Inter
{
public:
void Fun();
//{
// cout << "Inter Fun" << endl;
//}
};
public:
Inter obj;
void Fun()
{
cout << "Outer Fun" << endl;
obj.Fun();
}
};
//Inter为嵌套类,Outer为外围类

void Outer::Inter::Fun()
{
cout << "Inter Fun" << endl;
}

void Fun()
{
class LocalClass
{
/*
private:
int m;//部类中不能有静态成员。
*/
public:
int num;
void Init(int num)
{
this -> num = num;
}
void Display()
{
cout << num << endl;
}
};
LocalClass l;
l.Init(5);
l.Display(); // 局部类只能定义在它的局部域内可见。
}

int main(void)
{
Outer o;
o.Fun();
Fun();
Outer::Inter inter;
inter.Fun();
/*
LocalClass l;
l.Init(5);
l.Display();
*/
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值