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

  • SEARCH
  • Home
  • 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 3982300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:30:42+00:00 2026-05-20T05:30:42+00:00

I just want to find an answer to this question Let A be a

  • 0

I just want to find an answer to this question

Let A be a parent class with B,C the child classes.

Now we create objects as follows,

  1. A o1 = new A();
  2. B o2 = new B();
  3. C o3 = new C();

  4. A o4 = new B();

  5. B o5 = new A();
  6. C o6 = new B();

From these which will all create errors and why.. I got confused a lot because from 6 we know that it is an error because b may have some specialized variables and 7 is possible and 8 is not possible.

If i’m wrong please correct me and also,

  1. A o7 = o4;
  2. B 08 = o5;

Kindly suggest me the right answers and with explanations and also give me links for tutorials with this kind of puzzles.

  • 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-20T05:30:42+00:00Added an answer on May 20, 2026 at 5:30 am

    Assuming all objects are of pointer types-

    1. A *o1 = new A();   // Correct
    2. B *o2 = new B();   // Correct
    3. C *o3 = new C();   // Correct
    
    4. A *o4 = new B(); // A pointer to a derived class is type-compatible with a pointer to its base class. This is up casting which acts by default.
    5. B *o5 = new A(); // Wrong because the other-wise relationship of the above comment isn't true. Though there is a separate concept called Down-casting which isn't valid here.
    6. C *o6 = new B();  // Wrong : C, B has no hierarchial relationships
    
    7. A *o7 = o4;      // Correct because o4, o7 are both of same type
    8. B *o8 = o5;      // Wrong. Since, 5 itself is wrong.
    

    Explanation for 4 & 5:

    1. new B() calls A's constructor followed by B's constructor. So, we have sub-objects of type A*,B*. Since, there is a sub-object of type A*, the lvalue can point to it which is also equal to of type A*. This is helpful to access base class overridden virtual methods in derived class.

    2. new A() constructs an object of type A* and returns it’s address. So, return type is of A* but the receiving type is of B*. So, they both are incompatible and is wrong.

    Example: Output results

    #include <iostream>
    using namespace std;
    class A
    {
        public:
        A(){
            cout << " \n Constructor A \n";
        }
        virtual ~A(){
            cout << "\n Destructor A \n";
        }
    };
    
    class B: public A
    {
        public:
        B(){
            cout << " \n Constructor B \n";
        }
        ~B(){
            cout << "\n Destructor B \n";
        }
    

    };

    class C: public A
    {
        public:
        C(){
            cout << " \n Constructor C \n";
        }
        ~C(){
            cout << "\n Destructor C \n";
        }
    

    };

    int main()
    {
        A* obj1 = new A;
        std::cout<< "******************************" << std::endl;
    
        A* obj2 = new B;
        std::cout<< "******************************" << std::endl;
    
        A* obj3 = new C;
        std::cout<< "******************************" << std::endl;
    
        delete obj1;
        std::cout<< "******************************" << std::endl;
    
        delete obj2;
        std::cout<< "******************************" << std::endl;
    
        delete obj3;
        std::cout<< "******************************" << std::endl;
    
        return 0;
    }
    

    Results:

    Constructor A


    Constructor A

    Constructor B


    Constructor A

    Constructor C


    Destructor A


    Destructor B

    Destructor A


    Destructor C

    Destructor A


    Notice that the order of destruction is reverse to the order of construction.

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

Sidebar

Related Questions

I can't seem to find an answer on this and just want to make
This may be very simplistic question but I can't find the answer. I want
i just want to find out the name of this grid to refresh it.
I was surprised not to find an answer to this question, maybe is something
I have a very easy question. Unfortunately, I can't find the answer. I just
I've read every answer available for this question that I could find, including this
I bet that I could find the answer of this question from reading similar
Seen this question asked a lot but can't seem to find an answer. So
Hello I searched for an answer to this question but didn't find any here.
MySQL noob here; looked around first but couldn't find the answer to this question.

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.