Ok i have a parent class called MobileAccount, it has a public method called void PrintAccountInfo(), and within
this PrintAccountInfo() method I call a private method called void PrintAccountType().

Now as I mentioned the parent class called MobileAccount has a subclass called AdvancedAccount, it too has a method called PrintAccountInfo() decribed below.

void AdvancedAccount::PrintAccountInfo() {
	MobileAccount::PrintAccountInfo();
        PrintAccountType();
}

However because AdvancedAccount() has its own PrintAccountType() method I want it to overrun the
PrintAccountType() method within the MobileAccount::PrintAccountInfo().

The above code does not work, it prints the AccountType of MobileAccount, i.e when MobileAccount::PrintAccountInfo() is called, then it prints the account type of AvancedAccount.

Please can someone advise me of how to fix my code.

Since both PrintAccountInfo() mehtods are private it doesnt seem to like it when I declare the method
as virtual.

Please can someone help

Thanks

Make the function in MobileAccount a pure virtual function so that it has to be implemented by derived classes

class MobileAccount
{
....
protected:
   void PrintAccountInfo() = 0; // <<< pure virtual function
...
}

Then MobileAccount class doesn't have to know who implemented the function.

void AdvancedAccount::PrintAccountInfo() {
	PrintAccountInfo(); // <<<< here
        PrintAccountType();
}

I can't understand what for you define both methods in Mobile and Advanced classes and why you can't define virtual PrintAccountType to solve your problem.

class Mobile
{
public:
    void PrintInfo() {
        cout << "Mobile::PrintInfo; ";
        PrintType();
    }
private:
    virtual void PrintType() { 
        cout << "Mobile::PrintType\n"; 
    }
};
class Advanced: public Mobile
{
public:
    /* It seems no need in this method at all
    void PrintInfo() {
        cout << "Advanced::PrintInfo ";
        Mobile::PrintInfo();
        PrintType(); // why? Mobil::PrintInfo calls it!
    }
    */
private:
    void PrintType() { cout << "Advanced::PrintType\n"; }
};
int main()
{
    Mobil m;
    Advanced a;
    m.PrintInfo();
    a.PrintInfo();
    return 0;
}
/* Output:
Mobile::PrintInfo; Mobile::PrintType
Mobile::PrintInfo; Advanced::PrintType
*/

>>PrintType(); // why? Mobil::PrintInfo calls it!
No it doesn't. Mobile::PrintInfo calls Mobile::PrintType. Base class can not call derived class methods unless they are implemented as pure virtual functions in the base class.

I have often seen need for derived class to call base class implementation of a virtual method, such as

void Advanced::PrintType()
{
     Mobile::PrintType();
     // blabla
}

OH sorry!

I dont understand what you wrote? Please could your clarify?

void AdvancedAccount::PrintAccountInfo() {
        MobileAccount::PrintAccountInfo();
                MobileAccount::PrintAccountType();
}

Did you mean the above?

Thanks for your reply!

You carnt use the code you suggested, Its a worksheet assignment So I have a set specification/contract. So in both parent class and base class void PrintAccountType() is private and I cannot alter this.

So any suggested soultions must work with the data I gave you.

Thanks

>No it doesn't. Mobile::PrintInfo calls Mobile::PrintType . Base class can not call derived class methods unless they are implemented as pure virtual functions in the base class.
Pure virtual functions are not the most virtual or especially virtual ones. Base class calls derived class virtual methods as usually (try to compile and run my example).
>I have often seen need for derived class to call base class implementation of a virtual method...
Yes, it's a trivial fact, but it does not bear a relation to this case. Base class (non-virtual) PrintInfo calls the most derived virtual PrintType, so derived PrintInfo is redundant here. Of course, it's possible to define it in derived class but in that case no special needs to declare PrintType as a pure virtual.
Add yet another call to example snippet:

Mobile&& mref = a;
mref.PrintInfo();

I think it's an ordinar case of a polymorphic behaviour and no need in specialized methods. In any case it's of no importance that member functions are private or public...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.