Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here we have two classes
1)Base class Account
2)Derived class SavingAccount


SavingAccount class derived from Base class as a public inheritance.

C++
#include<iostream>
#include<vector>

class Account
{
public:

    virtual void PrintBalance()
    {
        std::cout<<"Base"<<std::endl;
    }
};

class SavingAccount
    : public Account
{
public:
   void PrintBalance()
   {
       std::cout<<"SavingAccount"<<std::endl;
   }
};



int main()
{
    1)Account* p = new SavingAccount();
    p->PrintBalance();

    2)Account* p1 = new Account();
    p1->PrintBalance();

    3)SavingAccount *p3 = new Account();
    p3->PrintBalance();

    4)SavingAccount* p4 = new SavingAccount();
    p4->PrintBalance();

    return 0;
};



consider 4 scaniro in main function.

please explain in each scaniro and output.


Regards,
Ranjith
Posted
Comments
Legor 12-Mar-14 6:37am    
Did you actually try to implement this and just SEE the output for yourself? What is the output of this program and then think about it.

No.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you want to explain it to us, we will willingly tell you if you are right - but I'm not writign it down so you can submit it as yours!
 
Share this answer
 
Running a C++ compiler would help you...
  1. Polimorphism.
  2. Standard method call.
  3. Error.
  4. Standard method call.
 
Share this answer
 
v2
Comments
ranjithkumar81 12-Mar-14 7:27am    
Can you please describe about scaniro 3 , why its error?
CPallini 12-Mar-14 7:42am    
Simple: OOP speaking, Account is not a SavingAccount (while the opposite is true).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900