Neel Kamath Neel Kamath - 1 year ago 56
C++ Question

Do I need to put everything in a class in C++?

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

Answer Source

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),


  • 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download