例5-7常成员函举例
代码部分:
#include<iostream> using namespace std; class R { private: int r1, r2; public: R(int r1, int r2):r1(r1),r2(r2){} void print(); void print()const; }; void R::print() { cout << r1 << ":" << r2 << endl; } void R::print()const { cout << r1 << ";" << r2 << endl; } int main() { R a(5, 4); a.print(); const R b(20, 52); b.print(); return 0; }
例5-8
常数据成员举例
代码部分:
#include<iostream> using namespace std; class A { private: const int a; static const int b; public: A(int i); void print(); }; const int A::b = 10; A::A(int i):a(i){} void A::print() { cout << a << ";" << b << endl; } int main() { A a1(100), a2(0); a1.print(); a2.print(); return 0; }
标签:const,r1,r2,23,int,void,print,打卡 From: https://www.cnblogs.com/xuechenhao173/p/17425509.html