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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:13:27+00:00 2026-06-05T13:13:27+00:00

I have a couple of classes that share a common base class, with the

  • 0

I have a couple of classes that share a common base class, with the exception that they differ in the way their methods work. So in the example below, Adder and Multiplier are the same except for the way in which their calculation is performed.

Is there a way to change “a” to a Multiplier on the fly? Do I need to implement methods which convert derived classes to each other? e.g. something like

a = a.asMultiplier()?

As you can see in the code below I tried reinterpret_cast to a Multiplier, with no luck, it still acts like an Adder. (gcc OS X v4.2.1)

#include <iostream>


class Base {
protected:
    int a,b;    
public:
    Base(int a, int b) {
        this->a = a;
        this->b = b;
    }  
    virtual ~Base() { }    
    virtual int calculate() = 0;
};


class Adder : public Base {    
public:
    Adder(int a, int b) : Base(a, b) {

    }      
    int calculate() {
        return this->a + this->b;
    }
};


class Multiplier : public Base {    
public:
    Multiplier(int a, int b) : Base(a, b) {    
    }  

    int calculate() {
        return this->a * this->b;
    }
};



int main() {

    Base* a = new Adder(3,4);
    Base* m = new Multiplier(3,4);

    std::cout << "Adder gives " << a->calculate() << std::endl;
    std::cout << "Multiplier gives " << m->calculate() << std::endl;

    a = reinterpret_cast<Multiplier*>(a);

    std::cout << "Now adder gives " << a->calculate() << std::endl;

    delete a;
    delete m;
    return 0;
}
  • 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-05T13:13:28+00:00Added an answer on June 5, 2026 at 1:13 pm

    The best thing that comes up to me to solve this, is implementing a copy constructor, taking the base class:

    class Multiplier : public Base {    
    public:
        Multiplier(int a, int b) : Base(a, b) {    
        }  
    
        explicit Multiplier(const Base& iBase) : Base(iBase.a, iBase.b) {    
        }  
    
        int calculate() {
            return this->a * this->b;
        }
    };
    

    But since I am not the most advanced c++ developer here, it might not be correct or other people might have a better idea, just trying 🙂

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

Sidebar

Related Questions

I have a couple classes that can each have comments: class Movie < ActiveRecord::Base
In C#, I have a class hierarchy with a couple of abstract base classes
I have a number of data classes, which share an abstract base class so
I have a couple classes that look like this public class ParentClass { public
I have a couple of classes, all derived from the same base type. class
I have a couple classes that have nearly identical code. Only a string or
I have a couple of classes that are singletons so I tried to create
If I have a couple of classes like this ; public class Employee {
I have a couple of classes that implement an interface, IFoo. I need to
I have a couple of classes BitMask & BitMaskLarge that both implement an interface

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.