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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:37:26+00:00 2026-06-18T14:37:26+00:00

class A { protected: int a; public: A(); A(int); virtual void print()=0; virtual ~A();

  • 0
class A
{
protected:
    int a;
public:
    A();
    A(int);
    virtual void print()=0;
    virtual ~A();
};

class B: public A
{
    int b;
public:
    B();
    B(int,int); //initialize attributes a and b 
    void print(); //print a and b
};

class C: public A
{
    float c;
public:
    C();
    C(float,int); //initialize attributes a and c
    void print(); //print a and c
};

class D
{
    int size; //number of objects in v
    A **v; /* an array(a vector) of A pointers that allows me to have both B and C type objects */
public:
    D();
    D(int);
    D(D&);
    ~D();
    D operator=(const D&);
    void PrintAll();
};

All methods for D:

D::D()
{
    v=NULL;
}
D::D(int x)
{
    size=x;
    v=new A*[x];
    for(int i=0;i<x;i++)
    {
        if(i%2==0)
            v[i]=new B(4,7);
        else
            v[i]=new C(3,5);
    }
}

D::D(D& other)
{
    size=other.size;
    v=new A*[size];
    for(int i=0;i<size;i++)
    {
        if(i%2==0)
        {
            v[i]=new B();
            *v[i]=other.v[i][0];
        }
        else
        {
            v[i]=new C();
            *v[i]=other.v[i][0];
        }
    }
}

D::~D()
{
    if(v!=NULL)
    {
        for(int i=0;i<size;i++)
        {
            delete v[i];
        }
        delete[] v;
    }
}
D D::operator=(const D& other)
{
    if(v!=NULL)
    {
        for(int i=0;i<size;i++)
        {
            delete v[i];
        }
        delete[] v;
    }
    size=other.size;
    v=new A*[size];
    for(int i=0;i<size;i++)
    {
        if(i%2==0)
        {
            v[i]=new B();
            *v[i]=other.v[i][0];
        }
        else
        {
            v[i]=new C();
            *v[i]=other.v[i][0];
        }
    }
    return *this;
}
void D::PrintAll()
{
    cout<<"Printall():"<<endl;
    for(int i=0;i<size;i++)
        v[i]->print();
}

As you can see, the D class constructor makes the objects of type B or C as i is odd or even. If I know this, then I know how to write the operator= and the copy constructor for D. But If the class D constructor would make the objects of type B or C randomly, then how can I write the copy constructor (and operator=) for class D? My guess is I have to use typeid operator to solve this problem.

  • 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-18T14:37:28+00:00Added an answer on June 18, 2026 at 2:37 pm

    Define pure virtual method clone as part of your interface A definition – clone shall return a copy of the object.
    Override and implement it in each of your B and C classes.
    In D class copy constructor and assignment operator implementations use A‘s interface to create the required class instance instead of explicit call to new: v[i] = other.v[i]->clone();. No need for RTTI, normal polymorphism will do.

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

Sidebar

Related Questions

Possible Duplicate: Default constructor and virtual inheritance class Base { private: int number; protected:
class Base { protected: int data; public: virtual int getData() { return data; }
public class A { protected int x; public A(int x) { this.x = x;
I have the following class to test: public abstract class Challenge { protected int
I have the code: class Point3D{ protected: float x; float y; float z; public:
So i have a interface class class interfaceClass { public: virtual void func1( void
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
I'm porting some code to another structure: class EnvironObject { protected: vector<float> mX, mY,
class A // abstract class { protected: int m_iA; int m_iB; int m_iC; int
Lets assume we have the following code: abstract class Base1 { protected int num;

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.