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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:54:50+00:00 2026-06-14T10:54:50+00:00

Assume there are class A , and class B that inherits A. what is

  • 0

Assume there are class A , and class B that inherits A. what is the correct way to use B objects?
I’ve seen in “cplusplus.com” that they are “using” B objects this way:

B b;
A* a=&b;

What’s the difference between using a class “A” pointer and using b?

Thanks!

  • 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-14T10:54:51+00:00Added an answer on June 14, 2026 at 10:54 am

    The advantage of using pointers to base classes comes when you have virtual functions.
    Imagine the following

    class A {
        A(){}
        virtual ~A(){} // note virtual destructor
        virtual void func(){ cout << "A func" << endl; }
    };
    
    class B : public A {
        B(){}
        virtual ~B(){}
        virtual void func(){ cout << "B func" << endl; }
    };
    
    class C : public A {
        C(){}
        virtual ~C(){}
        virtual void func(){ cout << "C func" << endl; }
    }
    

    now if we have a pointer to base class we can call func() on it and the correct function is called depending on whether the pointer actually points at an A, a B or a C. Example:

    A* p1 = new A;
    A* p2 = new B;
    A* p3 = new C;
    p1->func();
    p2->func();
    p3->func();
    

    will output the following

    A func
    B func
    C func
    

    Now you may say why not use three different pointers to A, B and C types separately but what if you wanted an array of pointers? Then they would all need to be the same type, namely A*. This type of polymorphism is useful for grouping things which are similar in function or spirit but different in implementation.

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

Sidebar

Related Questions

We all know that most applications out there assume class names to follow the
Assume there is the BindingList<Person{Name, age}> class and a PersonManager class that hosts the
Assume that I have three class; UserAccount, UserGroup and Role. UserGroup and Role are
I think Im tying myself up in knots with this and I assume there's
Assume that there's no problem with my header file and some included library. I'm
There is a table alt text that uses CSS (by using inherit in CSS)
When speaking about casting here, I mean implicit cast. Assume class B inherits from
I have a class that inherits from a base class and implements the following...
On Rails 3.2.6, I have a class that inherits from ActiveRecord::Base: class Section <
Assume there is a class with the following interface: #import <Foundation/Foundation.h> @interface MyClass :

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.