-2

The problem was we are given three classes: We can make the variables public . It was not mentioned in problem, we were give code block which we need to complete and we were allowed to choose the acess specifiers of class.By default it was private . We can make one class inherit the other or any type of changes but can't change the no and type of variables .

class A{
   int a,b;
};

class B{
  int c,d,e;
};

class C{
   int f,g;
};

We need to overload (>>) extraction and (<<) insertion operator , such that they will be valid for all the three classes.

We need to create 1 overloading function for (>>) extraction and 1 overloading function for (<<) insertion which will work for all the three classes.

I know how to overload operators for one class, but for more than one class I have no idea.

  • 1
    You need to provide 1 function (for each operation) that can work with all three types? – NathanOliver Mar 22 '23 at 12:19
  • Just three different overloaded functions? One for each class? – Some programmer dude Mar 22 '23 at 12:19
  • We need to create 1 oveloading function for (<<) insertion and 1 for (>>) extraction which will work for extracting input and printing values for all the three classes. – Pawan Giri Mar 22 '23 at 12:21
  • 1
    Looking for resources is off-topic here, grab [a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Quimby Mar 22 '23 at 12:22
  • @close votes, the resource request part of the Q has been edited out. – NathanOliver Mar 22 '23 at 12:24
  • Unclear question. – kiner_shah Mar 22 '23 at 12:25
  • 2
    ***Why*** do you need a *single* operator that handles all three different classes? What is the *original*, *real* and *underlying* problem you need to solve? Right now this question is an [XY problem](https://en.wikipedia.org/wiki/XY_problem). Please ask about the actual, real and underlying problem directly instead. – Some programmer dude Mar 22 '23 at 12:26
  • 1
    create a base class holding the operator overload (prototype at least to have a common interface for all three classes), that the 3 classes derive from. In case you also manage to pull some of the members to be the same, you could also implement the operators in that base class. In case that's not possible, you ofcause have to implement three different implementations for the operators. – Synopsis Mar 22 '23 at 12:26
  • 1
    If I were to write a `void foo()` function that was overloaded for three different parameters, I'd be writing 3 `void foo()` functions. They are **overloaded** because their parameters differ. How is this situation different? – Eljay Mar 22 '23 at 12:28
  • @Someprogrammerdude that was the task , there is no underlying problem . I have stated each and every bit of the problem . In more simple words "We have three different classes , 1 class has 2 variable , 2nd class has 3 variables and 3rd class has 2 variables . We need to create 1 function each for overloading (<<) and ( >>) operators which will work for all the three. – Pawan Giri Mar 22 '23 at 12:32
  • Given that `A`, `B` and `C` are classes and not structs and that their members are `private` that looks a bit impossible. – Werner Henze Mar 22 '23 at 12:43
  • That's impossible. You must be missing some crucial bit of information, or whoever gave you the problem failed at explaining it. – molbdnilo Mar 22 '23 at 12:45
  • 1
    @PawanGiri Unless this is a common pattern in C++ to do this, if this task was given to you by your teacher, IMO, this is a waste of your time and other student's time. There would be much more relevant C++ topics that could have been covered. "C++ tricks" should be shown up-front and explained, not be given to students to waste hours, days, and maybe weeks on trying to solve. – PaulMcKenzie Mar 22 '23 at 12:46
  • @WernerHenze we can make them public , My whole problem was edited once published .We can make the variables public . We just need to implement the operator overloading function once which will work for all the three classes. – Pawan Giri Mar 22 '23 at 12:51
  • If this is a school assignment or exercise, then first please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) as well as [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) Then edit your question to include the *full* and *complete* assignment or exercise text, especially all requirements and limitations. – Some programmer dude Mar 22 '23 at 12:58
  • Also please ask your teacher about the purpose of this assignment or exercise. What are you supposed to learn from it? Why does it have the requirements and limitations it have? It just makes no sense to have a single function that can handle tree different, separate and distinct classes that have no real relationship. Good code just isn't written that way. I just can't think of any major (or minor) "OOP" principle for this use-case. – Some programmer dude Mar 22 '23 at 13:00
  • And I'm sorry for nagging, it's not your fault for having a bad assignment. – Some programmer dude Mar 22 '23 at 13:02
  • @Someprogrammerdude it is not a school assignment or excercise problem . I have edited the best possible way it can be and provided all the details . Although I am extremely sorry if it felt like a school assignment or excercise or incomplete problem. Pardon me. It was a interview problem for C++ developer role .They gave the code and I need to complete it . We can make the variables public, but the main problem was to implement two operator overloading function 1 each for (>>) and (<<) which will work for 3 classes with different no of variables. – Pawan Giri Mar 22 '23 at 13:03
  • Okay, it still seems like a poorly though out exercise. Or there's something in it that we just don't know about. Please [edit] your question to include the full and complete exercise text, copy-pasted as text. If it's from a book and not from online, then include a photo of it, *plus* write it all down (will make it easier to search and copy-paste if needed, also possible to be handled by screen-readers). Most importantly, we need the complete list of requirements and limitations. – Some programmer dude Mar 22 '23 at 13:07
  • 1
    If it's an interview problem, the expected answer *could* be "that's impossible, because (something)". – molbdnilo Mar 22 '23 at 13:26
  • @molbdnilo it is not impossible as one of methods is show by nathan ,using templates . – Pawan Giri Mar 22 '23 at 13:40
  • A function template is not one function. It's not even a function. If I gave you this problem, I would expect "you can't, but here's how you can make it work", not a clever trick that nobody would let into their codebase. – molbdnilo Mar 22 '23 at 13:42

1 Answers1

2

One way to handle this is to use a template and then constexpr if to handle the specific code for the different types. That could look like

// SFINAE to only enable is ABC is an A, B or C
template <typename ABC, std::enable_if_t<std::is_same_v<ABC, A> ||
                                         std::is_same_v<ABC, B> || 
                                         std::is_same_v<ABC, C>, bool> = true>
std:ostream operator <<(std::ostream& os, const ABC& abc)
{
    if constexpr(std::is_same_v<ABC, A>)
        return os << abc.a << " " << abc.b;
    if constexpr(std::is_same_v<ABC, B>)
        return os << abc.c << " " << abc.d << " " << abc.e;
    if constexpr(std::is_same_v<ABC, C>)
        return os << abc.f << " " << abc.g;
}

This is a bit of cheat. It's only one overload, but up to 3 distinct functions will get instantiated from it.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • `A`, `B` and `C` are classes, the members are `private`. That must be changed first so that your code compiles. – Werner Henze Mar 22 '23 at 12:44
  • We can make them **public** .We can edit the codeblock . the main problem was to implement two operator overloading function 1 each for (>>) and (<<) which will work for 3 classes with different no of variables . We just can't change no and type of variables of all the classes . We can also make **B** derive from **A** etc etc. – Pawan Giri Mar 22 '23 at 13:06
  • 1
    @PawanGiri So there actually *was* more information than you've given so far. If you can add inheritance, it's simple - you only need to implement the operators for the base class and dispatch to a virtual function. – molbdnilo Mar 22 '23 at 13:47
  • @molbdnilo sir I have given more than information given to me , I was just given the codeblock which I have shared as it is , I was asked to make two operator overloading functions one for (>>) extraction and one for (<<)insertion which will work for 3 classes . First class has 2 variables , second class has 3 variables and last class has 2 variables . We can make changes in codeblock , just can't change the number and type of arguments , we can change the variables to public and make one class inherit the other ( we can make changes in code ) . – Pawan Giri Mar 22 '23 at 14:17