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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:22:30+00:00 2026-05-16T03:22:30+00:00

I have two classes which represent a special kind of numeric value. Let’s call

  • 0

I have two classes which represent a special kind of numeric value. Let’s call one “Value” and one “Fraction”; Fraction consists of a numerator and denominator of type Value. Because these are numeric values, operator overloading (+ – * /) makes a lot of sense. Given the nature of Value, division returns a Fraction. Operations can be performed with a mix of Values and Fractions.

Currently, I have just defined an implicit cast operator from Value to Fraction (and an explicit one from Fraction to Value, although I’m not using it), and am not using any sort of inheritance.

I am wondering if there is any “elegant” way to define both in terms of an abstract base class that defines the set of operations that are available, and calls the appropriate operation. Efficiency is a concern. I’d rather only use Fraction when necessary, and would rather not have to define more complex operations for both cases separately when they can be expressed using the same syntax.

One way I can see is doing something like the following for each operation, but I’m wondering if there are any better ways to do this?

    public static AbstractValue operator *(AbstractValue a, AbstractValue b)
    {
        Value valA = a as Value;
        Value valB = b as Value;
        if(valA == null)
        {
            Fraction fractA = a as Fraction;
            if(valB == null)
            {
                Fraction fractB = b as Fraction;
                return fractA * fractB;
            }
            return fractA * valB;   
        }
        if(valB == null)
        {
                Fraction fractB = b as Fraction;
                return valA * fractB;
        }
        return valA * valB;
    }
  • 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-16T03:22:31+00:00Added an answer on May 16, 2026 at 3:22 am

    If your base class has to do things like trying to cast to a sub type and checking if it worked, this is a sign that the base class should not being implementing this method. In theory, the base class should have no knowledge of any specific child classes.

    With your proposed implementation, if you want to add another child class, you will need to go and edit the base class. The whole point of inheritance is that you can add new types without having to edit the existing objects.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Let's say you have two classes, Person and Address. Person has a reference to
Say I have a unit test that wants to compare two complex for objects
I have several classes that need to load some properties files, and I was
I have the situation where i use GIS software which stores the information about
I'm writing a Java application that will instantiate objects of a class to represent
I have some software that is heavily dependent on MySQL, and is written in
I'm developing some algorithms in OCaml which need some parts to be pluggable so
I'm writing an algorithm in PHP to solve a given Sudoku puzzle. I've set
I'm stuck on a portion of the Java tutorial, specifically this exercise . The

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.