Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does everything go inside a class? [closed]

Tags:

c++

oop

I have another rudimentary question. I somewhat recall hearing that everything in C++ goes inside a class. Then I hear that classes shouldn't be used where possible. So my question goes: When do you make classes and when do you not? (an example or two would be cool)

And a random side-question: When is it appropriate to put 2 classes in one header? Or does it matter?

like image 316
Christian Avatar asked Jun 04 '11 21:06

Christian


3 Answers

Does everything go inside a class?

No, most definitly not. Classes are part of Object Oriented Programming, but C++ isn't just that, in contrast to, say, Java or C#.

C++ is a multi-paradigm language, which means that it also allows other stuff. That's because the biggest drawback when using OOP is reusability of algorithms.
Sure, you can just write certain functions for every class out there, but how cool would it be to just write it once and be done with it forever? That's what STL is build on. Classes in the STL, like vector, only have the member functions they absolutly need for encapsulation. Most of them are unique anyways, like how you retrieve the first element from a vector is different from retrieving the first element of a list. All that is encapsulated and abstracted away by member functions like front and back (for direct access to the members), or begin and end (for iterator-access).

Now, all other algorithmic stuff is a free function as long as it works on more than one class and doesn't need direct access to the internals of that class. Take std::sort as an example. It works on any iterator-pair as long as they are random access iterators. In the STL, that would be vector and deque, with C++0x we get array, but outside of the STL, your classes too if they provide such iterators. Or even more prominent, C style arrays! Yes, you can use them for sort, very easily:

#include <algorithm>
#include <iostream>

int main(){
  int arr[5] = { 5, 2, 4, 1, 3 };
  std::sort(&arr[0], &arr[0] + 5);
  // arr == { 1, 2, 3, 4, 5 }
}

Write once, use everywhere.

As a last point, this article by Scott Meyers is a very very interesting read on class design and when to use free functions and when not.

like image 148
Xeo Avatar answered Nov 15 '22 05:11

Xeo


In C++ everything need not go inside a class(as opposed to Java where everything does go inside a class).

  1. You should make a class when you want to represent some real world entity for example a person, a customer, an user, animal, car etc. You require to store some data about the entity and have some functions related to the entity.

    For Example: Customer. You create a customer class. Customer has the following data to be stored. {name,age,address,phone}. You need some functions like addCustomer(), sendMessage() etc.

  2. Choice of where to use classes and where not to is a serious design issue. There is no general rule. Before you begin your application, you need to sit down with a paper and pencil and brainstorm the basic classes you will be requiring. You can always add and fine tune your design in the future. While designing your classes the most important thing to be kept in mind is code re-usability. Also try to keep your code as loosely coupled as possible.

  3. As a standard practice, you should have one class per header file.

like image 2
knurdy Avatar answered Nov 15 '22 05:11

knurdy


In C++ it's not necessary to encapsulate everything in a class. C++ is a multi-paradigm programming language, which means that you can either take an object-oriented approach, or procedural (no classes), or some mix of inbetween.

Generally though, encapsulation is a good thing and can improve code readability and maintainability.

like image 2
mpen Avatar answered Nov 15 '22 04:11

mpen