语法
|
例子
|
类的通式:
class
class-name{
private data and functions
public
:
public data and functions
}object-list;
|
class
Vehicle {
public
:
int passengers;
int fuelcap;
int mpg;
int range();
};
int Vehicle::range() {
return mpg * fuelcap;
}
|
构造函数
:
Class-name(){
//constructor code
}
|
class
MyClass {
public
:
int x;
MyClass(); // constructor
~MyClass(); // destructor
};
MyClass::MyClass() {
x = 10;
}
MyClass::~MyClass() {
cout << "Destructing.../n";
}
|
拷贝构造函数
:
classname(const classname & obj){
//body of constructor
}
|
在对象作为参数传递、由函数返回或用于初始化时,调用拷贝构造函数复制对象
只适用于初始化,不适用于赋值
当对象副本不能与原始对象保持一致时调用,可以防止原始对象受到破坏
可以访问类的私有成员,不使用对象和句点运算符
|
友元函数
:
class
Myclass{
//...
public
:
friend void frnd(Myclass ob);
//...
};
|
class
Cylinder; // a forward declaration
enum
colors { red, green, yellow };
class Cube {
colors color;
public
:
Cube(colors c) { color= c; }
bool sameColor(Cylinder y);
// ...
};
class
Cylinder {
colors color;
public
:
friend
bool Cube::sameColor(Cylinder y);
// ...
};
bool Cube::sameColor(Cylinder y) {
if(color == y.color) return true;
else return false;
}
|
结构体
:
struct
Test {
int get_i() { return i; } // these are public
void put_i(int j) { i = j; } // by default
private
:
int
i;
};
共同体:
union
utype{
short int i;
char ch;
};
|
|
运算符重载:
type classname::operator#(arg-list)
{
//operations
}
ThreeD ThreeD::operator++()
ThreeD ThreeD::operator++(int notused)
//
后缀版本的
++
|
class
ThreeD {
int x, y, z; // 3-D coordinates
public
:
ThreeD() { x = y = z = 0; }
ThreeD(int i, int j, int k) { x = i; y = j; z = k; }
ThreeD operator+(ThreeD op2);
void show() ;
};
// Overload +.
ThreeD ThreeD::operator+(ThreeD op2)
{
ThreeD temp;
temp.x = x + op2.x;
temp.y = y + op2.x;
temp.z = z + op2.z;
return temp; //return *this;
}
|
继承的通式:
class
derived-class:access bade-class{
//body of derived class
Cylinder(colors c) { color = c; }
|
class
Rectangle : public TwoDShape {
// .. .
}
|
类声明的完整形式
:
class
classname{
//private members by default
protected
:
//protected members
public
:
//public members
};
|
|
调用基类构造函数
:
derived-constructor(arg-list):base-cons(arg-list);
{
body of derived constructor
}
|
Triangle(char *str, double w,
double h) : TwoDShape(w, h) {
strcpy(style, str);
}
|
虚函数
:
在声明前加关键字virtual
virtual
type func-name(parameter-list)
p->who();
通过基类指针调用
|
class
B {
public
:
virtual void who() {
cout << "Base/n";
}
};
class D1 : public B {
public
:
void who() {
cout << "First derivation/n";
}
};
class
D2 : public B {
public
:
void who() {
cout << "Second derivation/n";
}
};
|
纯虚函数
:
virtual
type func-name(parameter-list)=0
|
virtual
double area()=0;
|
异常处理
:
try
{
//try block
//throw exception;
catch
(type 1 arg){
//catch block
}
...
catch
(type n arg){
//catch block
}
|
catch(...) { // catch all exceptions
cout << "Caught One!/n";
}
}
}
|
指定由函数抛出的异常:
ret-type func-name(arg-list)throw(type-list){
//...
}
Throw;
再次抛出异常
|
void
Xhandler(int test) throw(int, char, double)
{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
|
通用函数
:
template
<class Ttype>ret-type func-name(parameter list)
{
//body of function
}
|
template
<class X> void swapargs(X &a, X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
template
<class Type1, class Type2>
void myfunc(Type1 x, Type2 y)
|
通用类
:
template
<class Ttype>class classname{
//body of class
}
Classname<type>ob;
创建类的特定实例
|
template
<class T> class MyClass {
template
<> class MyClass<int> {
类MyClass的显式具体化
|
动态分配符
:
p_var=new type;
delete
p_var;
初始化分配的内存:
p_var=new var_type(initializer);
给数组分配内存:
p_var=new array_type[size];
delete
[] p_var;
|
int
*p;
try {
p = new int; // allocate space for an int. // p = new int (87);
} catch (bad_alloc xa) {
cout << "Allocation Failure/n";
return 1;
}
|
命名空间
:
namespace
{
//declarations
}
|
|
Using
语句
:
using
namespace name;
using
name::member;
|
|
获取对象类型typeid:
typeid
(object)
|
int
i, j;
cout << "The type of i is: " <<
typeid
(i).name();
|
强制类型转换:
dynamic_cast
<target-type>(expr)
|
int
i, j;
cout << "The type of i is: " <<
typeid
(i).name();
base *bp,b_ob;
derives *dp,d_ob;
bp=&d_ob;
dp=dynamic_cast<derived *>(bp);
if
(dp) cout<<"cast ok";
|