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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:14:12+00:00 2026-06-13T02:14:12+00:00

so I have a Top class, let say: //Top.h #pragma once #include <string> using

  • 0

so I have a Top class, let say:

//Top.h
#pragma once
#include <string>
using std::string;

class Top
{
    protected:
        string name;
    public:
        virtual string GetName() = 0;
}

This class won’t have any object Top instantiate, that’s why it’s an abstract class. I also have two Middle class, let say:

//MiddleA.h
#pragma once
#include "Top.h"

class MiddleA : public Top
{
    protected:
        int value;
    public:
        MiddleA(string, int);
        string GetName();
        int GetValue();
}

//MiddleB.h
class MiddleB : public Top
{
    protected:
        double factorial;
    public:
        MiddleB(string, double);
        string GetName();
        double GetFactorial();
        double Calculate(int);
}

Now, what I need is an array, or anything, that can contains multiple objects of type MiddleA, MiddleB or any classes that inherit from those two classes. Is there a way to do this in C++?

EDIT : Would it be “acceptable” to add a default constructor in the protected section and use a vector or array of Top?

  • 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-13T02:14:13+00:00Added an answer on June 13, 2026 at 2:14 am

    Use the pointers and arrays of C++:

    typedef std::array<std::unique_ptr<Top>,3> Tops;
    Tops tops =
    {{
        new MiddleA( "Kuku", 1337 ),
        new MiddleB( "Hoi", 42.0 ),
        new MiddleC()
    }};
    

    Only thing you have to have virtual destructor on your Top. Without virtual destructor you may only use shared_ptr, others will result with undefined behavior when objects are destroyed.

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

Sidebar

Related Questions

Let's say I have: public class Fruit { public static List<String> Suppliers { get;
What's the reason of making top-level class non-public in Java? Let's say we have
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Let's say I have this loop in my header.php: <div id=top-20> <h2 class=top-20-title>TOP 20
Let's say I have a GridViewEx class declared which extends GridView . And inside
Let's say I have the following class: class SQLMapper{ static find_user_by_id($id){ //sql logic here,
Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
Let's say I have a controller function with these lines: $this->load->view('stdHeader_view'); echo <div class='main'>;
Let’s say we have the following class Cell , which is composed of a
let's say i have this classes which are being mapped to SQLServer: class Statistic

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.