Question:
what is inheritance in c lenguage?
2007-08-31 09:31:15 UTC
what is inheritance in c lenguage?
Seven answers:
Neeraj Yadav♄
2007-08-31 09:52:45 UTC
I disagree with all above posts..



Here's what i feel

***************

Inheritance is a mechanism by which new and more specialized classes can be defined in terms of existing classes. When a child class (subclass) inherits from a parent class (superclass), the subclass then includes the definitions of all the attributes and methods that the superclass defines. Usually, the subclass extends the superclass by adding its own attributes and methods. Objects that are instances of the subclass contain all data defined by the subclass and its parent classes, and they are able to perform all operations defined by this subclass and its parents.



This kind of class relationship can be implemented in C by embedding the parent class attribute structure as the first member of the child class structure.structuring results in such an attribute alignment that a pointer to the Child class can always be safely cast (upcast) on the pointer to the Parent class. In particular, this pointer can always be passed to any C function expecting a pointer to the Parent class. (To be strictly correct in C, you should explicitly upcast this pointer.) This means that all methods of parent classes are automatically available to child classes-in other words, they are inherited.



This simple approach works only for single inheritance (one parent class) because a class with many parent classes cannot align attributes with all of those parents.



I've named the inherited member super to make the inheritance relationship between classes more explicit and to increase the similarity to Java. The super member provides a handle to access the superclass attributes. For example, a grandchild class can access its grandparent protected attribute _foo as this-> super.super._foo.



Inheritance adds responsibilities to class constructors and the destructor. Because each child object contains an embedded parent object, the child constructor must take care of initializing the portion controlled by the parent. To avoid any potential dependencies, the superclass constructor should be called before initializing the attributes. Exactly the opposite holds true for the destructor. The inherited portion should be destroyed as the last step.



In my implementation, I also adopt from Java the concept of a single abstract base class Object. This means that no class can be defined as standalone, but rather must extend some other class, with the Object class at the root of the class hierarchy. This setup is especially convenient in this hybrid implementation (OO add-on to a procedural language) because every object can be ultimately treated as the Object class instance-which clearly separates objects from all other types. This differs from the C++ approach, in which each structure is equivalent to a class. As I will demonstrate, my Object class adds important behavior, subsequently inherited by all other classes, thus enabling polymorphism.



Listing 1 shows the declaration of a basic class String extending the class Object. The class encapsulates a character buffer (__buffer), provides two ways of construction (StringCon1, StringCon2), a destructor, and a method for read-only access to the character buffer StringToChar. The class is declared by means of preprocessor macros: CLASS, VTABLE, METHODS, and END_CLASS, which are defined and documented in object.h



so forth visit.....

http://www.embedded.com/97/fe29712.htm
voorhies
2016-12-10 20:52:52 UTC
Inheritance In C Programming
gobexe
2007-08-31 09:35:55 UTC
Wikipedia says:

In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification.
Jae
2007-08-31 09:46:33 UTC
Inheritace is not possible in C like language... It is only Possible when the Language supports the Object Oriented Concepts...

If u want to use inheritance like concepts but not actual Inheritance... Use the Sub-structure(Inner Structure) within a Structure.... or create structure variable within a structure...
Elana
2007-08-31 09:38:32 UTC
Inheritance tends to be a property of object oriented languages, which "C" is not.

"C++", "Java", "Small talk" etc all have inheritance.



Inheritance is where you declare an object definition to be a type of another object definition, so everything you don't re-specify is assumed to be like the object being used as a reference. Object def 1 "inherits" object def 2's properties.
ankit G
2007-08-31 09:47:24 UTC
well its like this you inherit certain qualities/features from your parents similarly in Object Oriented Language like c++ (not c) we model entities on real life objects hence the need for inheritance for ex:

you have a class/entity "Vehichle" in which you describe a general vehichle it could be anything a Car/Bus etc

now you have a entity named "Cars". This shall have all features of a vehicle so instead of writing the whole code again you can inherit from "Vehicle" class and it will have all attributes of the super class
2007-08-31 09:36:43 UTC
The C language isn't object-oriented and doesn't have inheritence.



C++ does though.



Check this out:



http://en.wikipedia.org/wiki/Inheritance_(computer_science)



#


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...