answersLogoWhite

0

How do you inherit from class in c?

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

struct SClass{

int iNumber;

int (*fpgetValue)();

int (*fpsetValue)();

}Sc;

int m_Func(){

printf("\n\n Hello How are you doing,i called by function pointer");

getchar();

return 1;

}

int _tmain(int argc, _TCHAR* argv[])

{

Sc.fpgetValue=m_Func;

Sc.fpgetValue();

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

You inherit from an existing class (the base class) by deriving a new class (the derived class) from the existing class. The derived class automatically inherits the public and protected members of its base class.

By way of an example, consider the following base class:

class Base{

private: int m_private;

protected: int m_protected;

public: int m_public;

};

Now consider the following derived classes which all inherit from Base:

class Derived_public : public Base{

};

class Derived_protected : protected Base{

};

class Derived_private : private Base{

};

For the sake of brevity, the three derived classes have no members other than those they inherit from Base.

All three inherit m_public and m_protected. m_private remains private to Base and is therefore inaccessible via the derived classes.

Derived_public is functionally equivalent to the following non-derived declaration:

class NonDerived_public{

public: int m_public;

protected: int m_protected;

};

However, Derived_protected is functionally equivalent to the following non-derived declaration:

class NonDerived_protected{

protected: int m_public;

protected: int m_protected;

};

Note that m_public has switched to protected access.

Finally, Derived_private is functionally equivalent to the following non-derived declaration:

class NonDerived_private{

private: int m_public;

private: int m_protected;

};

Again, note that m_public and m_protected have switched to private access.

It is also possible to change the access to individual members regardless of the inheritance:

class Derived : public Base{

private: int Base:m_public;

public: int Base:m_protected;

};

Note that the type of inheritance has no effect on the base class access specifiers. Only the derived class is modified according to the type of inheritance.

Derived classes can also inherit from more than one base class. Simply separate each inheritance with a comma:

class Base2{};

class MultiDerivative : public Base, public Base2{};

Multiple-inheritance is the same as single inheritance, other than that the derived class inherits the sum total of all public and protected members of its combined base classes.

Multi-level inheritance occurs whenever any derived class inherits from another derived class. The only real difference is that the most-derived class inherits the public and protected members of its most-immediate ancestor, including those it inherited from its base class and that still remain public or protected in that class.

Multi-level and multiple-inheritance can also be combined. However, sometimes this can introduce ambiguity when two or more base classes are themselves derived from a common base class. The most-derived class then inherits two or more instances of that common base class which is often undesirable. To avoid this problem, the common base class can be declared virtual, which then makes it a direct ancestor of the most-derived class, and this one instance is then shared amongst the base classes that are directly derived from it. For instance:

class Common{};

class Derived1 : virtual public Common{};

class Derived2 : public virtual Common{}

class Object : public Derived1, public Derived2{};

Note that the virtual keyword can be placed before or after the inheritance type, as shown in the example.

Without the virtual keyword in both Derived1 and Derived2, Object would inherit two separate instances of Common (one from Derived1, the other from Derived2). This would make it impossible to implicitly access any members of Common that were inherited by Object. You could use explicit access via Derived1::Common or Derived2::Common, but more often than not it is better to eliminate the duplicate instance altogether and have a single shared instance, thus removing the ambiguity. Ideally, the Common class should have few (if any) member variables; its existence should mainly be to provide a virtual (or pure-virtual) interface to all its derivatives.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you inherit from class in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why inherit function is not called?

Inherit is not a function. It is a class derivation where some of the methods and attributes of the new class inherit from a parent class.


Is it ever necessary to add extends Object to a class declaration?

No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.


Final classes in java?

A class declared as final means that no other class can inherit from it.


What is the difference between abstract class and normal class?

Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods. We cannot create an object for the abstract classes. When we inherit the abstract class we should implement the abstract method which we inherit.


What is the advantages of virtual base class?

Virtual classes are useful when you have a derived class that has two base classes which are themselves derived from a common base class. That is, if class A is derived from classes B and C, and classes B and C are both derived from class D, then class A would inherit two instances of class D (one from B, the other from C). This introduces an ambiguity when referring directly to D from A, because there is no way to determine which instance of D you are referring to. By declaring class D to be a virtual base class of both B and C, they will both share the same instance of D, virtually, thus eliminating the ambiguity.

Related questions

Why inherit function is not called?

Inherit is not a function. It is a class derivation where some of the methods and attributes of the new class inherit from a parent class.


Which is the topmost class in java?

The "Object" class is the topmost class in the class hierarchy. Classes inherit directly from this class by default; all classes inherit from Object directly or indirectly.


Examples of inheritance in c plus plus?

Single-inheritance is where one class inherits directly from another class: class A {}; class B : public A {}; Here, class B inherits all the public and protected members of class A. Multiple-inheritance is where one class inherits directly from two or more classes: class A {}; class B {}; class C : public A, public B {}; Here, class C inherits all the public and protected members of both A and B. Multi-level inheritance is where one class inherits from another class that itself derived. class A {}; class B : public A {}; class C : public B {}; Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A. Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A. Another use of virtual inheritance is when you need to make a class final. class A; class B { friend class A; B() {} // private constructor }; class A : public virtual B { }; Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.


Is there any way to apply class diagram with structured programming language like C?

No. Class diagrams only apply to object-oriented languages where one class can inherit or derive from another. There is no inheritance model in C, thus data structures cannot be derived.


Is it ever necessary to add extends Object to a class declaration?

No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.


Can you use multiple inheritance in .NET?

Multiple inheritance in C# In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.


What is the Difference between multilevel and multiple inheritance?

Multiple Inheritance : we can inherit more than one class in the same class. Multi-Level Inheritance: where one class can inherit only one base class and the derived class can become base class of some other class.


Final classes in java?

A class declared as final means that no other class can inherit from it.


The top most class in java?

All classes in java must inherit from the Object class


What is the difference between abstract class and normal class?

Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods. We cannot create an object for the abstract classes. When we inherit the abstract class we should implement the abstract method which we inherit.


Is it possible to make the object of abstract class?

no u cant abstract class u can just inherit from


Which base class is never inherited?

You can't inherit from sealed classes.