//5_4.cpp #include <iostream> using 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.cpp静态函数成员 #include <iostream> using 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; } 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(); Point::showCount(); //输出对象个数 Point b(a); //定义对象b,其构造函数回使count增1 cout << "Point B: " << b.getX() << ", " << b.getY(); Point::showCount(); //输出对象个数 return 0; }
//友元类 #include<bits/stdc++.h> #define endl ‘\n‘ #define _for(i,a,b) for(int i=a;i<b;i++) using namespace std; const int N = 1e5+5; typedef long long ll; class A { friend class B; public: void display() { cout << x << endl; } private: int x; }; class B { public: void set(int i); void display(); private: A a; }; void B::set(int i) { a.x=i; } void B::display() { a.display(); } int main(){ A a; a.display(); B b; b.set(4); b.display(); return 0; }
//友元函数 #include <bits/stdc++.h> 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(Point &a, Point &b); private: //私有数据成员 int x, y; }; float dist( Point& a, Point& b) { double x = a.x - b.x; double y = a.y - b.y; return static_cast<float>(sqrt(x * x + y * y)); } int main() { Point p1(1, 1), p2(4, 5); cout <<"The distance is: "; cout << dist(p1, p2) << endl; return 0; }
//例5-7 常成员函数举例 #include<iostream> 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; }
//例5-8 常数据成员举例 #include <iostream> using 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 <<endl; } int main() { /*建立对象a1和a2,并以100和0作为初值,分别调用构造 函数,通过构造函数的初始化列表给对象的常数据成员赋 初值*/ A a1(100), a2(0); a1.print(); a2.print(); return 0; }
//常引用做形参
#include <iostream> #include <cmath> 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<float>(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; }
//变量,生存期
#include<iostream> using namespace std; int i = 1; // i 为全局变量,具有静态生存期。 void other() { static int a = 2; static int b; // a,b为静态局部变量,具有全局寿命,局部可见。 //只第一次进入函数时被初始化。 int c = 10; // C为局部变量,具有动态生存期, //每次进入函数时都初始化。 a += 2; i += 32; c += 5; cout<<"---OTHER---\n"; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; b = a; } int main() { static int a;//静态局部变量,有全局寿命,局部可见。 int b = -10; // b, c为局部变量,具有动态生存期。 int c = 0; cout << "---MAIN---\n"; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; c += 8; other(); cout<<"---MAIN---\n"; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; i += 10; other(); return 0; }
原文:https://www.cnblogs.com/SunChuangYu/p/12577453.html