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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:26:55+00:00 2026-05-13T20:26:55+00:00

If I have a class called Animal, and a derived class Bird : public

  • 0

If I have a class called “Animal”, and a derived class “Bird : public Animal”, I can create a Bird these two ways:

Animal *sparrow = new Bird;
Bird *sparrow = new Bird;

Both compile fine and work as expected. Are they equivalent? Should I prefer one over the other?

  • 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-13T20:26:55+00:00Added an answer on May 13, 2026 at 8:26 pm

    The line, in itself, doesn’t demonstrate the difference. However, assume Bird declares a method Fly that doesn’t exist on Animal. You wouldn’t be able to do:

    Animal* a = new Bird;
    a->Fly();
    

    on the other hand, this is legal:

    Bird* b = new Bird;
    b->Fly();
    

    The distinction here is a result of the fact that C++ is a statically typed language. The static type of the variable is what the compiler cares about when it’s verifying things like method calls. Since the static type of the variable a is Animal which doesn’t have a Fly method, the compiler will not allow you to call Fly on it (not all animals are able to fly, so you’ll have to explicitly cast to Bird: dynamic_cast<Bird*>(a)->Fly() is legal).

    The expression new Bird will have the type Bird*. If you assign a value of a derived type to a variable of a based type, the compiler will not complain (all Birds are Animals, so it should always work). Basically, the compiler upcasts Bird* to Animal*. The reverse is not true. Not all Animals are Birds, so you’ll have to take the responsibility and do the cast explicitly and tell the compiler that I know that object is really a Bird*. Only in that case the compiler will let you to use Bird-specific features. So, in general, if you need to use a Bird-specific member, you’d better use Bird* b = new Bird;.

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

Sidebar

Related Questions

I have an Animal class and an extension of Animal called AnimalExtension. public class
I have this class called Table: class Table { public string Name { get
In a C++ physics simulation, I have a class called Circle, and Square. These
If I have a class called animal, dog and fish is the subclass. The
Let's say I have 2 instance of a class called 'Animal'. Animal has 3
Lets say I have a simple Enum called Animal defined as: public enum Animal
I have Class CustomDate and that is referred in other class called Test. public
I have a class called Worker public class Worker : BaseEntity { public virtual
I have a class called Object : class Object { public: Vector pos; float
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.

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.