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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:59:44+00:00 2026-05-22T14:59:44+00:00

I’m writing a program with three levels of inheritance. It is required that I

  • 0

I’m writing a program with three levels of inheritance. It is required that I overload the operator== in the base class, and then override that function in the derived classes (I am not allowed to change this design).

The function is a bool which does a memberwise comparison of two objects. How do I process supers return value? In Java getting this value is simple: I just make the first line of the overriden function a call to super, for example:

retValue = super.myFunction();

I need to achieve this same result, but in C++ and with awful operator overloading. Some pseudo code for the overrloaded function would be much appreciated.

I also have another question relating to this; What will happen if the operator is used on instances from different subclasses? For example sub1 and sub2 inherit from base, which version of the function will be executed for the following line of code in Main:

if (sub1 == sub2)

Will it generate a compiler error?

Edit: Firstly, I am a student so I cannot write the code to accomplish this task in the way I would like to. The program requirements state;

“Vehicle operator overload: the == operator

The Vehicle class needs to override the == operator. Two objects are equal if all of their data fields are equal and if their passenger lists are identical. This needs to be defined in the base class and then overridden in the derived classes.”

I don’t really have any code to post because in reading Billy’s response I realized that I had actually written the operator== function free of the class (which won’t allow me to override it, and won’t meet the program requirements).

The example;

if (sub1 == sub2)

was entirely hypothetical. I will actually have an array of Vehicle objects, I have a Car and Airplane class which inherit from Vehicle, and then a MagicSchoolBus which inherits from Car and an Xwing which inherits from Airplane. In the Vehicle class I have an int ‘vType_’ which identifies the specific subclass each Vehicle instance belongs to. To prevent a runtime crash I will simply check to make sure the objects have the same vType_ prior to using the == operator on them.

As much as I would to prefer to write a compare function, I am not allowed to. I have to find a way to use the return value from the operator== function in the base class because it is comparing several data members, and I will of course be graded down if I rewrite this code in each of the derived classes.

If I can’t use that return value I either have to rewrite code, or I will get the wrong result when an object has different values in members of the base class but the same values in the derived class.

I’m going to have to rewrite this function as a member of the Vehicle class but I will post it anyways in hope that it may be useful.
Each of the derived classes have a few more members specific to those types of vehicles, and an array of Passenger objects which will have be compared.

// equality operator overloaded to do a memberwise comparison

bool operator== (Vehicle& obj1, Vehicle& obj2)
{
bool retValue = false;

if (strcmp(obj1.getName(), obj2.getName()) == 0)
{
    if (obj1.getType() == obj2.getType() && obj1.getFuel() == obj2.getFuel())
    {
        if (obj1.getRate() == obj2.getRate() && obj1.getStatus() == obj2.getStatus())
        {
            retValue = true;
        }
    }
}

return retValue;

}

  • 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-22T14:59:45+00:00Added an answer on May 22, 2026 at 2:59 pm

    If you inherit from a class, then you have to use an explicit qualifier. There’s no keyword.

    class A {
    public:
        virtual bool operator==(const A&);
    };
    class B : public A {
    public:
        virtual bool operator==(const B& other) {
            bool result = A::operator==(other);
            //.. do other stuff
        }
    };
    

    If you don’t define a new one, then the base one is called.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests

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.