22 Lecture

CS304

Midterm & Final Term Short Notes

PRACTICAL IMPLEMENTATION OF INHERITANCE IN C

Inheritance is a powerful concept in object-oriented programming that allows the creation of new classes based on existing classes. In C, inheritance can be implemented using structures and function pointers. Inheritance allows the reuse of code


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Inheritance in C can be implemented using: a) Structures and function pointers b) Classes and objects c) Inheritance keyword d) None of the above Answer: a) Structures and function pointers Which of the following is not a type of inheritance? a) Single inheritance b) Multiple inheritance c) Hierarchical inheritance d) Parallel inheritance Answer: d) Parallel inheritance The derived class inherits: a) All the properties and methods of the base class b) Only the properties of the base class c) Only the methods of the base class d) None of the above Answer: a) All the properties and methods of the base class What is the syntax to define a derived structure in C? a) struct Derived : Base {} b) struct Derived extends Base {} c) struct Derived : public Base {} d) struct Derived : private Base {} Answer: c) struct Derived : public Base {} Inheritance is used to: a) Achieve code reusability b) Encapsulate data c) Control access to data d) None of the above Answer: a) Achieve code reusability Which type of inheritance allows a derived class to inherit from multiple base classes? a) Single inheritance b) Multiple inheritance c) Hierarchical inheritance d) Hybrid inheritance Answer: b) Multiple inheritance Which keyword is used to call the constructor of the base class from the derived class constructor? a) super b) base c) this d) parent Answer: b) base Which type of inheritance involves creating a new class that inherits from a base class, and then creating another class that inherits from the new class? a) Single inheritance b) Multiple inheritance c) Hierarchical inheritance d) Hybrid inheritance Answer: c) Hierarchical inheritance Which of the following is not a benefit of inheritance? a) Code reusability b) Improved maintainability c) Reduced coupling d) Increased code complexity Answer: d) Increased code complexity Which of the following is an example of polymorphism? a) Overriding a method in a derived class b) Calling a method from the base class c) Inheriting properties from a base class d) None of the above Answer: a) Overriding a method in a derived class


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. How is inheritance implemented in C? Answer: Inheritance in C is implemented using structures and function pointers. What is the difference between a base class and a derived class? Answer: A base class is a class from which other classes are derived, while a derived class is a class that inherits properties and methods from the base class. How is a derived structure defined in C? Answer: A derived structure in C is defined using the syntax "struct Derived : public Base {}". What is the purpose of inheritance? Answer: The purpose of inheritance is to achieve code reusability and simplify the creation of new classes with similar functionality to existing classes. What is multiple inheritance? Answer: Multiple inheritance is a type of inheritance in which a derived class can inherit from multiple base classes. How do you call the constructor of the base class from the derived class constructor? Answer: The constructor of the base class can be called from the derived class constructor using the "base" keyword. What is hierarchical inheritance? Answer: Hierarchical inheritance is a type of inheritance in which a new class is created that inherits from a base class, and then another class is created that inherits from the new class. What are the benefits of inheritance? Answer: The benefits of inheritance include code reusability, improved maintainability, and reduced coupling. What is function overriding in inheritance? Answer: Function overriding in inheritance is when a derived class defines a method with the same name and signature as a method in the base class, effectively replacing the method in the base class. How does inheritance relate to polymorphism? Answer: Inheritance and polymorphism are related in that polymorphism is achieved through inheritance, specifically through the use of function overriding.

In C programming language, inheritance can be achieved through the use of structures and function pointers. A derived structure is defined by including a base structure as the first member of the derived structure, followed by the additional members specific to the derived structure. The derived structure can access the members of the base structure using the dot notation or the arrow notation. Function pointers are used to implement the concept of virtual functions, which allow a derived structure to override a function defined in the base structure. This is achieved by declaring a function pointer in the base structure that points to the function that can be overridden by the derived structure. The derived structure then overrides the function by defining a new function with the same signature and pointing the function pointer to the new function. Multiple inheritance can also be implemented in C by including multiple base structures in the derived structure. However, this can lead to name conflicts if the base structures contain members with the same name. To resolve this, the derived structure can use the dot notation to specify which base structure member to access. Inheritance can greatly improve code reusability and maintainability by allowing the creation of new structures with similar functionality to existing structures. It also simplifies the code structure by organizing related code into a hierarchy of structures. Overall, while C is not a purely object-oriented language, inheritance can still be practically implemented through the use of structures and function pointers, providing a useful tool for code organization and reuse.