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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:50:24+00:00 2026-05-20T10:50:24+00:00

I want to have a set of interchangeable classes to perform operations on a

  • 0

I want to have a set of interchangeable classes to perform operations on a dataset. I “think” what I’m trying to do is called Polymorphism but I’m unsure.

This is an example of what I’m trying to do. I want to create an object which contains the initial values, then initialize another class to use the data in the first class and perform a operation using exec, then repeat this with another recursive class.

I want to be able to change the order of operations, the idea is any class can call exec() which will always return unsigned long. init() might be different but is called during initialization and won’t be accessed within a recursive class.

Thanks,

class operationsObject {
public:
    virtual unsigned long exec (void) =0;
};

class addObject: public operationsObject {
private:
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b)
    {valueA = a; valueB = b;}
    unsigned long exec()
    {return valueA + valueB;}
};

class subtractObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA;
public:
    void init(unsigned long a, operationsObject *o)
    {valueA = a; obj = o;}
    unsigned long exec()
    {return obj->exec() - valueA;}
};

class multiplyObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b, operationsObject *o)
    {valueA = a; valueB = b; obj = o;}
    unsigned long exec()
    {return obj->exec() * valueA * valueB;}
};

int main(){
    operationsObject *op1 = new addObject;
    operationsObject *op2 = new subtractObject;
    operationsObject *op3 = new multiplyObject;
    op1->init(4,5);
    op2->init(4, op1);
    op3->init(1, 2, op2);
    unsigned retVal = op3->exec();
}
  • 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-20T10:50:25+00:00Added an answer on May 20, 2026 at 10:50 am

    Your description sounds like you might want to check the following design pattern: Chain of Responsibility.

    I’d probably come up with something like this:

    • command interface / abstract base class with
    • public method exec (non-virtual!) that calls a
    • protected abstract virtual method execLocal that implements the behavior you are looking for.

    Now, exec could be defined as:

    def exec(param):
        self.execLocal(param)
        if self.hasNext():
            self.next.exec(param)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a AutoCompleteTextView and I have set OnItemClick , but now I want
HI, I want to have set configuration settings for a unit test project that
I want to have a set of checkboxes that use AJAX to post to
I want to have a set of JRadioButtons and next to each one a
i want have an image and i want to set it as a background
I have set the two buttons in preference.xml file. I want to go to
I want to create a Binary file of my project. I have set my
I have a set of characters I want to remove from a string :
I have a set where i want to find items in it. Right now
I have a set of data and I want to find the biggest and

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.