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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:03:34+00:00 2026-06-04T14:03:34+00:00

I currently have a base class Expr and some derivatives like AddExpr , MultExpr

  • 0

I currently have a base class Expr and some derivatives like AddExpr, MultExpr and NumExpr. I am trying to overload a general print() function that recursively calls the print() function of its members which are pointers to other Expr objects.

//Addition Expression
AddExpr::AddExpr( Expr* l, Expr* r ) {
    left = l;
    right = r;
}
string AddExpr::print() {
    string s;
    stringstream out;
    out << "(+" << left->print() << right->print() << ")";
    s = out.str();
}

NumExpr::NumExpr( string n ) {
    number = atoi( n.c_str() );
}
string NumExpr::print() {
    string s;
    stringstream out;
    out << number;
    s = out.str();
    return s;
}

So ultimately I want the add expression to print out a ( number + number ) if the left and right pointers are numbers, ( expression + number ), ( expression + expression ), etc. by recursively calling the print() function.

However I don’t think I’m approaching this correctly as I am getting jumbled output 🙁
I am not too familiar with pass by pointer and pass by reference, however every post that I have gone through with similar question are not quite relevant to my situation.

Here’s a debug sample for calling the functions:

NumExpr* left = new NumExpr("1");
cout << left->print() << endl;
NumExpr* right = new NumExpr("2");
cout << right->print() << endl;
AddExpr* temp = new AddExpr( left, right );
cout << temp->print() << endl;
Expr* temp2 = temp;
cout << temp2->print() << endl; 

This will print out 1 and 2 but has the problem on the AddExpr.

Any help is appreciated!

EDIT:
the header for my expression classes:

class Expr {
    public:
        virtual string name();
        virtual string print();
};

class AddExpr : public Expr {
    public:
        AddExpr(Expr* l, Expr* r);
        string print();
    private:
        Expr* left;
        Expr* right;
};

class NumExpr : public Expr {
    public:
        NumExpr( string v );
        string print();
    private:
        int number;
};
  • 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-04T14:03:35+00:00Added an answer on June 4, 2026 at 2:03 pm

    AddExpr::print() does not return any value.

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

Sidebar

Related Questions

I have a base class which adds some functionality to a number of derived
I have a base class for some plugin-style stuff, and there are some methods
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I currently have the following: class Question < ActiveRecord::Base belongs_to :product belongs_to :user belongs_to
Currently I'm using a base class for some Customer model classes. As defined as
a question about class design. Currently I have the following structure: abstract Base Repository
I currently have a Order and image model which look like this: class Order
I currently have 2 models setup: class Topic < ActiveRecord::Base belongs_to :category end class
I currently have the gcc4.7 and the gcc4.7-base, etc., packages installed but GCC seems
Currently I have solution A that contains a domain layer base and solution B

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.