您的位置:首页 > 理论基础 > 数据结构算法

数据结构(C++)中C++常用语法

2010-07-15 08:26 337 查看
1.C++类

数据结构大都用来存储数据(通常是相同类型项的集合)的对象,并且提供处理这些集合的函数。

1.1 基本class语法

在C++中类由成员(member)构成。成员可以是数据,也可以是函数,其中函数成为成员函数(member function)。类中的每一个实例都是一个对象。每一个对象包含类中指定的数据成员(除非这些数据成员是static,否则这是一个可以暂时安全忽略的细节)。成员函数作用域对象,通常被称为方法(method)。

以下是IntCell类的一个例子。在IntCell类中,IntCell的每一个实例(IntCell对象)都包含一个称为storedValue的数据成员。这个类中的其他部分是方法。在这个例子中有4个方法,其中的2个方法是read和write,另外的2个是称为构造函数的特殊方法。下面论述其关键特性。
/**
*A class for simulating an integer memory cell.
*/
class IntCell
{
public:
/**
*Construct the IntCell.
*Initial value is 0.
*/
IntCell()
{
storedValue=0;
}
/**
*Construct the IntCell.
*Initial value is initialValue.
*/
IntCell(int initialValue)
{
storedValue = initialValue;
}
/**
*Return the stored value.
*/
int read()
{
return storedValue;
}
/**
*change the stored value to x.
*/
void write(int x)
{
storedValue = x;
}
private:
int storedValue;
};


首先:一般地,数据成员声明为private,这样子可以禁止对该类内部细节的访问,而作为一般用途的方法为public.这被称为信息隐藏(information hiding)。

其次,讨论两个构造函数。构造函数是描述如何构建类的实例的方法。

1.2特别的构造函数语法与访问函数

/**
*A class for simulating an integer memory cell.
*/
class IntCell
{
public:
explicit IntCell(int initialValue = 0)
:storedValue(initialValue){}
int read() const
{
return storedValue;
}
void write(int x)
{
storedValue = x;
}
private:
int storedValue;
};

/*--------改进的IntCell类--------*/


1)默认参数

IntCell构造函数阐述了默认参数(default parameter)。相应地,定义了两个IntCell构造参数。一个构造参数接受initialValue,另一个是零参数构造函数。后者是隐含的。

2)初始化列表

IntCell构造函数在其代码体之前使用初始化列表(initializer list) 。初始化列表用来直接初始化数据成员。但是在数据承压unshi具有复杂初始化过程的类类型的时候,使用初始化列表代替代码体中的赋值语句可以节省很多时间。例如:如果一个数据成员是const的(意味着对象被构造后就不能再改变),那么数据成员的值就只能在初始化列表进行初始化。另外,如果一个数据成员是不具有零参数的构造函数的类类型,那么,该数据成员也必须在初始化列表进行初始化。

3)explicit构造函数

IntCell构造函数explicit的。所有的单参数的构造函数都必须是explicit的,以避免后套的类型转换。否则,一些宽松的规则将允许在没有显式类型转换操作的情况下进行类型转换。通常,这种不希望发生的行为会破坏代码的可读性,并导致难以发现的错误。

4)常量成员函数

只进行检测但不改变其对象的状态的成员函数成为访问函数(accessor)。改变其对象的状态的成员函数成为修改函数(mutator).

在C++中,每个承压unhanshu都标记为访问函数或修改函数。在设计阶段这是很重要的一步,不可以被简单地看成注释。事实上,这是重要的语义逻辑。

1.3.接口与实现的分离

1)预处理命令

接口通常都放在以.h结尾的文件中。需要接口信息的源代码必须#include接口文件。本例及时实现文件又是包含main的文件。偶尔,一个复杂的项目中包含其他文件的文件,这样在编译一个文件就存在一个接口被读两次的危险,这是非法的。为了避免这种情况,每个头文件在读类接口时都定义一个预处理器来定义一个符号,如下所示,符号名IntCell_H不应出现在其它文件中,通常该符号都是文件名。接口文件的第一行是否未定义,如果答案是肯定的,就接着处理文件,否则就不处理文件(跳到#endif),因为该文件已知是读过的了。

#ifndef IntCell_H
#define IntCell_H
/**
*A class for simulating an integer memory cell
*/
class IntCell
{
public:
explicit IntCell(int initialValue = 0);
int read() const;
void write(int x );
private:
int storedValue;
};
#endif
/*--------Interface of IntCell class  IntCell.h File-------- */


2)作用域运算符

语法ClassName::member;

3)

#include "IntCell.h"
/**
* construct the IntCell with initialValue
*/
IntCell::IntCell(int initialvalue): storedValue
{
}

/**
*return the stored value
*/
int IntCell::read() const
{
return storedValue;
}

/**
*store x.
*/
void IntCell::write(int x )
{
storedValue = x;
}


#include<iostream>
#include "IntCell.h"
using namespace std;
int main()
{
IntCell m;
m.write(5);
cout<<"Cell contents:"<<m.read()<<endl;
return 0;
}


2.C++细节

3.模板

4.使用矩阵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: