0% found this document useful (0 votes)
13 views

types of base class

In C++, base classes can be categorized into four types: Direct Base Class, which is directly inherited by another class; Indirect Base Class, inherited through another base class; Virtual Base Class, which prevents duplication in multiple inheritance; and Abstract Base Class, which cannot be instantiated and contains at least one pure virtual function. Code reusability is emphasized through features like functions, classes, inheritance, templates, the Standard Template Library (STL), and polymorphism, which collectively save time, reduce errors, improve maintainability, and enhance readability. Overall, these concepts are fundamental to object-oriented programming in C++.

Uploaded by

Sukhwinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

types of base class

In C++, base classes can be categorized into four types: Direct Base Class, which is directly inherited by another class; Indirect Base Class, inherited through another base class; Virtual Base Class, which prevents duplication in multiple inheritance; and Abstract Base Class, which cannot be instantiated and contains at least one pure virtual function. Code reusability is emphasized through features like functions, classes, inheritance, templates, the Standard Template Library (STL), and polymorphism, which collectively save time, reduce errors, improve maintainability, and enhance readability. Overall, these concepts are fundamental to object-oriented programming in C++.

Uploaded by

Sukhwinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‭Explain the types of base classes in c++ in detail.


I‭n C++, a‬‭base class‬‭is a class from which other classes (called‬‭derived‬
‭classes‬‭) inherit properties and behaviors. Depending on how the inheritance is‬
‭structured and used, base classes can be classified into the following types:‬
‭1. Direct Base Class:-‬‭A class that is directly inherited by another class is called‬
‭direct base class.‬
‭Example‬‭:‬
class Animal {‬

public:‬

void eat() {‬

cout << "Animal is eating." << endl;‬

}‬

};‬

class Dog : public Animal {‬

// Dog directly inherits from Animal‬

};‬

‭Here,‬‭ Dog‬‭directly inherits from‬‭
Animal‬ Animal‬‭is a direct base class.‬
‭, so‬‭
‭2. Indirect Base Class:-‬‭A class that is not directly inherited by the derived class‬
‭but is inherited through another base class.‬
‭Example‬‭:‬
class LivingBeing {‬

public:‬

void breathe() {‬

cout << "Breathing..." << endl;‬

}‬

};‬

class Animal : public LivingBeing {‬

// Animal inherits from LivingBeing‬

};‬

class Dog : public Animal {‬

// Dog indirectly inherits from LivingBeing‬

};‬

Here,LivingBeing‬‭is the indirect base class of‬‭
‭ Dog‬ ‭, inherited through‬
Animal‬
‭ ‭.‬
‭3. Virtual Base Class:-‬‭A base class that is shared between multiple derived‬
‭ lasses to prevent duplication of inherited members. To solve the‬‭diamond‬
c
‭problem‬‭in multiple inheritance, Virtual Base class is used..‬
‭Example‬‭:‬
class LivingBeing {‬

public:‬

void exist() {‬

cout << "Existing..." << endl;‬

}‬

};‬

class Animal : virtual public LivingBeing { };‬

class Plant : virtual public LivingBeing { };‬

class Hybrid : public Animal, public Plant {‬

// Only one copy of LivingBeing exists in Hybrid‬

};‬

‭Here, The‬‭ virtual‬‭keyword ensures that only one copy of‬‭ LivingBeing‬‭is‬
Hybrid‬
‭inherited by‬‭ Hybrid‬‭inherits from both‬‭
‭, even though‬‭ Animal‬‭and‬
Plant‬
‭ ‭.‬
‭4. Abstract Base Class:_‬‭A class that provides a blueprint for other classes‬
‭ nd cannot be instantiated. It contains at least one‬‭pure virtual function‬‭. Object‬
a
‭of abstract class can not be created,because it contains one or more pure virtual‬
‭functions without definition..An abstract class is used as a template on which the‬
‭class hierarchy may be created.‬
‭Considerations:-‬‭1. It can only be used through inheritance.‬
‭2.It can not be used as an argument type in call by value‬
‭3. It can not be used as an return type‬
‭4. It can not be used as the type of an explicit conversion‬
‭5. Pointers and references to abstract class can be declared.‬
‭Example‬‭:‬
class Shape {‬

public:‬

virtual void draw() = 0; // Pure virtual function‬

};‬

class Circle : public Shape {‬

public:‬

void draw() override {‬

cout << "Drawing a circle." << endl;‬

}‬

‭;‬
}
Here,Shape‬‭is an abstract base class because it has a pure virtual function‬

‭(‭d
‬ raw‬ Shape‬
‭). You cannot create an object of‬‭ ‭, but it can be used as a base for‬
Circle‬
‭classes like‬‭ ‭.‬
‭Code reusability :-‬‭Code reusability in C++ refers to the concept of writing‬
‭ ode in a way that allows it to be used in multiple programs or multiple parts of‬
c
‭the same program without rewriting it. It is one of the key benefits of‬
‭object-oriented programming (OOP) and helps reduce duplication, save time,‬
‭and make code easier to maintain and extend.‬
‭C++ provides several features and mechanisms to achieve code‬
‭reusability.These features are given below:-‬
‭1. Functions:-‬‭Functions allow you to write reusable blocks of code that can‬
‭be called multiple times with different inputs.‬
‭ . Classes and Objects:-‬‭Classes encapsulate data and functions into reusable‬
2
‭components. Once a class is defined, you can create multiple objects from it.‬
‭3. Inheritance:-‬‭Inheritance allows a new class (derived class) to reuse the‬
‭properties and methods of an existing class (base class).‬
‭4. Templates:-‬‭Templates allow you to write generic code that works for different‬
‭data types, making it highly reusable.‬
‭5. Standard Template Library (STL):-‬‭C++ provides pre-built classes and‬
f‭unctions (like vectors, stacks, queues, etc.) in the STL, which you can‬
‭reuse in your programs.‬
‭6. Polymorphism:-‬‭Polymorphism allows you to write code that works with‬
‭different types of objects in a reusable way, especially through base class‬
‭pointers or references.‬
‭Benefits of Code Reusability‬
‭ .‬ ‭Saves Time‬‭: You don’t need to write the same logic again and again.‬
1
‭2.‬ ‭Reduces Errors‬‭: Tested and reusable code is less likely to have bugs.‬
‭3.‬ ‭Improves Maintainability‬‭: Changes in reusable components automatically‬
‭reflect wherever they are used.‬
‭4.‬ ‭Enhances Readability‬‭: Reusable code is modular, making programs‬
‭easier to understand.‬

You might also like