小编Rav*_*han的帖子

如何在没有基类的对象时访问虚函数的内容

我在这里有一段实现多态的代码.我想访问虚函数的内容而不影响我以前的输出.

任何人都可以帮我吗?

class parent{

      private:
              int m,n;
      public:
              parent(int x,int y): m(x),n(y)
              {
                   cout<<"parent constructor called";
              }
              virtual void display()
              {
                   cout<<"\n"<<m<<"\t"<<n;
                   cout<<"\nParent display called";
              }      
      };

class child: public parent{

      private: 
               int a,b;
      public:
             child(int x,int y, int c, int d): parent(x,y),a(c), b(d)
             {
                    cout<<"\nchild constructor called";
             }
             void display()
              {
                   cout<<"\n"<<a<<"\t"<<b;
                    cout<<"\nChild display called";
              }   
      };      

int main()
{

    child c(4,5,6,7);
    parent *p=&c;
    p->display();
    getch();
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance function

2
推荐指数
1
解决办法
70
查看次数

标签 统计

c++ ×1

function ×1

inheritance ×1