5

Does C support inheritance? If so, is it by using structs since classes are not defined in C?

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
ckv
  • 10,539
  • 20
  • 100
  • 144

4 Answers4

12

Yes, it does. See http://gcc.gnu.org/ml/gcc/2010-05/msg00725.html . See Axel-Tobias Schreiner's book Object-Oriented Programming with ANSI C. There's an English translation of it available.

Also, see Object-orientation in C and How can Inheritance be modelled using C? .

Community
  • 1
  • 1
Yktula
  • 14,179
  • 14
  • 48
  • 71
  • Can you add an explanation for the first link? I'm interpreting this as "Yes, you can emulate it, but you really shouldn't." Is that what you were trying to say? – General Grievance Apr 24 '23 at 16:37
8

No, it doesn't. C is not an Object-Oriented language. Inheritance is a property of OO languages.

You should try C++. It's OO and supports much more than inheritance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Midhat
  • 17,454
  • 22
  • 87
  • 114
6

No, it doesn't.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
5

There is no Compiler-level support for inheritance in C. Nevertheless, as others have already pointed out, Object Oriented coding does not REQUIRE such support. However, its a lot easier to write OO code in C++.

swestrup
  • 4,079
  • 3
  • 22
  • 33