码迷,mamicode.com
首页 > 其他好文 > 详细

继承与虚函数

时间:2019-06-09 20:50:16      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:ios   print   int   oid   std   stream   span   style   div   

 1 #include <string>
 2 #include <iostream>
 3 
 4 class A{
 5 public: 
 6     virtual std::string toString(){
 7     //std::string toString(){
 8         return "A";
 9     }
10 };
11 
12 class B: public A{
13 public: 
14     std::string toString(){
15         return "B";
16     }
17 };
18 
19 class C: public B
20 {
21 public: 
22     std::string toString(){
23         return "C";
24     }
25 };
26 
27 void print(A* o){
28     std::cout<<o->toString()<<std::endl;
29 }
30 
31 int main() {
32     A a;
33     B b;
34     C c;
35     print(&a);
36     print(&b);
37     print(&c);
38     return 0;
39 }

 

继承与虚函数

标签:ios   print   int   oid   std   stream   span   style   div   

原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10994717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!