Please check this for me. :)

Started by
17 comments, last by Nathan2222_old 10 years, 3 months ago

He mean with c style exactly what he wrote:

First, you mention "C++." What you are programming in there is the beginner "hybrid mix." The way you are declaring functions is C-style syntax, but you are including C++ standard headers and using C++ data types (std::string). While not an issue, it is a pet peeve of mine

I know what he meant. What i'm asking is what part is c style since i don't know what c style code looks like.

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy

Advertisement

I'm curious about that too. How would those functions look if they weren't in 'c-style'?

C-style functions are stand alone functions, C++-style functons are wrapped up and stored in an object.

Header.h


//This is C-Style header
void MakeAnimalSound();

//This is C++ style header
class Animal {
public:
        void MakeSound();
}

Source.c


//This is a C-style implementation
void MakeAnimalSound() {
    //Do stuff here

}

Source.cpp


//This is a C++ style implementation
void Animal::MakeSound() {
    //Do stuff here
}

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

-- First, you mention "C++." What you are programming in there is the beginner "hybrid mix."

So, put everything in a class "just because?" I see people who liberally create c++11 inline templated headers as full fledged implementations. It's not how I program, and I don't judge them either. They certainly know what they're doing, but it's an uncomfortable read for me. I came from many years of programming C. If anything, it has taught me to be practical about my programming. I programmed faster when I didn't use classes, but I also produced more code. I soon after programmed mostly pure C++. Now, I just do whatever I need to do, simply out of acknowledging I should really only produce the code I absolutely need (right now). Whatever floats your boat, right?

I don't think the guy who posted the questions is currently learning classes, though.

It's easier to learn functions by seeing the simplest examples where they are useful, and many here have already provided some.

One basic rule of thumb: "prefer non-friend non-member functions". Meaning: if there isn't a good reason for a function to be part of a class (like needing to access private members), it has no business being a member-function.

Unless one wants to argue that most of the C++ standard library is bad code, it's not just perfectly fine but even recommended not to stuff everything into objects "just because". Namespaces on the other hand are almost always a good idea for projects that are either complex and/or use multiple libraries.

Here are two examples of "C style functions":

//Explicitly declaring "no parameters" (in C: empty list = any number of parameters)

int function(void) { return 42; }

//Skipping return and/or parameters type (C defaults to int, C++ won't compile)

function(x) { return 2*x; }

f@dzhttp://festini.device-zero.de
I haven't learned classes.
What's the difference between my code and pure c++ code? I thought mine was c++. I didn't copy/paste it from anywhere, i wrote it after reading about it in the jumping into c++ book.
Could you write that code using only c++ so that i know what you guys are refering to.

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy

Think of functions in a programming language same as you would in mathematics. That's where the name comes from, after all.

A parabola is defined algebraically as y=x*x. It could be defined as a function P:R->R (the function named 'P' that converts one Real number into another Real number) denoted P(x) = x*x.

Then anytime you need a parabola you don't have to type out the whole expression (though quite short in this case, it wouldn't be for something like an associated Legendre function) but instead you can just drop in P(whatever) and get the result. This is a form of parameterization. You want to encode some behavior that you can change with different inputs. You don't want to write code for a single specific parabola but rather for all parabolas (any X parameter).

In math, you usually write functions for complicated pieces of math because you don't want to repeat yourself while writing out a complex equation. This is similar for programs: you write functions where you have complicated pieces of logic that you don't want to cut-n-paste all over while writing your program.

In your example, std::cin>> and std::cout<< are actually function calls (using a funky form of operator overloading in C++, a more advanced topic). Writing something like std::cout<<"text" is basically equivalent to writing print("text"). That's a use of a function (which itself calls through many, many other functions). You're very thankful that's a function because you really, really don't want to have to write all the code get text on your screen.

As you explore C++, you may also come across "methods" (formally called "member functions" in C++) and operator loading (which are just a fancy syntax for functions that look like built-in operators like << or + or whatnot). Conceptually, they're all the exact same thing: they are differing syntaxes for convenience purposes.

Sean Middleditch – Game Systems Engineer – Join my team!

I haven't learned classes.
What's the difference between my code and pure c++ code? I thought mine was c++. I didn't copy/paste it from anywhere, i wrote it after reading about it in the jumping into c++ book.
Could you write that code using only c++ so that i know what you guys are refering to.

Yeah just ignore the c/c++ comparison stuff.

Ok. I'm going to write a switching menu program (i.e. switch from calculator to games and vice versa).
I think that would use functions better because i won't have to use if statements when the user inputs switch to game and the re-type the game code.

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy

This topic is closed to new replies.

Advertisement