从C#重返C++学习过程总结
2007-05-22 12:27
501 查看
1.类
类在设计时前面没有访问修饰符,统一为Class;类的成员定义时(包括函数成员,数据成员),才使用访问修饰符;而且是分类放置,比如,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void Function1(int x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
类中函数实现是在类的外面实现的(下面讲的隐式内联函数可放在内部),与普通函数不同的是在实现函数时要指明类的名称:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Test::Function1(int x)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
类中成员函数可以带默认形参值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Test::Function1(int x=500)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
构造函数:如果没有定义构造函数,编译系统会在编译时自动生成一个默认形式的构造函数,它不做任何事情.
2.内联函数
内联函数就是小型函数,牺牲空间来节省函数调用的开销,一般用作比较小的函数,即函数内部没有循环、开关语句等。内联函数被发明出来
就是为了取代C中的宏,因为宏是单纯的替换而没有类型检查所以经常出毛病
隐式声明:将函数体直接放在类主体中
显示声明:函数体实现时加上inline,如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
inline void Clock::ShowTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.双冒号::
在C++里的,::是“域操作符”。比如声明了一个类A,类A里声明了一个成员函数void f(),但没有在类的声明里给出f的定义,那么在类外定义f时,就要写成void A::f(),表示这个f()函数是类A的成员函数。
::另外的用法: 在派生类中覆盖基类同名成员时,使用::可以访问已经被覆盖了的基类的同名成员(public),如:obj.Class1::Function1();
::另外的用法: 就是直接用在全局函数前,表示是全局函数。比如在VC里,你可以在调用API函数里,在API函数名前加::。
4.new 和 delete
在C++中,new和delete是建立和删除堆对象的两个运算符
new 动态分配内存,或称动态创建堆对象,格式:new 类型名 (初始列表)
a.建立普通对象就是赋值操作
如: int *point;
point = new int(2); 表示动态分配了用于存放int类型数据的空间,并把初始值2放入该空间,然后将首地址赋值给指针point
b.建立对象是类的一个实例对象,根据实际情况调用构造函数.用delete来删除new来创建的对象,释放指针所指向的内存空间.
如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Point
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ X=Y=0; cout<<"调用默认构造函数Point()"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point(int xx,int yy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ X=xx; Y=yy; cout<<"调用有参构造函数Point(int xx,int yy)"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
~Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ cout<<"Destroy Point()"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int GetX()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ return X; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int GetY()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ return Y; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int X,Y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point *point1=new Point;//没有给出初始值,因此调用默认构造函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete point1;//删除对象,自动调用~Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
point1=new Point(1,2);//调用有参构造函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete point1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* //输出结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
调用默认构造函数Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Destroy Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
调用有参构造函数Point(int xx,int yy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Destroy Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Press any key to continue
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c.创建数组对象
......
类在设计时前面没有访问修饰符,统一为Class;类的成员定义时(包括函数成员,数据成员),才使用访问修饰符;而且是分类放置,比如,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void Function1(int x);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
类中函数实现是在类的外面实现的(下面讲的隐式内联函数可放在内部),与普通函数不同的是在实现函数时要指明类的名称:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Test::Function1(int x)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
类中成员函数可以带默认形参值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Test::Function1(int x=500)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
构造函数:如果没有定义构造函数,编译系统会在编译时自动生成一个默认形式的构造函数,它不做任何事情.
2.内联函数
内联函数就是小型函数,牺牲空间来节省函数调用的开销,一般用作比较小的函数,即函数内部没有循环、开关语句等。内联函数被发明出来
就是为了取代C中的宏,因为宏是单纯的替换而没有类型检查所以经常出毛病
隐式声明:将函数体直接放在类主体中
显示声明:函数体实现时加上inline,如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
inline void Clock::ShowTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.双冒号::
在C++里的,::是“域操作符”。比如声明了一个类A,类A里声明了一个成员函数void f(),但没有在类的声明里给出f的定义,那么在类外定义f时,就要写成void A::f(),表示这个f()函数是类A的成员函数。
::另外的用法: 在派生类中覆盖基类同名成员时,使用::可以访问已经被覆盖了的基类的同名成员(public),如:obj.Class1::Function1();
::另外的用法: 就是直接用在全局函数前,表示是全局函数。比如在VC里,你可以在调用API函数里,在API函数名前加::。
4.new 和 delete
在C++中,new和delete是建立和删除堆对象的两个运算符
new 动态分配内存,或称动态创建堆对象,格式:new 类型名 (初始列表)
a.建立普通对象就是赋值操作
如: int *point;
point = new int(2); 表示动态分配了用于存放int类型数据的空间,并把初始值2放入该空间,然后将首地址赋值给指针point
b.建立对象是类的一个实例对象,根据实际情况调用构造函数.用delete来删除new来创建的对象,释放指针所指向的内存空间.
如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Point
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ X=Y=0; cout<<"调用默认构造函数Point()"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point(int xx,int yy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ X=xx; Y=yy; cout<<"调用有参构造函数Point(int xx,int yy)"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
~Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ cout<<"Destroy Point()"<<endl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int GetX()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ return X; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int GetY()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{ return Y; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int X,Y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point *point1=new Point;//没有给出初始值,因此调用默认构造函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete point1;//删除对象,自动调用~Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
point1=new Point(1,2);//调用有参构造函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete point1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* //输出结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
调用默认构造函数Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Destroy Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
调用有参构造函数Point(int xx,int yy)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Destroy Point()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Press any key to continue
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c.创建数组对象
......
相关文章推荐
- 黑马程序员_C#面向过程学习总结1
- C++程序员的C#转型手册之二:学习过程
- C#学习过程中vs使用和代码编辑技巧总结
- 个人学习C++过程中对const的总结:初始化系列之用字面值常量与其他类型的值初始化的区别(一)
- 黑马程序员_C#面向过程学习总结2
- C++程序员的C#转型手册之二:学习过程
- C和C++的学习过程总结
- c#学习之与c++的语法区别总结
- 在学习C#过程中的一些总结和收集的一些材料望对初学者有用
- c#利用SWIG调用c++dll学习总结
- 用c/c++语言写的一个小的“爬虫”程序学习过程总结
- 学习Python过程中遇到的错误总结
- 学习红黑树过程中的个人总结
- C++程序设计-2014年11月-月末学习总结
- c++关于数组,指针的学习总结
- Hadoop学习过程遇到的问题总结
- 学习WCF过程的总结
- C++ 学习总结 复习篇
- C++学习,总结一
- [C++再学习系列] STL容器删除操作总结