类的声明
class 类名称
{
public:
公有成员(外部接口)
private:
私有成员
protected:
保护成员
};
- public后面声明,它们是类与外部的接口,任何外部函数都可以访问公有数据类型和函数;
- private后面声明,只允许本类中的函数访问,而类外部的任何函数都不能访问;
- protected后面声明,与private类似,其差别表现在继承与派生时对派生类的影响不同
内联成员函数
内联函数
提高效率,编译的时候直接将代码嵌入到调用的地方,从而减少函数调用的开销。内联函数都比较短小。
定义方式
两种定义方式:
/*------第一种定义方式------*/
/* Test.h */
#ifndef _TEST_H_
#define _TEST_H_
class Test
{
public:
int Add(int a, int b);
};
#endif
/* Test.cpp */
#include "Test.h"
inline int Test::Add(int a, int b)
{
return a+b;
}
/*------第二种定义方式------*/
/* Test.h */
#ifndef _TEST_H_
#define _TEST_H_
class Test
{
public:
int Add(int a, int b)
{
return a+b;
}
// 此时Add已为内联函数,即使没有inline关键字
};
#endif
成员函数的重载及其缺省参数
- 重载
/* Test.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_
/* Test.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::Init(int x/* =0 */, int y/* =0 */, int z/* =0 */)
{
x_ = x;
y_ = y;
z_ = z;
}
void Test::Display()
{
cout<<"x="<<x_<<" y="<<y_<<" z="<<z_<<endl;
}
- 参数缺省
/* Test.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_
/* Test.cpp */
#include "Test.h"
#include <iostream>
using namespace std;
void Test::Init(int x/* =0 */, int y/* =0 */, int z/* =0 */)
{
x_ = x;
y_ = y;
z_ = z;
}
void Test::Display()
{
cout<<"x="<<x_<<" y="<<y_<<" z="<<z_<<endl;
}
/* main.cpp */
#include "Test.h"
int main(void)
{
Test t;
t.Init(); //参数缺省,此时x=0,y=0,z=0
t.Display();
t.Init(10); //参数缺省,此时x=10,y=0,z=0
t.Display();
return 0;
}
类与结构体
class与struct的区别:在未指定访问权限时,class默认为私有的,struct默认为公有的
struct S
{
int X;//公有的
}
class C
{
int X;//私有的
}
隐含的this指针
成员函数有一个隐含的附加形参,即指向该对象的指针,这个隐含的形参叫做this指针。使用this指针保证了每个对象可以拥有不同的数据成员,但处理这些成员的代码可以被所有对象共享。
前向声明
使用时机
C++中类必须先定义才能够实例化。两个类需要相互引用形成一个“环形”引用时,无法先定义使用,这时候需要用到前向申明。
特别注意
前向申明的类不能够实例化。
/*示例代码*/
/*定义两个类A和B,当二者需要相互引用的时候*/
/*------A.h-------*/
#ifndef _A_H_
#define _A_H_
#include "B.h"
class A
{
public:
A(void);
~A(void);
B b_;
};
#endif // _A_H_
/*------B.h-------*/
#ifndef _B_H_
#define _B_H_
class A; //前向申明
class B
{
public:
B(void);
~B(void);
//A a_; Error! 前向声明的类不能实例化对象,因为前向申明的类我们并不知道这个类是什么样子的,就不知道怎么分配内存,所以只能定义指针或引用
A* a_;
void fun(A& a)
{
}
};
#endif // _B_H_
嵌套类
外围类需要使用嵌套类对象作为底层实现,并且该嵌套类志勇于外围类的实现,且同时可以对用户隐藏该底层实现。
/*————————嵌套类示例代码————————*/
#include<iostream>
using namespace std;
class Outer
{
//嵌套类
class Inner
{
public:
void Fun()
{
cout<<"Inner::Fun"<<endl;
}
};
public:
Inner obj_;
void Fun()
{
cout<<"Outer::Fun"<<endl;
obj.Fun;
}
};
从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用,需要加名字限定。
/*-------将上例中Inner中的Fun定义在外围类之外------*/
class Outer
{
//嵌套类
class Inner
{
public:
void Fun();
};
public:
Inner obj_;
void Fun()
{
cout<<"Outer::Fun"<<endl;
obj.Fun;
}
};
void Outer::Inner::Fun()
{
cout<<"Inner::Fun"<<endl;
}
嵌套类的成员函数无法访问外围类的私有成员,反之亦然;
嵌套类为public时,仅仅只是语法上的嵌入
局部类
类也可以定义在函数体内,这样的类被称为局部类(local class)。局部类只在定义它的域内可见;
局部类的成员函数必须被定义在类体中;
局部类中不能有静态成员。
/*------局部类-------*/
#include<iostream>
using namespace std;
void Fun()
{
//局部类
class Localclass
{
public:
int num_;
//局部类的成员函数必须被定义在局部类体中
void Init(int num)
{
num_ = num;
}
void Display()
{
cout<<"num="<<num_<<endl;
}
};
Localclass lc;
lc.Init(10);
lc.Display();
}