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

  • Home
  • SEARCH
  • 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 8760085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:57:31+00:00 2026-06-13T14:57:31+00:00

After a long time of C-style procedural coding, I am just beginning to ‘get’

  • 0

After a long time of C-style procedural coding, I am just beginning to ‘get’ OOP. So I suspect there may be standard way of dealing with the situation I am facing. I have an application with the class hierarchy shown below:

#include <iostream>
using namespace std;

class A {
public:
  virtual int intf() { return 0;} // Only needed by B
  virtual double df() {return 0.0;} // Only needed by C
};    
class B : public A {
  int intf() {return 2;}
  // B objects have no use for df()
};    
class C : public B {
  double df() {return 3.14;}
  // C objects have no use for intf()
};    
int main(){
  // Main needs to instantiate both B and C.
  B b;
  C c;
  A* pa2b = &b;
  A* pa2c = &c;

  cout << pa2b->intf() << endl;
  cout << pa2b->df() << endl;
  cout << pa2c->intf() << endl;
  cout << pa2c->df() << endl;

  return 0;
}

Now this program compiles and runs fine. However, I have question about its design. Class A is the common interface and does not need to be instantiated. Class B and C need to be. Regarding the functions: intf() is needed by B but not C, and df() is needed by C but not B. If I make intf() {df()} pure virtual in A, then there is no reasonable definition of df() {intf()} for B {C}.

Edit: B and C share some data members and also some member functions other than f(). I have not shown it my stripped down code.

Finally, as is standard, my application needs to access both B and C through a pointer to A. So my question is: Is there a way to ‘clean up’ this design so that unrequired/empty member function definitions (such as I have done in declaration/definition of A) can be eliminated? There is a clear “IS-A” relationship between the classes. So even though I share every newbie’s thrill about inheritance, I dont feel I have stretched my design just so I could use inheritance.

Background in case it helps: I am implementing a regression suite. Class A implements functions and matrices common to every regression (such as dependent and independent variables). Class B is logistic regression with two classes (‘0’ and ‘1’) and defines cost functions, and training algorithm for two-class logistic regression. Class C is multi-class logistic regression. It extends class B by training for multiple classes using the “one-vs-all” algorithm. So in a sense C is a binary logistic regression if you think of your class of interest as positive examples and all others as negative examples. Then you do it for every class to implement multi-class regression. The functions (intf and df) in question return the output. In case of logistic regression, the return value is a vector, while for multiclass regression, it is a matrix. And, as stated above, B and C dont have any use for each others’ return functions. Except that I cant seem to be able to eliminate redundant definitions in A (the regression class).

Thanks for your help.

  • 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-13T14:57:32+00:00Added an answer on June 13, 2026 at 2:57 pm

    You touched one of the most controversial point of OOP: the is-a == derivation pattern, resulting int the “god object” anti-pattern, since everything is ultimately a child-of-god, with god knowing every method of everyone and having an “answer” (read “default implementation”) for everything.

    “Is-a” is not enough to justify inheritance, where no replace-ability exist, but in real world no object is really fully replaceable with another, otherwise it will not be different.

    You are in the “land of nowhere” where the substitution principle doesn’t work well, but -at the same time- virtual functions look the best tool to implement dynamic dispatch.

    The only thing you can do come to a compromise, and sacrifice one of the two.

    As far the situation looks like, since B and C have nothing in common (no shared useful methods), simply don’t let those method to originate from A.
    If you have something to “share” is probably a runtime mechanism to discover the type of B or C before entering B related specific code or C related specific code.

    This is typically done with a common base having a runtime-type indicator to switch upon, or just a virtual function (typically the destructor) to let dynamic_cast to be able to work.

    class A
    {
    public:
        virtual ~A() {}
    
        template<class T>
        T* is() { return dynamic_cast<T*>(this); }
    };
    
    class B: public A
    {
    public:
        int intf() { return 2; }
    };
    
    class C: public A
    {
    public:
        double df() { return 3.14; }
    };
    
    int main()
    {
        using namespace std;
    
        B b;
        C c;
    
        A* ba = &b;
        A* ca = &c;
    
        B* pb = ba->is<B>();
        if(pb) cout << pb->intf() << endl;
    
        C* pc = ca->is<C>();
        if(pc) cout << pc->df() << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After a long time searching and trying I'm asking now for help: My situation:
After searching a long time for a performance bug, I read about denormal floating
I've been using Symfony for a long time but I'm new to Symfony2. After
I heard the word buffer after a long time today and wondering if somebody
After you've been programming for a long time with a language, you pick up
Long time reader, first time poster asks: After many weeks of google and forum
After long time of searching the best upload technique I've decided to go with
I am starting to learn Java a little after long time. And learning Netbeans
this is my first question after long time checking on this marvelous webpage. Probably
So after a long time of debugging here is whats happening. (Using Facebooks Graph

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.