Inheritance
Inheritance is a mechanism in object-oriented programming that
allows a class (child class) to inherit properties and
behaviours (methods) from another class (parent class). This
promotes code reusability and establishes a natural hierarchy
between classes.
Types of Inheritance
1. Single Inheritance: A child class inherits from one
parent class.
2. Multiple Inheritance: A child class inherits from more
than one parent class.
3. Multilevel Inheritance: A class is derived from another
derived class.
4. Hierarchical Inheritance: Multiple classes inherit
from a single parent class.
5. Hybrid Inheritance: A combination of two or more types
of inheritance.
Virtual Base Class
In multiple inheritance, a virtual base class is used to
prevent multiple "instances" of a base class appearing in
an inheritance hierarchy when using multiple inheritance.
This is achieved by declaring the base class as virtual.
Function Overriding
Function overriding occurs when a derived class has a
definition for one of the member functions of the base
class. That base function is said to be overridden.
Abstract Class and Pure Virtual Function
An abstract class is a class that cannot be instantiated
and is designed to be inherited by other classes. It often
contains at least one pure virtual function.
A pure virtual function is a function that has no
implementation in the base class and must be overridden in
derived classes. It is declared by assigning 0 in its
declaration.
Constant Data Member and Member Function
• Constant Data Member: A constant data member is a
variable whose value cannot be changed once it is
initialized. It is declared using the const keyword. This
ensures that the value remains constant throughout the
lifetime of the object.
• Constant Member Function: A member function that does not
modify any data members of the class. It is declared using
the const keyword at the end of the function declaration.
This guarantees that the function will not alter the state
of the object.
Static Data Member and Member Function
• Static Data Member: A data member that is shared by all
objects of the class. It is declared using the static
keyword. Static data members are initialized outside the
class definition and are common to all instances of the
class.
• Static Member Function: A member function that can be
called without creating an object of the class. It is
declared using the static keyword. Static member functions
can only access static data members and other static member
functions.
Polymorphism
Polymorphism allows objects of different classes to be treated
as objects of a common base class. It is achieved through
function overloading, operator overloading, and virtual
functions.
• Function Overloading: Multiple functions with the same
name but different parameters. This allows functions to be
defined with the same name but to perform different tasks
based on the parameters passed.
• Operator Overloading: Defining a new behaviour for an
existing operator. This allows operators to be used with
user-defined types in a way that is intuitive and
consistent with their use with built-in types.
Dynamic Binding and Virtual Function
• Dynamic Binding: The code to be executed in response to
a function call is determined at runtime. It is achieved
using virtual functions. This allows for more flexible and
reusable code.
• Virtual Function: A function in a base class that can be
overridden in derived classes. It is declared using the
virtual keyword. Virtual functions enable polymorphism by
allowing derived classes to provide specific
implementations of functions that are defined in the base
class.
Exception Handling
Exception handling is a mechanism to handle runtime errors,
ensuring the normal flow of the program. It uses try, catch, and
throw keywords.
• try: Block of code where exceptions might occur.
• catch: Block of code that handles the exception.
• throw: Used to throw an exception.
Example:
Templates
Templates allow writing generic programs that can work with any
data type. They are used for creating functions and classes.
• Function Template: A template for a function.
• Class Template: A template for a class.
Stream Classes
Stream classes are used for input and output operations in
C++. They are part of the iostream library.
• cin: Standard input stream.
• cout: Standard output stream.
• cerr: Standard error stream.
• clog: Standard log stream.
Example:
File Handling
File handling in C++ involves reading from and writing to
files using file stream classes: ifstream, ofstream, and
fstream.
• ifstream: Input file stream (reading from files).
• ofstream: Output file stream (writing to files).
• fstream: File stream (both reading and writing).
Example: