特征:抽象-封装-继承-多态函数
class 类名 { private: 私有成员(本类) public: 公共成员(全部) protected: 保护成员(继承类) }
class 结构体名 { private: 私有成员(本类) public: 公共成员(全部) protected: 保护成员(继承类) }
union 联合体名 { private: 私有成员(本类) public: 公共成员(全部) protected: 保护成员(继承类) }
enum class 枚举类型名:类型 { 枚举1,枚举2...枚举n }
class Demo { public: //默认生成空函数体的构造器 //若自定义构造器,系统再也不生成默认构造器 //若要系统生成默认构造器,指明便可 Demo()=default; Demo(int x,int y); private: int x,y; }
Demo(int x,int y) { this->x=0; this->y=0; } Demo():x(0),y(0){}
Demo():Demo(0,0){}
调用复制构造器的状况:
1.以本类对象初始化新对象
2.实参初始化形参
3.return类的对象this
class Point { private: int x, y; public: Point():x(0),y(0){} Point(int x, int y); Point(const Point& p); }; Point::Point(int x, int y) { this->x = x; this->y = y; } Point::Point(const Point& p) { this->x = p.x; this->y = p.y; cout << "复制构造函数运行中..." << endl; }
class Point { ~Point(); }
局部做用域:形参和代码块中的标识符
类做用域:类内成员
静态生存期:与程序运行期相同(static变量)
动态生存期:从声明开始到做用域结束code
构造构造器初始化表(按成员变量在类中的定义顺序)
执行构造器体对象
class 类名;继承
friend double view(const Point &p1,const Point &p2) { double x = p1.x - p2.x; double y = p1.y - p2.y; return sqrt(x * x + y * y); }
class A { friend class B; public: void view() { cout << x << endl; } private: int x; }; class B { public: void set(int i) { a.x = 1; } void view() { a.view(); } private: A a; };