在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
学习C++ -> 类的特殊数据成员
1 #include <iostream> 2 3 using namespace std; 4 5 class A 6 { 7 public: 8 A(int n = 0) { num = n; } //在构造函数内使用 n 初始化const型数据成员 num 9 void showNum() { cout<<"num = "<<num<<endl; } 10 private: 11 const int num; //const 型数据成员 12 }; 13 14 int main() 15 { 16 A a(10); 17 a.showNum(); 18 19 return 0; 20 }
error: uninitialized member 'A::num' with 'const' type 'const int' error: assignment of read-only data-member 'A::num' 1 #include <iostream> 2 3 using namespace std; 4 5 class A 6 { 7 public: 8 A(int n = 0):num(n) { } //使用初始化表达式对const型数据成员进行初始化 9 void showNum() { cout<<"num = "<<num<<endl; } 10 private: 11 const int num; 12 }; 13 14 int main() 15 { 16 A a(10); 17 a.showNum(); 18 19 return 0; 20 }
1 #include <iostream> 2 3 using namespace std; 4 5 class A 6 { 7 public: 8 A(int &rn):rNum(rn) { } //使用初始化表达式对引用型数据成员进行初始化 9 void showNum() { cout<<"num = "<<rNum<<endl; } 10 private: 11 int &rNum; //引用类型 12 }; 13 14 int main() 15 { 16 int x = 10, &rx = x; 17 A a(rx); //传递引用类型 18 a.showNum(); 19 20 return 0; 21 }
private: int xPos(10, 20); int yPos(100, 200);
1 #include <iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 public: 8 void printPoint() { cout<<"("<<xPos<<", "<<yPos<<")\n"; } 9 int xPos; //public类型的 xPos 和 yPos 10 int yPos; 11 }; 12 13 class Line 14 { 15 public: 16 Line(int x1, int y1, int x2, int y2) 17 { 18 M.xPos = x1; M.yPos = y1; //在构造函数内完成类对象的初始化 19 N.xPos = x2; N.yPos = y2; 20 } 21 void printLine() { M.printPoint(); N.printPoint(); } 22 private: 23 Point M; //类对象成员, M和N 24 Point N; 25 }; 26 27 int main() 28 { 29 Line L(10, 20, 100, 200); 30 L.printLine(); 31 32 return 0; 33 }
(10, 20) (100, 200) Process returned 0 (0x0) execution time : 0.500 s Press any key to continue.
1 #include <iostream> 2 3 using namespace std; 4 5 class Point 6 { 7 public: 8 Point(int x, int y) { xPos = x; yPos = y; } 9 void printPoint() { cout<<"("<<xPos<<", "<<yPos<<")\n"; } 10 private: 11 int xPos; //private类型的 xPos 和 yPos 12 int yPos; 13 }; 14 15 class Line 16 { 17 public: 18 Line(int x1, int y1, int x2, int y2):M(10, 20), N(100, 200) { ; } //借助初始化表达式完成对M,N的初始化 19 void printLine() { M.printPoint(); N.printPoint(); } 20 private: 21 Point M; //类对象成员, M和N 22 Point N; 23 }; 24 25 int main() 26 { 27 Line L(10, 20, 100, 200); 28 L.printLine(); 29 30 return 0; 31 }
1 #include <iostream> 2 3 using namespace std; 4 5 class Book 6 { 7 public: 8 Book() { ibookNumber++; } //通过构造函数访问static型数据ibookNumber并使其自增1 9 ~Book() { ibookNumber--; } //对象在被撤销时将static型数据ibookNumber并使其自减1 10 void showNum() { cout<<"Book number = "<<ibookNumber<<endl; } 11 private: 12 static int ibookNumber; //static类型数据成员 ibookNumber 13 }; 14 15 int Book::ibookNumber = 0; //在类外对static类型的数据成员进行初始化 16 17 int main() 18 { 19 Book A; Book B; Book C; Book D; Book E; //创建一些对象 20 A.showNum(); //使用对象A查看当前对象个数 21 B.showNum(); //使用对象B查看当前对象个数 22 cout<<"销毁一些对象...\n"; 23 B.~Book(); C.~Book(); //将B、C对象进行撤销 24 D.showNum(); //使用D对象查看剩余对象个数 25 E.showNum(); //使用E对象查看剩余对象个数 26 27 return 0; 28 } Book number = 5 Book number = 5 销毁一些对象... Book number = 3 Book number = 3 Process returned 0 (0x0) execution time : 0.266 s Press any key to continue.
--------------------
wid, 2013.02.23
上一篇: 学习C++ -> 复制构造函数
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论