class Base {
      //private methods
      void printMyType() {
      cout << "this is base type" << endl;
     }
      
      //public methods
      void printAllInfo() {
      cout << "this An accout" << endl;
      printMyType();
      }
}; //end base class

class derived : public Base {

      //private method
      void printMyType() {
      cout << "this is derived type" << endl;
      }
 
      //public methods
      void printAllInfo() {
      Base::PrintAccountInfo();
      printMyType();
      }
}; //end derived class

Ok i have the above methods! Its an assingment so the public and private methods visabilites cannot be altered.

Now when I call the printAllInfo() from the base class it prints out the following
"this An accout"
"this is base type""this An accout"

I want to write correct the printAllInfo() method in the derived class so that
when I call it it prints

"this An accout"
"this is a derived account"

so in other words when I call the printAllInfo method in derived class the printMyType() method overwirtes the one in the base class and prints "this is a derived account"

thanks

P.S Note the visibility etc is implemented in a header file! So dont take this code as being too literal but i gives a general idea of what I have and what I want.

P.P.S virtual methods is also not a possibility because the printAccountType() methods are both private

pls help

The only way to do what you want is to use virtual functions. Make PrintMyType() in the base class a pure virtual function that has to be implented in the derived class.

Example:

class Base
{
private:
    virtual void PrintMyType() = 0;
public:
    void PrintAllInfo()
    {
        cout << "This is Account\n";
        PrintMyType();
    }
};

class derived : public Base
{
private:
    virtual void PrintMyType()
    {
        cout << "This is derived\n";
    }
public:
    virtual void PrintAllInfo()
    {
        Base::PrintAllTyes();
    }
};


int main()
{
    derived d;
    d.PrintAllInfo();
return 0;
}

The only way to do what you want is to use virtual functions. Make PrintMyType() in the base class a pure virtual function that has to be implented in the derived class.

Example:

class Base
{
private:
    virtual void PrintMyType() = 0;
public:
    void PrintAllInfo()
    {
        cout << "This is Account\n";
        PrintMyType();
    }
};

class derived : public Base
{
private:
    virtual void PrintMyType()
    {
        cout << "This is derived\n";
    }
public:
    virtual void PrintAllInfo()
    {
        Base::PrintAllTyes();
    }
};


int main()
{
    derived d;
    d.PrintAllInfo();
return 0;
}

ok thanks i`ll try it.

This article has been dead for over six months. Start a new discussion instead.