疑问:
Point(int x = 0, int y = 0) : x(x), y(y){
}//通过构造函数的初始化列表给对象的常数据成员赋初值
- x(x),y(y)是什么意思?
通过构造函数的初始化列表给对象的常数据成员赋初值 - 为什么静态函数成员只能访问静态数据成员?
- 值传递 vs 引用传递(双向)
- 哪些操作会有试图改变常对象状态的危险?
采用常引用friend float dist(const Point &p1,const Point &p2); - 静态数据成员在类内声明,类外进行初始化并使用类名限定
- 常数据成员可以在定义时直接初始化(C++11)
例5-4
//5_4.cpp #includeusing namespace std; class Point { //Point类定义 public: //外部接口 Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数 //在构造函数中对count累加,所有对象共同维护同一个count count++; } Point(Point &p) { //复制构造函数 x = p.x; y = p.y; count++; } ~Point() { count--; } int getX() { return x; } int getY() { return y; } void showCount() { //输出静态数据成员 cout << " Object count = " << count << endl; } private: //私有数据成员 int x, y; static int count; //静态数据成员声明,用于记录点的个数 }; int Point::count = 0;//静态数据成员定义和初始化,使用类名限定 int main() { //主函数 Point a(4, 5); //定义对象a,其构造函数会使count增1 cout << "Point A: " << a.getX() << ", " << a.getY(); a.showCount(); //输出对象个数 Point b(a); //定义对象b,其构造函数会使count增1 cout << "Point B: " << b.getX() << ", " << b.getY(); b.showCount(); //输出对象个数 return 0; }
例 5-5 具有静态数据、函数成员的 Point 类
#includeusing namespace std; class Point { public: Point(int x = 0, int y = 0) : x(x), y(y) { count++; }//构造函数 Point(Point &p) { //复制构造函数 x = p.x; y = p.y; count++; } ~Point() { count--; } int getX() { return x; } int getY() { return y; } static void showCount() { cout << "Object count = " << count << endl; } private: int x, y; static int count; //静态数据成员声明,用于记录点的个数 }; int Point::count = 0;//静态数据成员定义和初始化,使用类名限定 int main() { Point a(4, 5); //定义对象a,其构造函数会使count增1 cout << "Point A: " << a.getX() << ", " << a.getY() << endl; a.showCount(); //输出对象个数 //Point::showCount();是一样的 Point b(a); //定义对象b,其构造函数会使count增1 cout << "Point B: " << b.getX() << ", " << b.getY() << endl; b.showCount(); //输出对象个数 return 0; }
类的友元
封装隐藏(安全性)和效率(快速性)中找平衡、
共享数据的保护 常对象#include常成员函数using namespace std; class A { public: A(int i,int j) { x=i; y=j;} private: int x,y; }; int main(){ A const a(3,4);//a是常对象,不能被更新 }
#include常数据成员using namespace std; class R { public: R(int r1, int r2) : r1(r1), r2(r2) { } void print(); void print() const;//区分重载函数 private: int r1, r2; }; void R::print() { cout << r1 << ":" << r2 << endl; } void R::print() const { cout << r1 << ";" << r2 << endl; } int main() { R a(5,4); a.print(); //调用void print() const R b(20,52); b.print(); //调用void print() const return 0; }
#includeusing namespace std; class A { public: A(int i); void print(); private: const int a; static const int b; //静态常数据成员 //在类体内声明 }; const int A::b=10;//在类体外初始化 A::A(int i) : a(i) { }//通过构造函数的初始化列表给对象的常数据成员赋初值 void A::print() { cout << a << ":" << b < 常引用 例题 常引用做形参
#include#include using namespace std; class Point { //Point类定义 public: //外部接口 Point(int x = 0, int y = 0) : x(x), y(y) { } int getX() { return x; } int getY() { return y; } friend float dist(const Point &p1,const Point &p2); private: //私有数据成员 int x, y; }; float dist(const Point &p1, const Point &p2) { double x = p1.x - p2.x; double y = p1.y - p2.y; return static_cast (sqrt(x*x+y*y)); } int main() { //主函数 const Point myp1(1, 1), myp2(4, 5); cout << "The distance is: "; cout << dist(myp1, myp2) << endl; return 0; }



