本文共 2702 字,大约阅读时间需要 9 分钟。
数据类型:
Type | Meaning | Minimum Size |
---|---|---|
C++: Arithmetic Types | ||
bool | boolean | NA |
char | character | 8 bits |
wchar_t | wide character | 16 bits |
short | short integer | 16 bits |
int | integer | 16 bits |
long | long integer | 32 bits |
float | single-precision floating-point | 6 significant digits |
double | double-precision floating-point | 10 significant digits |
long double | extended-precision floating-point | 10 significant digits |
数组:
string:
被string声明的是对象.
跟C差别较大,尽量使用string.
结构:
结构数组:
枚举:
指针:
1.虚函数:基类中定义virtual函数,被两个类分别继承和重写,在通过基类指针调用,不同的实体指针出不同的结果.
(1) 虚函数在重新定义时参数的个数和类型必须和基类中的虚函数完全匹配,这
一点和函数重载完全不同。
(2) 只有通过基类指针才能实现虚函数的多态性,若虚函数的调用是普通方式来
进行的,则不能实现其多态性。
(3) 如果不使用new来创建相应的派生类对象指针,也可使用通过&运算符来获取
对象的地址。
(4) 虚函数必须是类的一个成员函数,不能是友元函数,也不能是静态的成员函
数。
(5) 可把析构函数定义为虚函数,但不能将构造函数定义为虚函数。
C++的虚函数就相当于java的普通函数,被继承后可以根据实例调用对应的函数。
C++ Java
虚函数 -------- 普通函数 纯虚函数 -------- 抽象函数 抽象类 -------- 抽象类基类成员函数的二义性:相当于近亲(继承统一先类)结婚,后代的某个先类基因("父母"都有),无法确定是从哪方继承过来。
可以通过::直接指定希望从哪方继承而来.
2.纯虚函数:
class CTriangle:public CShape
{
public:
CTriangle(float h, float w)
{ H = h; W = w; }
float area() // 在派生类定义纯虚函数的具体实现代码
{ return (float)(H * W * 0.5); }
private:
float H, W;
};
运算符重载
<函数类型><类名>::operator <重载的运算符>(<形参表>)
{ … } // 函数体
CComplex CComplex::operator + (CComplex &c)
CCounter CCounter::operator ++()
CCounter CComplex::operator ++(int);
转换函数:
CMoney::operator string ()
Vector:
string str[]={"Alex","John","Robert"};
// empty vector object vector<int> v1; // creates vector with 10 empty elements vector<int> v2(10); // creates vector with 10 elements, // and assign value 0 for each vector<int> v3(10,0); // creates vector and assigns // values from string array vector<string> v4(str+0,str+3); vector<string>::iterator sIt = v4.begin(); while ( sIt != v4.end() ) cout << *sIt++ << " "; cout << endl;Deque:
string str[]={"Alex","John","Robert"};
// empty deque object deque<int> d1; // creates deque with 10 empty elements deque<int> d2(10); // creates deque with 10 elements, // and assign value 0 for each deque<int> d3(10,0); // creates deque and assigns // values from string array deque<string> d4(str+0,str+3); deque<string>::iterator sIt = d4.begin(); while ( sIt != d4.end() ) cout << *sIt++ << " "; cout << endl;List:
int ary[]={1,2,3,4,5};
list<int> l;// assign to l the contains of ary
l.assign(ary,ary+5);copy(l.begin(),l.end(),
ostream_iterator<int>(cout," ")); cout << endl;// replace l for 3 copies of 100
l.assign(3,100);copy(l.begin(),l.end(),
ostream_iterator<int>(cout," ")); cout << endl;typedef Member<string,double> M;
list<M> l;l.push_back(M("Robert",60000));
l.push_back(M("Linda",75000));list<M>::iterator It = l.begin();
cout << "Entire list:" << endl;while ( It != l.end() )
(It++)->print(); cout << endl;Set:
Multiset:
Map:
转载地址:https://blog.csdn.net/wellsmith/article/details/96647179 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!