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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:25:07+00:00 2026-05-30T11:25:07+00:00

For example, in the main function, I want to get the user’s input. Depending

  • 0

For example, in the main function, I want to get the user’s input. Depending on the input, I will create either a Rectangle or a Circle, which are child classes of Object. If there’s no input (or unknown), then I will just create a generic object.

class Object
{ 
       public:
           Object();
           void Draw();
       private:
           ....  
};
class Rectangle:public Object
{ 
       public:
           Rectangle();
           .... //it might have some additional functions
       private:
           ....  
};

class Circle:public Object
{ 
       public:
           Circle();
           .... //it might have some additional functions
       private:
           ....  
};

main function:

string objType;
getline(cin, objType);

if (!objType.compare("Rectangle"))
     Rectangle obj;
else if (!objType.compare("Circle"))
     Circle obj;
else 
     Object obj;

obj.Draw();

Of course, the code above won’t work because I can’t instantiate an object inside an If statement. So i tried something like this.

Object obj;
if (!objType.compare("Rectangle"))
    obj = Rectangle();
else if (!objType.compare("Circle"))
    obj = Circle();


obj.Draw();

This code would compile, but it won’t do what I want. For some reason, the object was not initiated the way the child class should (for example, I set the some Object’s member variables, specifically, a vector, differently in the child classes). However, when I put a break point at the Child class constructor, it did run through there.

So how should I put instantiate Objects as its child classes in some if-statements??

  • 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-30T11:25:09+00:00Added an answer on May 30, 2026 at 11:25 am

    You can create automatic objects in if statements, but they will be destroyed at the end of the scope they are created in so they don’t work for this problem.

    The reason you can’t do the obj = Rectangle() one is because of slicing.

    You have to have a pointer to an Object. Pointers to base objects can also point to instances of child objects. Then you can dynamically create the object inside the if with new (objects created with new disregard scope and are only destroyed when you call delete on a pointer to them), then delete it when you’re done:

    Object* obj = NULL; // obj doesn't point to anything yet
    string objType;
    getline(cin, objType);
    
    if (objType == "Rectangle")
        obj = new Rectangle; // make obj point to a dynamic Rectangle
    else if (objType == "Circle")
        obj = new Circle; // make obj point to a dynamic Circle
    else
        obj = new Object;  // make obj point to a dynamic Object
    
    obj->Draw(); // draw whatever shape obj really points to
    
    delete obj; // deallocate dynamic object
    

    Alternatively, you can use smart pointers and then you don’t have to worry about manually deallocating the object:

    std::unique_ptr<Object> obj(NULL); // obj doesn't point to anything yet
    string objType;
    getline(cin, objType);
    
    if (objType == "Rectangle")
        obj.reset(new Rectangle); // make obj point to a dynamic Rectangle
    else if (objType == "Circle")
        obj.reset(new Circle); // make obj point to a dynamic Circle
    else
        obj.reset(new Object);  // make obj point to a dynamic Object
    
    obj->Draw(); // draw whatever shape obj really points to
    
    // the unique_ptr takes care of delete'ing the object for us
    // when it goes out of scope
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an absolute URI/URL, I want to get a URI/URL which doesn't contain the
So I want a C++ class to contain for example one public function: int
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
For example: This is main body of my content. I have a footnote link
example scenario is: from login screen - main screen - then when i clicked
Example: public class TestClass { public static void main(String[] args) { TestClass t =
Example website is http://www.wikihow.com/Main-Page , check the Recent Changes heading on the right side.
considering this example: public static void main(final String[] args) { final List<String> myList =
For example, static void Main() { var someVar = 3; Console.Write(GetVariableName(someVar)); } The output
In one of the example servers given at golang.org: package main import ( flag

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.