C-like procedures in C ++?

Does the C++ programming style require a proper one , writing all your code with classes, or valid C procedures? If I passed code to someone, was it accepted as C++ just because it contained std::vector and std::string (instead of char * ), or should everything be a class?

eg:

 int number = 204; std::string result = my_procedure(number); OR MyClass machine; std::string result = machine.get(number); 

Are there cases where a programmer, or is he allowed to have C procedures in some of his source code? Have you ever had to do something like this?

In the context of this question, where is the boundary between C and C++ (if any)?

I hope my question is clear and consistent with the rules.

+8
c ++ c design oop
source share
6 answers

C ++ is a language with several paradigms, where OO, procedural, general / generative and - smaller (but increasing with C ++ 0x) extensional functionality - are among the paradigms. You should use what works best for the problem: you want the code to be easy to get and keep in order, and hard to fill.

The usefulness of classes lies in the packaging of data (state) along with related functions. If your wordify function wordify not need to maintain any state between calls, then there is no need to use a class / object. However, if you can predict that you will soon want to have a state, then it may be useful to start with a class so that the client code does not need to be changed.

For example, imagine adding a parameter to a function to indicate whether the output should be "first", "second", and not "one", "two." You want the behavior to be set once and be remembered, but in another place of the application, different code can also use functionality, but prefers a different setting. It is a good idea to use an object to store state and organize it so that every object of life and accessibility is aligned with the code that will use it.

EDIT:

In the context of this question, where is the border between C and C ++ (if any)?

C ++ just gives you a richer set of ways to solve your programming problems, each of which has its pros and cons. There are many times when the best way is still the same as it would be in C. It would be vicious for a C ++ programmer to choose the worst way, simply because it was only possible in C ++. Nevertheless, such a choice exists on many levels, therefore, as a rule, they say that the member function is not [class-], which takes the const std::string& parameter, combining the call to the procedural function with the object-oriented data that was generated using template : everything works well together.

+8
source share

Of course, you have free functions in your code - this is a matter of architecture, not "++ ness". For small programs, it doesn't even make sense to go all-in with classes, since OO is really a tool for managing complexity. If complexity does not exist, why bother?

Your second question, where is the line, does not have a short answer. The obvious thing is that the line is drawn in all places where the C standard differs from C ++. But if you are looking for a list of high-level language functions that C ++ has and C does not, here are some of them:

  • Class types and OO (of course)
  • STL
  • Overloading functions / operators
  • References
  • Patterns
  • new / delete for memory management
+10
source share

C ++ allows many programming styles, one of which is procedural code.

Which style to use depends on the problem you are trying to solve. The fields between C and C ++ are compiling your code using the C ++ compiler.

I sometimes use procedural functions in my code. Sometimes this solves the problem best.

+1
source share

C ++ code can still be valid C ++ code even without classes. Classes are more likely a function and are not required in every piece of code.

C ++ is basically C with a lot of functions, so there is no โ€œfieldโ€ between the two languages.

+1
source share

If you read Stroustrup Design and Evolution, you'll see that C ++ is designed to support several programming styles. Use what is most suitable for this problem (not the same as always, only what you know.)

In traditional real-world applications, there are often very few differences. Some C ++ code was originally C code and then recompiled. Slowly he migrates to take advantage of C ++ to improve its quality.

In short, yes, C ++ code can be procedural. But you will find that it is different from C code if you use C ++ features where necessary.

What is good practice, you need to consider things like encapsulation, testability and comprehensibility of the client API.

+1
source share
 #include <sstream> #include <string> #include <iostream> using namespace std; string wordify(int n) { stringstream ss; ss << n; // put the integer into the stream return ss.str(); // return the string } int main() { string s1 = wordify(42); string s2 = wordify(45678); string s3 = wordify(-99); cout << s1 << ' ' << s2 << ' ' << s3 << '\n'; } 
0
source share

All Articles