types of base class
types of base class
In C++, abase classis a class from which other classes (calledderived
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, Dogdirectly inherits from
Animal Animalis 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,LivingBeingis 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 thediamond
c
problemin 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 virtualkeyword ensures that only one copy of LivingBeingis
Hybrid
inherited by Hybridinherits from both
, even though Animaland
Plant
.
4. Abstract Base Class:_A class that provides a blueprint for other classes
nd cannot be instantiated. It contains at least onepure 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,Shapeis 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
functions (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.