设为首页 加入收藏

TOP

9.3.1 派生类中构造函数的操作(1)
2013-10-07 12:35:24 来源: 作者: 【 】 浏览:64
Tags:9.3.1 派生 构造 函数 操作

9.3.1  派生类中构造函数的操作(1)

虽然前面说派生类不会继承基类的构造函数,但它们仍然存在于基类中,并且要用于创建派生类对象的基类部分。因为创建派生类对象的基类部分实际上属于基类构造函数而非派生类构造函数的任务。我们毕竟已经看到,基类的私有成员在派生类中虽然被继承下来,但却是不可访问的,因此这些任务必须交给基类的构造函数来完成。

在上一个示例中,默认的基类构造函数被自动调用,以创建派生类对象的基类部分,但情况不是必须这样。我们可以在派生类的构造函数中安排调用特定的基类构造函数,这样就能用非默认的构造函数初始化基类的数据成员,实际上就是可以根据给派生类构造函数提供的数据,选择调用特定的类构造函数。

试一试:调用构造函数

通过下面这个对上一个示例作了修改的版本,我们可以了解构造函数的实际工作情况。为了使派生类便于使用,我们确实需要给它提供一个允许指定对象尺寸的构造函数。为此,我们可以在派生类中再添加一个构造函数,从中显式调用基类的构造函数,从而设定那些从基类继承的数据成员的值。

在Ex9_03项目中,Box.h文件包含以下代码:

  1. // Box.h in Ex9_03  
  2. #pragma once  
  3. #include <iostream> 
  4. using std::cout;  
  5. using std::endl;  
  6.  
  7. class CBox  
  8. {  
  9. public:  
  10. // Base class constructor  
  11. CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):  
  12. m_Length(lv), m_Width(wv), m_Height(hv)  
  13. { cout << endl << "CBox constructor called"; }  
  14.  
  15. //Function to calculate the volume of a CBox object  
  16. double Volume() const  
  17. { return m_Length*m_Width*m_Height; }  
  18.  
  19. private:  
  20. double m_Length;  
  21. double m_Width;  
  22. double m_Height;  
  23. }; 

CandyBox.h头文件应该包含下面的代码:

  1. // CandyBox.h in Ex9_03  
  2. #pragma once  
  3. #include <iostream> 
  4. #include "Box.h"  
  5. using std::cout;  
  6. using std::endl;  
  7.  
  8. class CCandyBox: public CBox  
  9. {  
  10. public:  
  11. char* m_Contents;  
  12.  
  13. // Constructor to set dimensions and contents  
  14. // with explicit call of CBox constructor  
  15. CCandyBox(double lv, double wv, double hv,
    char* 
    str = "Candy")  
  16.                                          
    :CBox(lv, wv, hv)  
  17. {  
  18. cout << endl <<"CCandyBox constructor2 called";  
  19. m_Contents = new char[ strlen(str) + 1 ];  
  20. strcpy_s(m_Contents, strlen(str) + 1, str);  
  21. }  
  22.  
  23. // Constructor to set contents  
  24. // calls default CBox constructor automatically  
  25. CCandyBox(char* str = "Candy")  
  26. {  
  27. cout << endl << "CCandyBox constructor1 called";  
  28. m_Contents = new char[ strlen(str) + 1 ];  
  29. strcpy_s(m_Contents, strlen(str) + 1, str);  
  30. }  
  31.  
  32. ~CCandyBox()                                 

    // Destructor  
  33. { delete[] m_Contents; }  
  34. }; 

嵌入<iostream>头文件的#include指令和两个using声明在这里不是绝对必要的,因为Box.h文件包含相同的代码,但放在这里也没有什么坏处。相反,加上这几条语句意味着如果因不再需要而从Box.h文件中将相同的代码删除,那么CandyBox.h仍然能够编译。

Ex9_03.cpp文件的内容如下:

  1. // Ex9_03.cpp  
  2. // Calling a base constructor from a derived class constructor  
  3. #include <iostream>     // For stream I/O  
  4. #include <cstring>      // For strlen() and strcpy()  
  5. #include "CandyBox.h"               // For CBox and CCandyBox  
  6. using std::cout;  
  7. using std::endl;  
  8.  
  9. int main()  
  10. {  
  11. CBox myBox(4.0, 3.0, 2.0);  
  12. CCandyBox myCandyBox;  
  13. CCandyBox myMintBox(1.0, 2.0, 3.0, "Wafer Thin Mints");  
  14.  
  15. cout << endl 
  16. << "myBox occupies " << sizeof myBox              
    // Show how much memory  
  17. << " bytes" << endl // the objects require  
  18. << "myCandyBox occupies " << sizeof myCandyBox  
  19. << " bytes" << endl 
  20. << "myMintBox occupies " << sizeof myMintBox  
  21. << " bytes";  
  22. cout << endl 
  23. << "myMintBox volume is " // Get volume of a  
  24. << myMintBox.Volume();      // CCandyBox object  
  25. cout << endl;  
  26. return 0;  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇9.3.1 派生类中构造函数的操作(2.. 下一篇9.4 派生类中的复制构造函数(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: