Does C have classes? [closed]

2019-02-03 07:11发布

Okay, you may call me a noob but I'm realling confused.

My ex classmate paid me to write a program in C. She gave me the task and it said something like "blah blah blah make at least two classes, write at least one constructor and rewrite at least one method" it says that word by word.

And then I told her "this is C++ not C" she said "but we're learning C"

I ignored it and wrote the program in c++ and sent to her as I thought she didn't know what she was talking about. She said "it doesn't work on code blocks, and wtf is cout <<" and then she sent me a chunk of code that they write and instead of cout and cin there was printf and scanf. It had to be C. So, I rewrote the program with printf and scanf and she still says codeblocks throw errors (I still left classes as task demanded).

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

标签: c class
6条回答
戒情不戒烟
2楼-- · 2019-02-03 07:40

No, C doesn't have classes. That said, there are ways of simulating object-oriented programming in C - a quick Google search should yield some useful results.

查看更多
Bombasti
3楼-- · 2019-02-03 07:40

A classic case of conflicting requirements, it seems :-)

The terminology of her requirements CLASS, CONSTRUCTOR, METHOD are all C++ terminology, while none of them is C terminology (the closest of which would arguably be STRUCT, INITIALIZATION, FUNCTION). Your friend is confusing something here. I doubt that her teacher is confusing something, though...

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-03 07:44

C does not have classes.

But one can approximate a class by using static globals as private class members, and static functions as private member functions. extern members as public. In this case an entire file could be viewed as a class.

Probably this is not what you want.

查看更多
爷、活的狠高调
5楼-- · 2019-02-03 07:46

C does not have classes, but you can emulate it with structures and pointers to a function. C99 is a little bit (just a bit) based on C++, so it's easy to reproduce classes with C.

查看更多
劫难
6楼-- · 2019-02-03 07:47

C does not have the formal construct of a class. You can produce modules with module-level data that by your own agreement you will not extern anywhere else, or static data, and write functions to get, set, and otherwise manipulate that data. You can even go to the point of using function pointers to manipulate similar data types as if they were in a class.

However, you won't 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.

查看更多
forever°为你锁心
7楼-- · 2019-02-03 07:48

No, C has no classes per se, only C++ (which started out as "C with classes" back 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 constructs, e.g. cout vs printf).

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

You should probably buy/get your ex classmate a C programming book :-)

查看更多
登录 后发表回答