Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:01:15+00:00 2026-05-25T20:01:15+00:00

I was messing around with inheritance in C++ and wanted to know if anyone

  • 0

I was messing around with inheritance in C++ and wanted to know if anyone had any insight on the way it functions. Code below

#include <iostream>
using namespace std;

class AA {
 int aa;
 public:
 AA() {cout<<"AA born"<<endl;}
~AA(){cout<<"AA killed"<<endl;} 
virtual void print(){ cout<<"I am AA"<<endl;}
};

class BB : public AA{
 int bb;
 public:
 BB() {cout<<"BB born"<<endl;}
~BB() {cout<<"BB killed"<<endl;}
 void print() {cout<<"I am BB"<<endl;}
};

class CC: public BB{
 int cc;
 public:
 CC() {cout<<"CC born"<<endl;}
~CC(){cout<<"CC killed"<<endl;}
 void print() {cout<<"I am CC"<<endl;}
};


int main()
{
 AA a;
 BB b;
 CC c;
 a.print();
 b.print();
 c.print();     
 return 0;
}

so I understand that when you inherit something you inherit constructors and destructors. So when I do , “BB b” it prints “AA born”. So the question I have

  1. Is an instance of AA created
  2. If yes, what is it called and how can I reference it?
  3. If no, why is the constructor being called
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:01:16+00:00Added an answer on May 25, 2026 at 8:01 pm

    Inheritance implements the “IS-A” relationship. Every BB is therefore also an AA.

    You can see this in a number of ways, the easiest to demonstrate is:

    BB b;
    AA *aptr = &b;
    

    Here your BB instance b is being pointed at by a pointer which only thinks of itself as pointing to an AA. If BB didn’t inherit from AA then that wouldn’t be legal.

    The interesting thing is that when you call:

    aptr->print();
    

    It still prints “I am BB”, despite the fact that the pointer you used is of type AA *. This happens because the print() method is virtual (i.e. polymorphic) and you’re using a pointer. (The same would also happen with a reference too, but the type must be one of those for this behaviour to happen)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just messing around with Visual Basic in Visual Studio 2010. Anyone know how
I am currently messing around with some code for an advertising network, i have
So I've been messing around with some JavaScript inheritance techniques and here's one case
I've been messing around with logical and bitwise expressions in C and wanted to
I'm messing around with bitwise operations and wanted to implement the simple logic puzzle
Messing around with 'extension functions' for the List module. (I spent quite a while
So I was messing around with JSFL, and I wanted to set the storke
While messing around with the custom formatting options in Eclipse, in one of the
I've been messing around with this for hours trying different ways of ordering by
I've been messing around with some coding and finally think I'm finally ready for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.