Here’s a detailed explanation of the concepts you mentioned:
1. Polymorphism
Polymorphism allows one interface to be used for multiple
purposes, enabling the same function or operator to behave
differently based on the input or context.
a. Function Overloading
Multiple functions with the same name but different parameter
lists (different types or numbers of arguments).
Example:
void print(int i) { cout << "Integer: " << i << endl; }
void print(double d) { cout << "Double: " << d << endl; }
void print(string s) { cout << "String: " << s << endl; }
b. Operator Overloading
Operators are redefined to work with user-defined types (e.g., + for
complex numbers).
Example:
,class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
};
c. Virtual Functions and Dynamic Polymorphism
Virtual functions: Member functions declared with the virtual
keyword. They allow derived class functions to override base class
functions.
Dynamic polymorphism: Achieved through virtual functions using
runtime binding.
Example:
class Base {
public:
virtual void show() { cout << "Base class" << endl; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived class" << endl; }
};
, 2. Abstract Classes and Pure Virtual Functions
Abstract Class: A class that cannot be instantiated and often serves
as a blueprint for other classes.
Pure Virtual Function: Declared by assigning = 0 in a class, forcing
derived classes to implement the function.
Example:
class AbstractBase {
public:
virtual void display() = 0; // Pure virtual function
};
class Derived : public AbstractBase {
public:
void display() override { cout << "Derived implementation" <<
endl; }
};
3. Encapsulation and Data Hiding
Encapsulation: Wrapping data and methods into a single unit
(class).
Data Hiding: Restricting access to internal details of a class using
access specifiers like private and protected.
1. Polymorphism
Polymorphism allows one interface to be used for multiple
purposes, enabling the same function or operator to behave
differently based on the input or context.
a. Function Overloading
Multiple functions with the same name but different parameter
lists (different types or numbers of arguments).
Example:
void print(int i) { cout << "Integer: " << i << endl; }
void print(double d) { cout << "Double: " << d << endl; }
void print(string s) { cout << "String: " << s << endl; }
b. Operator Overloading
Operators are redefined to work with user-defined types (e.g., + for
complex numbers).
Example:
,class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
};
c. Virtual Functions and Dynamic Polymorphism
Virtual functions: Member functions declared with the virtual
keyword. They allow derived class functions to override base class
functions.
Dynamic polymorphism: Achieved through virtual functions using
runtime binding.
Example:
class Base {
public:
virtual void show() { cout << "Base class" << endl; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived class" << endl; }
};
, 2. Abstract Classes and Pure Virtual Functions
Abstract Class: A class that cannot be instantiated and often serves
as a blueprint for other classes.
Pure Virtual Function: Declared by assigning = 0 in a class, forcing
derived classes to implement the function.
Example:
class AbstractBase {
public:
virtual void display() = 0; // Pure virtual function
};
class Derived : public AbstractBase {
public:
void display() override { cout << "Derived implementation" <<
endl; }
};
3. Encapsulation and Data Hiding
Encapsulation: Wrapping data and methods into a single unit
(class).
Data Hiding: Restricting access to internal details of a class using
access specifiers like private and protected.