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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:25:46+00:00 2026-06-09T02:25:46+00:00

If I create a pointer out of my Shape base class, how would I

  • 0

If I create a pointer out of my Shape base class, how would I got about making it behave like the circle (derived) class? Here are my class definitions:

    //CShape.h class definition
    //Shape class definition
    #ifndef CSHAPE_H
    #define CSHAPE_H

    class CShape
    {
    protected:
        float area;
        virtual void calcArea();
    public:
        float getArea()
        {
            return area;
        }
    };


    class CCircle : public CShape
    {
    protected:
        int centerX;
        int centerY;
        float radius;
        void calcArea()
        {
            area = float(M_PI * (radius * radius));
        }
    public:
        CCircle(int pCenterX, int pCenterY, float pRadius)
        {
            centerX = pCenterX;
            centerY = pCenterY;
            radius = pRadius;
        }
        float getRadius()
        {
            return radius;
        }
    };

In my project file where I’m calling these objects, I have the following code:

            CShape *basePtr = new CCircle(1, 2, 3.3);
            basePtr->getRadius();

It seems to me like this should work, however I’m told CShape has no member “getRadius().”

EDIT
Based on the response below I tried to dynamic_cast the basePtr object to CCircle like so:

CCircle *circle = new CCircle(1, 2, 3.3);
basePtr = dynamic_cast<CCircle *>(circle);

This failed too, however. I’ve never done a dynamic_cast and am unfamiliar with most of the syntax in C++, so any help is greatly appreciated.

  • 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-09T02:25:49+00:00Added an answer on June 9, 2026 at 2:25 am

    To get to getRadius function you must declare it of type CCircle:

            CCircle *basePtr = new CCircle(1, 2, 3.3);
            basePtr->getRadius();
    

    Otherwise, if you want to declare basePtr as CShape, use dynamic binding with dynamic_cast or use c-style cast directly, as @Janisz pointed out.

           CShape *basePtr = new CCircle(1, 2, 3.3);
           CCircle *child
           // choose one of the two ways, either this:
           child = (CCircle*)basePtr; // C-style cast
           // or this:
           child = dynamic_cast<CCircle*>(basePtr); // C++ version
           child->getRadius();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create something like a pointer to a 2 dimensional array
I got segfault when trying to call new to create a pointer and push
Possible Duplicate: What data-structure should I use to create my own “BigInteger” class? Out
I have a 3x3 array that I'm trying to create a pointer to and
in C++, I can easily create a function pointer by taking the address of
I'm attempting to create a CList with a COM smart pointer (one of the
I'm trying to create an array of structs and also a pointer to that
Is it possible to create a function which takes a pointer to another function?
I'm creating a pointer to a pointer to a structure to create a dynamic
I can create function pointers in Fortran 90, with code like real, external ::

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.