关键词搜索

源码搜索 ×
×

子类调用父类的构造函数

发布2013-10-01浏览9780次

详情内容

          在项目中,发现很多子类调用父类构造函数的情况,现在特地来熟悉一下:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int a;
  7. int b;
  8. public:
  9. A():a(0), b(0)
  10. {
  11. cout << "default A constructor" << endl;
  12. }
  13. A(int x, int y):a(x), b(y)
  14. {
  15. cout << "A constructor" << endl;
  16. }
  17. };
  18. class B : public A
  19. {
  20. public:
  21. int c;
  22. public:
  23. B():A(), c(0)
  24. {
  25. cout << "default B constructor" << endl;
  26. }
  27. B(int x, int y, int z):A(x, y), c(z)
  28. {
  29. cout << "B constructor" << endl;
  30. }
  31. };
  32. int main()
  33. {
  34. B b1;
  35. cout << b1.a << endl;
  36. cout << b1.b << endl;
  37. cout << b1.c << endl;
  38. B b2(1, 2, 3);
  39. cout << b2.a << endl;
  40. cout << b2.b << endl;
  41. cout << b2.c << endl;
  42. return 0;
  43. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载