001:注意,在C语言中,struct 定义的结构体,在使用的时候需要将它显视的写出来,而C++中,struct和class关键字在定义变量的时候可以省略。
002:C++中,struct默认所有成员存取说明是public的,同一种说符可以在结构中多次出现,这样就不一定非要将所有共有的放在一起,所有的私有的放在一起了。
003:友元函数,不论在私有部分声明还是共有部分,都是一样的,即:有完全的权限去访问类的所有成员。
004:类的所有成员都可以变成另一个类的友元。
005:struct 默认继承是共有,内部成员默认也是共有;class默认继承是似有,内部成员默认也是私有。
006:C++中的类可以多继承,JAVA中的类只能单继承,不要弄混淆了。
例如:
#include<iostream>
using namespace std;
class one
{
public:
one(){cout << "one" << endl;}
};
class three
{
public:
three(){cout << "three" << endl;}
};
class two:public one, three
{
int b;
public:
two(int j)
{
b = j;
cout << "two" << endl;
}
};
int main()
{
two o(20);
cout << "main" << endl;
return 0;
}
007:虽然不允许声明含有纯虚函数的类的实例,但是可以声明含有纯虚函数的类的指针,这样对于运行时多态性是很有必要的。
008:
重载操作符号
bool operator <(const Time & a,const Time & b){return false;}
bool operator >(const Time & a,const Time & b){return false;}
Time operator +(Time a,Time b)
{
Time t;
t.hour = a.hour + b.hour;
return t;
}
istream & operator >>(istream & is,Time & t)
{
return is>>t.date>>t.time;
}
ostream & operator <<(ostream & os,Time & t)
{
return os<<t.date<<" "<<t.time;
}