0

I've been told everything in C++ (even if it's not being instantiated, but is used in more than one place) must be put in a class. And although I know I don't have to, I don't know which is worse: making a bunch of classes that aren't going to be instantiated, or having a bunch of functions in the main.cpp.

Here's some sample code if I follow what I have been told:

#include <iostream>
using namespace std;

int main(){
// line of code to call class that acts as a calculator
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Neel Kamath
  • 1,188
  • 16
  • 34
  • There is really nothing wrong with free-standing functions and you don't need to put them in your the same file as your `main` function if you don't want to. I would say use a *namespace* though for your functions (and your classes). – Galik Jul 30 '16 at 09:27
  • 1
    No, you don't need to. Actually, you should take care not to: http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197 – Zdeslav Vojkovic Jul 30 '16 at 09:35

4 Answers4

5

No, you should not put everything into classes. Whoever told you that was wrong.

C++ is not (just) an OO language, it is a multi-paradigm language. Additionally, putting everything into classes does not mean that code is object oriented (especially since for static methods you don't need actual objects, just types).

If you check the C++ standard library, you will see that it contains many functions which are not part of any class (check contents of <algorithm> or <numeric> libraries for examples).

In many ways, non-member functions can improve your code

If you need to logically separate functionality to avoid having everything in global scope you can and should use namespaces. E.g. algorithms mentioned before are all put in std namespace.

Community
  • 1
  • 1
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • what exactly is a namespace, is it a different file? I always thought that it just differentiated between variable names depending on whether the variable is in the outer or inner function – Neel Kamath Jul 30 '16 at 09:41
  • 1
    please google `c++ namespace`. you will find much more info than it is possible to answer here. E.g. http://stackoverflow.com/a/41598/1663919 – Zdeslav Vojkovic Jul 30 '16 at 09:46
2

In any private effort there is no 'requirement', thus there is no 'need'. You need no one else's permission to write many functions and few methods.

In school assignments, particularly when you are studying C++ and software engineering, there is likely to be a requirement from the instructor to use classes (often provided to simplify his TA's work load). So yes, in that case, there is a 'need'.

Your employment in software development might include conformance to coding standards, working with peers, and following your team lead (even when you disagree with or have a distaste for where he is leading). This too implies requirements for using classes (sometimes with goals identified), and thus, you should consider that there is a 'need' to use classes.

In any case, as a beginner, I think you should not be seeking our permission to avoid C++ classes, instead, be courageous and use them, practice as much as you can, and then write to this forum when you fail to grok what or why your coding attempts fail to do.


Also, in general, there are software engineering goals for which all programmers should be able to defend their choices to colleagues or peers.

One example that I value is from "Software Engineering Concepts" by Richard Fairley

"A fundamental goal of software design is to structure the software product so that the number and complexity of interconnections between modules is minimized. An appealing set of heuristics for achieving this goal involves the concepts of coupling and cohesion."

In other words, he is describing two ideas that, when combined, might result in your code being easier to read, simpler to debug, and more acceptable to your teachers, class mates, and work colleagues, and SO peers. (Both terms are in wikipedia.)

Fairley listed

  • 7 coupling levels, from strongest coupling (least desirable) to weakest coupling (most desirable),

and

  • 8 cohesion levels, from weakest (least desirable) to strongest (most desirable).

If you accept (as I do) that Fairley's "fundamental goals" are important to you, then perhaps they have some bearing on your "every thing in classes" decisions.


I find C++ classes are a superior approach to achieving the 'most desirable" goals of Fairley lists. I recommend you embrace these ideas, along with encapsulation, data hiding, and even polymorphism. And practice these issues with C++ classes.

Also, I strongly prefer multiple small-ish classes to fewer large classes. And no more than 3 inheritance levels (7 was very tiresome).

And maybe I need to start my list, and search for other's lists of 'simplying rules I follow when I write C++ classes'.


Code example - most of my experiments look very much like the following:

int main(int argc, char* argv[] )
{
   T_t  myClass;   // instantiate a class T_t
   int retVal = myClass.exec(argc, argv);  // go
   return(retVal);
}
2785528
  • 5,438
  • 2
  • 18
  • 20
  • the only thing is that the reason i prefer not to use classes if im not instantiating those function(s) is because it takes more code and loses its modularity a bit (i prefer Python's approach to usage of classes but I am flexible and want to follow the preferred standard for each language) – Neel Kamath Jul 30 '16 at 15:21
0

there is no need to put everything in a class. you can write code as you write in c i.e. without class structure. it will run fine in c++ also

Ganesh Karewad
  • 1,158
  • 12
  • 21
0

C++ code can be written with an eye towards being object oriented. However, not everything needs to go into a class. In fact, main is not in a class (as it is in Java). You may write functions that are not attached to classes as well. Generally, your code can become more modular as you write more classes and functions, but depending on your needs, this can make your code more complicated than you require as well. There is a delicate balance there. Perhaps with more information we could give more advice.

When you say, which is worse, what do you mean? Computational efficiency, Code maintenance or something else? You could always put your function prototypes in .h and implementation in .cpp even if they're not attached to a class to help with your driver .cpp becoming overcrowded.

Shaun Ramsey
  • 562
  • 4
  • 14
  • Well, it really does depend on what you mean by object oriented. Some would say that since the main (and other functions) aren't required to be part of classes that it is not object oriented. It isn't a necessary condition of C++. I'll edit my response slightly. – Shaun Ramsey Jul 30 '16 at 09:34
  • By worse, I mean it takes longer to code, wastes more lines of code and hence adds to reading and indirectly maintaining time. Not to mention constantly having to declare variables etc. in both the header as well as the source file and calling entire classes in main when they just consist of one function. – Neel Kamath Jul 30 '16 at 09:40
  • 1
    There is definitely some maintenance in creating classes. If the code isn't going to be reused, the function is going to be called only once and the object is not needed, then I'd simply write a stand alone function. However, you'd be surprised how many times projects grow and you wish you had begun programming with an eye towards modularity. Since C++ is multi-paradigm (function/procedural and object oriented), it can be a challenge to figure out where to fit in. In this scenario, I'd say no classes. But, agile programming says to think to the future and look out for new features. – Shaun Ramsey Jul 30 '16 at 09:43