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 7710257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:04:53+00:00 2026-06-01T01:04:53+00:00

What i know as of now that a constructor is called when an object

  • 0

What i know as of now that a constructor is called when an object is created. I also know that its not possible to create an object of an Abstract Class.

But when i run this piece of code i see the following :-

#include <iostream>

using namespace std;

class Pet {
public:
    Pet(){cout<<"in base constructor\n";}

    virtual ~Pet() = 0;  //making pet abstract by making drstructor pure virtual
};

Pet::~Pet() {
    cout << "~Pet()" << endl;
}

class Dog : public Pet {
    public:
    Dog(){cout<<"in drvd constructor\n";}

    ~Dog() {
        cout << "~Dog()" << endl;
    }
};

int main() {
    Pet* p = new Dog; // Upcast
    delete p; // Virtual destructor call
    return 0;
}

When compiled and run its output is :-

in base constructor
in drvd constructor
~Dog()
~Pet()

why is constructor for Pet getting called even though its an abstract class and no object creation is allowed for it? So it boils down to finally is constructor called only in case of object creation?

  • 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-06-01T01:04:54+00:00Added an answer on June 1, 2026 at 1:04 am

    its not possible to create an object of an Abstract Class

    Don’t take this ad-litteram. You can’t create an object whose actual type is abstract.

    But, if you implement that class (extend it and implement all pure virtual methods in it) and instantiate the new class, an object of the original abstract base class will be created as part of the new class.

    Inheritance is a is-a relationship. Dog is a Pet. When you create a Dog, you create a Pet. But you can’t create a Pet on its own.

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

Sidebar

Related Questions

i have a file named functions.h....now i know that it is not wise to
So I know now that the debug assemblies have been intentionally left out of
Now that I know C++ I want to get into desktop application that have
Now that I know I can no longer communicate with Twitter mashups out there,
Other than that I don't know if I can reproduce it now that it's
Now that LINQ to SQL is a little more mature, I'd like to know
Now that Google Earth has been released for Android 2.1, does anyone know what,
Does anyone know if there's a windows Python executable creator program available now that
Now I know that bigint is 2^64; that is, more atoms than there are
Language Integrated Query. Now I know that the acronyms are. I have seen C#

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.