is C a OOP language?

I know that c++ was made to add object oriented programming to C but I recently discovered the struct key word

were you can create at least object looking code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct data
{
 int x = 5;
 double y = 10;
};

int main()
{
 struct data dataobj;
 dataobj.x = 12;
 dataobj.y = 45.678;
 return 0;
}


although it cant store functions isnt this basic opject oriented programming?
I am new to c so this may be a stupid question.
closed account (z05DSL3A)
It is not an object oriented language per se but you can use it in an object oriented way.

Google: "object oriented programming in c"
Last edited on by Canis lupus
tomplusplus wrote:
although it cant store functions isnt this basic opject oriented programming?
I am new to c so this may be a stupid question.


In C++, the only difference between a struct and a class, is that the struct defaults to public access, whereas a class defaults to private - (where no access type is specified). So structs can indeed have constructors, destructors, member functions, virtual functions & inheritance etc.

Have a read of the tutorial at the top left of this page, there is lots of info in the reference section as well, plus the articles.

EDIT: Stupid questions are when people don't learn from their mistakes - simple question are OK, remember that everyone has to start from somewhere. People are happy to help anyone from any level, as long there is some effort shown (which you have)

Canis lupus wrote:
It is not an object oriented language per se but you can use it in an object oriented way.


Would it be clearer to say that C++ is not as "pure OOP" as some other languages like Eiffel or D say? I have the impression that C++ has most nearly all of the features of OOP, there are only a few that it doesn't have.

Not trying to be pedantic, but the OP is a beginner - may be it is better not to leave the impression that "C++ is not really a OOP language".

Hope all is well at your end :+)

Last edited on
C certainly allows for encapsulation and OOP. Take a look at the cstdio header (ie: fopen, fread, FILE*, etc). FILE is an encapsulated object, and you can only access it with public functions fread/fwrite/etc. It's similar to C++'s iostream header.

So yeah... you can do OOP in C. C++ just makes it easier.
although it cant store functions isnt this basic opject oriented programming?

Well you can store function pointers, so it's close enough.
http://www.newty.de/fpt/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

typedef void (*FunctionPointer)(const char *);

struct Example
{
    const char *name;
    FunctionPointer fp0;
    FunctionPointer fp1;
};

void say_hello(const char *name)
{
    printf("Hello there, %s!\n", name);
}

void say_goodbye(const char *name)
{
    printf("Goodbye, %s!\n", name);
}

int main(void)
{
    struct Example e;

    e.name = "George Clooney";
    e.fp0 = &say_hello;
    e.fp1 = &say_goodbye;

    e.fp0(e.name); // clumsy
    e.fp1(e.name); // clumsy
}
Hello there, George Clooney!
Goodbye, George Clooney!

closed account (z05DSL3A)
TheIdeasMan wrote:
Not trying to be pedantic, but the OP is a beginner - may be it is better not to leave the impression that "C++ is not really a OOP language".
Also not being pedantic but the OP was talking C
@Canis lupus

You are right - my bad :+D. The reference to C++ in the OP's first sentence set me off on a tangent which led to my discombobulation!
Topic archived. No new replies allowed.