Polymorphism
English: Polymorphism means the ability of a function, operator, or object to take multiple forms. It allows the
same function or operator to behave differently based on the context.
Types:
- Compile time polymorphism (Function and Operator overloading)
- Runtime polymorphism (Virtual functions)
Example (Function Overloading):
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
Hinglish: Polymorphism ka matlab hai ek chiz ko alag alag tarike se use kar paana. Jaise ek function ya
operator situation ke hisaab se alag kaam kare.
Call by Value and Call by Reference
English: In call by value, a copy of the variable is passed to the function. Changes made inside the function
do not affect the original value. In call by reference, the address of the variable is passed. Changes made
inside the function directly affect the original value.
Example:
void byValue(int x) { x = x + 5; }
void byReference(int &x) { x = x + 5; }
Hinglish: Call by value me function ko variable ki copy milti hai, asli value safe rehti hai. Call by reference me
function ko asli variable ka address milta hai, isliye asli value change ho sakti hai.
Abstraction
English: Abstraction means showing only important information and hiding the details that are not needed. It
helps to reduce complexity and increases efficiency.
Example:
class Car {
public:
void drive() { } // user uses this
private:
void engineDetails() { } // hidden from user
};
Hinglish: Abstraction ka matlab hai sirf important chiz dikhana aur baaki chhupana. Jaise car chalate waqt sirf
steering, brake dekhte hain - engine kaise kaam karta hai ye nahi dikhate.
Friend Function
English: A friend function is not a member of a class but can access the private and protected members of
the class. It is declared using the friend keyword.
Example:
class Test {
private:
int a;
public:
friend void show(Test t);
};
void show(Test t) { cout << t.a; }
Hinglish: Friend function class ke andar nahi hota, par wo class ke private data ko dekh sakta hai. Jaise class
ka dost - dost private baat jaanta hai.
Virtual Base Class
English: A virtual base class is used in multiple inheritance to prevent multiple copies of the base class
members. It solves the diamond problem by ensuring only one instance of the base class is inherited.
Example:
class A { };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };
Hinglish: Jab ek class ko 2 jagah se inherit karte hain (diamond shape banta hai), tab same base class 2
baar aa sakti hai. Is problem ko virtual base class rokta hai.
Operator Overloading
English: Operator overloading allows us to give special meaning to an operator for user-defined data types.
The same operator can perform different operations based on the operands.
Example:
class Complex {
int real, imag;
public:
Complex(int r, int i) { real = r; imag = i; }
Complex operator + (Complex c) {
return Complex(real + [Link], imag + [Link]);
};
Hinglish: Operator overloading me hum operator (+, -, *, etc.) ko apne hisaab se naya meaning de sakte hain.
Jaise + operator numbers me add kare aur strings me join kare.
Constructor Overloading
English: Constructor overloading means having multiple constructors in a class with different sets of
parameters. It allows objects to be created in different ways.
Example:
class Test {
public:
Test() { }
Test(int x) { }
};
Hinglish: Constructor overloading matlab class ke andar multiple constructors hote hain, par unke arguments
alag alag hote hain. Isse object ko alag alag tarike se bana sakte hain.
Overloading
English: Overloading means using the same function or operator name to perform different tasks based on
the arguments or operands. It increases code readability and reusability.
Example (Function Overloading):
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
Hinglish: Overloading ka matlab hai same naam ka function ya operator alag alag kaam kare, arguments ke
hisaab se. Isse code simple aur smart dikhta hai.