设为首页 加入收藏

TOP

9.9.2 C++/CLI类的继承(2)
2013-10-07 12:38:14 来源: 作者: 【 】 浏览:66
Tags:9.9.2 /CLI 继承

9.9.2  C++(www.cppentry.com)/CLI类的继承(2)

默认的public基类是Box。该类的其余部分实质上与以前相同。

下面是Stack类的定义:

  1. // Stack.h for Ex9_14  
  2. // A push-down stack to store objects of any ref class type  
  3. #pragma once  
  4.  
  5. ref class Stack  
  6. {  
  7. private:  
  8. // Defines items to store in the stack  
  9. ref struct Item  
  10. {  
  11. Object^ Obj;    // Handle for the object in this item  
  12. Item^ Next;         // Handle for next item in 
    the stack or nullptr  
  13.  
  14. // Constructor  
  15. Item(Object^ obj, Item^ next): Obj(obj), Next(next){}  
  16. };  
  17.  
  18. Item^ Top;  // Handle for item that is at the top  
  19.  
  20. public:  
  21. // Push an object on to the stack  
  22. void Push(Object^ obj)  
  23. {  
  24. Top = gcnew Item(obj, Top);     // Create new
    item and make it the top  
  25. }  
  26.  
  27. // Pop an object off the stack  
  28. Object^ Pop()  
  29. {  
  30. if(Top == nullptr)  // If the stack is empty  
  31. return nullptr;         // return nullptr  
  32.  
  33. Object^ obj = Top->Obj;         // Get object from item  
  34. TopTop = Top->Next;        // Make next item the top  
  35. return obj;  
  36. }  
  37. }; 

需要注意的第一点区别是函数的形参和字段现在都是句柄,因为我们是在处理引用类对象。内部的结构Item现在存储着Object^类型的句柄,这样的句柄允许在堆栈中存储任何CLR类类型的对象,这意味着无论是数值类还是引用类都能被压入堆栈-- 这一点是对本地C++(www.cppentry.com)中CStack类的重大改进。我们不必担心调用Pop()函数时删除Item对象的问题,因为这项任务由垃圾回收器负责。

下面总结了这些类同本地C++(www.cppentry.com)类的区别:

只有引用类可以是派生类。

派生引用类的基类始终都是public。

引用类中没有定义的函数是抽象函数,必须使用abstract关键字声明。

我们必须通过在类名后面放上abstract关键字,将包含一个或多个抽象函数的类显式指定为抽象类。

不包含抽象函数的类也可以被指定为abstract,这种情况下我们将不能定义该类的实例。

当指定某个重写基类函数的函数时,我们必须显式使用override关键字。

试验这些类只需一个包含main()定义的CLR控制台项目即可,下面我们就来完成该项目。

试一试:使用派生的引用类

创建一个名为Ex9_14的CLR控制台项目,将前面几个类添加到该项目中,然后给Ex9_14.cpp文件添加下面的内容:

  1. // Ex9_14.cpp : main project file.  
  2. // Using a nested class to define a stack  
  3.  
  4. #include "stdafx.h"  
  5. #include "Box.h"    // For Box and Container  
  6. #include "GlassBox.h"       // For GlassBox (and Box and Container)  
  7. #include "Stack.h"      // For the stack class with nested struct Item  
  8.  
  9. using namespace System;  
  10.  
  11. int main(array<System::String ^> ^args)  
  12. {  
  13. array<Box^>boxes = {  gcnew Box(2.0, 3.0, 4.0),  
  14.                                gcnew GlassBox(2.0, 3.0, 4.0),  
  15.                                gcnew Box(4.0, 5.0, 6.0),  
  16.                                gcnew GlassBox(4.0, 5.0, 6.0)  
  17.                              };  
  18. Console::WriteLine(L"The array of boxes have the following volumes:");  
  19. for each(Box^ box in boxes)  
  20. box->ShowVolume();      // Output the volume of a box  
  21.  
  22. Console::WriteLine(L"\nNow pushing the boxes on the stack...");  
  23.  
  24. Stack^ stack = gcnew Stack;         // Create the stack  
  25. for each(Box^ box in boxes)  
  26. stack->Push(box);  
  27.  
  28. Console::WriteLine(  
  29. L"Popping the boxes off the stack presents them in reverse order:");  
  30. Object^ item;  
  31. while((item = stack->Pop()) != nullptr)  
  32. safe_cast<Container^>(item)->ShowVolume();  
  33.  
  34. Console::WriteLine(L"\nNow pushing integers on to the stack:");  
  35. for(int i = 2 ; i<=12 ; i += 2)  
  36. {  
  37. Console::Write(L"{0,5}",i);  
  38. stack->Push(i);  
  39. }  
  40.  
  41. Console::WriteLine(L"\n\nPopping integers off the stack produces:");  
  42. while((item = stack->Pop()) != nullptr)  
  43. Console::Write(L"{0,5}",item);  
  44.  
  45. Console::WriteLine();  
  46. return 0;  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇9.9.2 C++/CLI类的继承(3) 下一篇9.9.2 C++/CLI类的继承(1)

评论

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