Does C have classes?

Ok, you can call me noob, but I'm confused.

My former classmate paid me to write a program in C. She gave me a task, and she said something like: “Blah blah blah, do at least two classes , write at least one constructor and rewrite at least one method, ” he speaks this word for word.

And then I told her: “This is C ++, not C,” she said, “but we are learning C”

I ignored it and wrote a C ++ program and sent it to her, as I thought she did not know what she was talking about. She said that “this does not work with code blocks, and wtf is cout <and then she sent me a piece of code that they write, and instead of cout and cin there were printf and scanf. This should be C. So, I rewrote the program with using printf and scanf, and it still says code errors are blocking (I still left the classes at the request of the task).

Does C have classes? Or is there a misunderstanding or something like that?

+7
source share
6 answers

No, C has no classes. However, there are ways to model object-oriented programming in C - a quick Google search should provide some useful results.

+19
source

No, C does not have classes as such, but only C ++ (which started as "C with classes" then ...). But you can use the standard C library in C ++ code, even if it is often not considered good practice (where C ++ has its own higher-level constructors, for example cout vs printf ).

You can sort the behavior of classes, inheritance, and virtual functions in C too, but it's not worth the pain.

You should probably buy / get your former classmate in the C programming book :-)

+8
source

C has no classes.

But you can approximate a class using static globals as private members of a class, and static functions as private member functions. extern members as public. In this case, the entire file can be considered as a class.

Perhaps this is not what you want.

+2
source

C does not have a formal class design. You can create modules with data at the module level, which, by your own agreement, you will no longer use in other places or static data, as well as write functions in order to receive, install and otherwise manipulate this data. You can even move on to using function pointers to manipulate these types of data as if they were in a class.

However, you will not be protected by class semantics or other rules by the C compiler, because the C compiler does not know about classes. However, structuring your data is quite powerful.

+2
source

The classic case of conflicting requirements seems to be :-)

The terminology of its requirements CLASS, CONSTRUCTOR, METHOD is all C ++ terminology, while none of them is C terminology (the closest of them will probably be STRUCT, INITIALIZATION, FUNCTION). your friend confuses something here. I doubt her teacher is confusing something, though ...

0
source

C has no classes, but you can emulate it with structures and function pointers. C99 is slightly (slightly) C ++ based, so it's easy to play classes with C.

0
source

All Articles