简体   繁体   English

C 有课程吗?

[英]Does C have classes?

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.我的前同学付钱让我用 C 编写一个程序。她给了我这个任务,它说“等等等等等等,至少制作两个类,至少写一个构造函数并至少重写一种方法”它逐字逐句地说.

And then I told her "this is C++ not C" she said "but we're learning C"然后我告诉她“这是 C++ 不是 C” 她说“但我们正在学习 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.我忽略了它并用 C++ 编写程序并发送给她,因为我认为她不知道她在说什么。 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.她说“它不适用于代码块,wtf 是 cout <<”,然后她给我发送了一大块他们编写的代码,而不是 cout 和 cin,而是 printf 和 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).它必须是 C。所以,我用 printf 和 scanf 重写了程序,她仍然说代码块会抛出错误(我仍然按照任务要求留下类)。

I want to ask wtf?我想问wtf? Does C have classes? C 有课程吗? Or is there a misunderstanding or something?还是有什么误会之类的?

EDIT: I've come back to the question after so many years and noticed some a*****es took time to remove 99% text from the question.编辑:这么多年后我又回到了这个问题,并注意到一些 a*****es 需要时间从问题中删除 99% 的文本。 Get a life, this is not 1984 yet.得到生活,这还不是 1984 年。

No, C doesn't have classes.不,C 没有类。 That said, there are ways of simulating object-oriented programming in C - a quick Google search should yield some useful results.也就是说,有一些方法可以在 C 中模拟面向对象的编程——快速的谷歌搜索应该会产生一些有用的结果。

No, C has no classes per se, only C++ (which started out as "C with classes" back then...).不,C 本身没有类,只有 C++(当时它以“带有类的 C”开始......)。 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, eg cout vs printf ).但是您可以在 C++ 代码中使用标准 C 库,即使它通常不被认为是好的做法(其中 C++ 有自己的更高级别的构造,例如coutprintf )。

You can sort of emulate the behaviour of classes, inheritance and virtual functions in C too, but it's not worth the pain.您也可以在 C 中模拟类、继承和虚函数的行为,但这不值得。

You should probably buy/get your ex classmate a C programming book :-)你应该给你的前同学买一本 C 编程书 :-)

C does not have the formal construct of a class. C 没有类的正式构造。 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.但是,您不会受到 C 编译器的类语义或其他规则的保护,因为 C 编译器不了解类。 However, structuring your data is quite powerful.但是,构建数据非常强大。

C does not have classes. C 没有类。

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.可能这不是你想要的。

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 ).她的需求术语CLASS、CONSTRUCTOR、METHOD都是 C++ 术语,而它们都不是 C 术语(其中最接近的可以说是STRUCT、INITIALIZATION、FUNCTION )。 Your friend is confusing something here.你朋友这混乱的东西。 I doubt that her teacher is confusing something, though...我怀疑她的老师是在混淆什么,不过……

C does not have classes, but you can emulate it with structures and pointers to a function. C 没有类,但您可以使用结构和指向函数的指针来模拟它。 C99 is a little bit (just a bit) based on C++, so it's easy to reproduce classes with C. C99 有一点点(只是一点点)基于 C++,所以很容易用 C 重现类。

C mostly uses functional/structural programming instead of implementing Object Oriented Programming as in languages like C++ , Java , Python etc. which use classes . C 主要使用函数式/结构式编程,而不是像 C++、Java、Python 等使用类的语言那样实现面向对象编程。 But in few instances we use classes like in:但在少数情况下,我们使用如下类:

typedef struct {
    ShapeClass shape;
    float width, height;
}
RectangleClass;

Hope it helped.希望它有所帮助。

C doesn't support classes, but we do have tricky hacky workaround . C 不支持类,但我们确实有棘手的 hacky 解决方法 Read entire explanation or simply scroll down to code section.阅读整个解释或简单地向下滚动到代码部分。

struct declaration:结构声明:

I declared an struct with name class .我声明了一个名为classstruct Inside, I put uninitialized function with type void , name Print and one parameter char .在里面,我放置了类型为void 、名称为Print和一个参数为char 的未初始化function This will look like void class.Print(char);这看起来像void class.Print(char);

struct don't allow initialization inside:结构不允许内部初始化:

But struct doesn't allow initialization of variables inside.但是 struct 不允许在内部初始化变量。 So we will init it outside.所以我们将在外面初始化它。 We created an a function with type of class , name Printer .我们创建了一个类型为Printerfunction It inits that struct and return initialized struct which we can easily make use of.它初始化该struct并返回我们可以轻松使用的初始化结构。

call the class:调用类:

Now we included helper header and declared variable with type class , name printer1 , value Printer() .现在我们包含了 helper 标头和类型为class 、名称为printer1 、值为Printer()声明变量。 After I called member of class and print string using printer1.Print("Hello from class function");在我调用类成员并使用printer1.Print("Hello from class function");打印字符串之后printer1.Print("Hello from class function");

main.c

#include "helper.h"
void main()
{
    class printer1 = Printer();
    printer1.Print("Hello from class function");
}

helper.h

#include <stdio.h> //imports int printf(...);
void print(const char* text)
{
    printf(text);
}

typedef struct 
{
    void (*Print) (const char*);
} class;

class Printer()
{
    class Printer;
    Printer.Print = &print;
    return Printer;
}

Note:笔记:

  • This exact example was compiled and tested successfully with VC and tcc compiler.这个确切的例子是用 VC 和 tcc 编译器成功编译和测试的。
  • class is example name. class是示例名称。 You can use any other name as well.您也可以使用任何其他名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM