0% found this document useful (0 votes)
6 views11 pages

CPP Oop Complete

This document provides comprehensive notes on C++ Object-Oriented Programming (OOP) concepts, including operators that cannot be overloaded, abstract classes, types of inheritance, memory management, inline functions, function overloading, and more. It also covers advanced topics such as exception handling, access specifiers, constructors, friend functions, and polymorphism. The notes are structured in a way that is exam-ready, with examples and explanations in Hinglish.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

CPP Oop Complete

This document provides comprehensive notes on C++ Object-Oriented Programming (OOP) concepts, including operators that cannot be overloaded, abstract classes, types of inheritance, memory management, inline functions, function overloading, and more. It also covers advanced topics such as exception handling, access specifiers, constructors, friend functions, and polymorphism. The notes are structured in a way that is exam-ready, with examples and explanations in Hinglish.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ OOP — Complete Notes

Hinglish mein | 4-Mark Answers | Exam Ready

1. Operators Which CANNOT Be Overloaded


Yeh 5 operators C++ mein kabhi overload nahi ho sakte:

Operator Name

:: Scope Resolution

.* Pointer to Member

. Member Access (dot)

?: Ternary / Conditional

sizeof Size Operator


• Trick: 'Scope Member Ternary Size' — yeh sab fixed hain, change nahi hote.

2. Abstract Class
Wo class jisme kam se kam ek pure virtual function ho, abstract class kehlaati hai. Iska object directly nahi ban
sakta — sirf base class ki tarah kaam karti hai.
class Shape {
public:
virtual void draw() = 0; // pure virtual — body nahi
};

class Circle : public Shape {


public:
void draw() { cout << "Drawing circle"; } // implement karna zaroori
};

// Shape s; // ERROR — abstract class ka object nahi banega


Circle c; // OK

• = 0 matlab pure virtual function


• Derived class ko saare pure virtual functions define karne padte hain

3. Types of Inheritance

Type Description Syntax

Single Ek parent, ek child class B : public A {}

Multiple Do+ parents, ek child class C : public A, public B {}

Multilevel Chain: A->B->C class C : public B {}


class B : public A {} class C : public A
Hierarchical Ek parent, multiple children
{}

Hybrid Upar ke combinations ka mix Combination of above

4. Memory Management Operators in C++

Operator Kaam

new Heap pe single variable allocate karo

delete Single variable ki memory free karo

new[] Array ke liye heap memory lo

delete[] Array memory free karo

int* p = new int(10); // allocate + initialize


delete p; // free — zaroori hai!

int* arr = new int[5]; // array allocate


delete[] arr; // array free

• Rule: Har new ke saath delete zaroori hai — warna memory leak hoga

5. Inline Function
Jab function call hone ki jagah uska code directly wahan paste ho jaye — function call ka overhead khatam.
inline keyword use hota hai.
inline int square(int x) {
return x * x;
}

int main() {
cout << square(5); // compiler yahan 5*5 paste kar deta hai
}

• Chhote, frequently called functions ke liye best


• Compiler inline ko ignore bhi kar sakta hai — yeh sirf request hai

6. Function Overloading
Same naam ke multiple functions, but alag parameters — compile-time polymorphism ka example.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }

cout << add(2, 3); // int version


cout << add(2.5, 1.5); // double version
cout << add(1, 2, 3); // 3-param version

• Return type alag hona kaafi nahi — parameters alag hone chahiye
• Compiler parameter type/count dekh ke decide karta hai

7. Class and Object


Class — blueprint/template jo data aur functions define karta hai. Object — class ka real instance.
class Student { // CLASS — blueprint
private:
string name;
int age;
public:
void setData(string n, int a) { name = n; age = a; }
void display() { cout << name << " " << age; }
};

int main() {
Student s1; // OBJECT — actual instance
[Link]("Rahul", 20);
[Link](); // Rahul 20
}

8. get() and put() Functions


get() — stream se ek character padhta hai (whitespace bhi). put() — stream pe ek character likhta hai.
// get() — input
char ch;
[Link](ch); // ek character lo (whitespace bhi padh leta)
cout << ch;

// put() — output
[Link]('A'); // 'A' print karo
[Link](65); // ASCII 65 = 'A'

• cin >> whitespace skip karta hai, lekin get() nahi karta

9. Destructor
Object destroy hone par automatically call hone wala special function. Memory/resources free karta hai.
class Demo {
public:
Demo() { cout << "Constructor called"; } // object bana
~Demo() { cout << "Destructor called"; } // ~ se shuru
};

int main() {
Demo d; // constructor call
} // scope khatam — destructor auto call

• Tilde (~) se shuru hota hai naam


• No return type, no parameters — overload nahi ho sakta
• Ek class mein sirf EK destructor hota hai

10. Encapsulation
Data aur usse kaam karne wale functions ko ek unit (class) mein band karna. Data private, functions public.
class BankAccount {
private:
double balance; // data chupa ke rakha
public:
void deposit(double amt) { balance += amt; }
void withdraw(double amt) { balance -= amt; }
double getBalance() { return balance; }
};

• Data directly access nahi hota bahar se


• Sirf public functions ke through access milta hai — data hiding

11. Reference Variable


Kisi existing variable ka doosra naam (alias). Same memory share karte hain dono.
int x = 10;
int& ref = x; // ref, x ka alias hai — same memory

ref = 50;
cout << x; // 50 — x bhi change ho gaya

// Function mein use karo — copy avoid hogi


void doubleIt(int& val) { val *= 2; } // original change hoga

• Declare karte waqt initialize karna ZAROORI hai


• Pointer ki tarah * ya & baar baar nahi likhna padta

12. Operator Overloading


Existing operators ko user-defined classes ke liye naya meaning dena. operator keyword use hota hai.
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}

Complex operator+(Complex c) { // + overload kiya


return Complex(real+[Link], imag+[Link]);
}
void display() { cout << real << "+" << imag << "i"; }
};

int main() {
Complex c1(2,3), c2(1,4);
Complex c3 = c1 + c2; // operator+ call hoga
[Link](); // 3+7i
}

13. Two Manipulators


setw(n) — field width set karo (iomanip header chahiye):
#include <iomanip>
cout << setw(10) << "Hello"; // " Hello" (right-aligned)
cout << setw(10) << 42; // " 42"

setprecision(n) — decimal places set karo:


cout << fixed << setprecision(2) << 3.14159; // 3.14
cout << fixed << setprecision(4) << 3.14159; // 3.1416

14. Scope Resolution Operator (::)


:: ka kaam — class ya namespace ke bahar se member access karna. Do main uses:
// Use 1: Class ke bahar function define karo
class Demo {
public:
void show();
};
void Demo::show() { cout << "Hello"; } // :: use kiya

// Use 2: Global variable access karo jab local same naam ka ho


int x = 100;
int main() {
int x = 10;
cout << ::x; // global x = 100
cout << x; // local x = 10
}

15. this Pointer


Current object ka apna address hold karta hai. Automatically milta hai har non-static member function mein.
class Student {
string name;
public:
void setName(string name) {
this->name = name; // this->name = member, name = parameter
}
Student& getObj() {
return *this; // current object return karo
}
};

• this pointer implicit hota hai — tumhe pass nahi karna


• Static functions mein this nahi hota

16. Virtual Function


Base class mein virtual keyword se declare hota hai. Runtime pe derived class ka version call hota hai —
Runtime Polymorphism.
class Animal {
public:
virtual void sound() { cout << "Generic sound"; }
};

class Dog : public Animal {


public:
void sound() { cout << "Woof"; }
};

int main() {
Animal* a = new Dog();
a->sound(); // "Woof" — Dog ka version call hua (runtime pe decide)
}

• Bina virtual ke base class ka function call hota


• virtual se correct derived version milta hai — late binding
17. Pure Virtual Function
Jis virtual function ka koi body nahi hota — sirf = 0 likha hota hai. Class abstract ban jaati hai.
class Shape {
public:
virtual double area() = 0; // pure virtual — no body
virtual void draw() = 0;
};

class Circle : public Shape {


double r;
public:
Circle(double r) : r(r) {}
double area() { return 3.14 * r * r; } // implement karna ZAROORI
void draw() { cout << "Drawing circle"; }
};

• Derived class mein implement karna zaroori — warna wo bhi abstract ban jaati

18. Exception Handling


Runtime errors ko gracefully handle karna. try, catch, throw use hote hain.
int divide(int a, int b) {
if (b == 0)
throw "Division by zero!"; // error throw karo
return a / b;
}

int main() {
try {
cout << divide(10, 0); // risky code yahan
}
catch (const char* msg) { // error pakdo
cout << "Error: " << msg;
}
catch (...) { // koi bhi error
cout << "Unknown error";
}
}

• try — risky code yahan rakho


• throw — error signal karo
• catch — error handle karo

19. Access Specifiers


Class members ka visibility control karte hain. Teen types hain:

Specifier Same Class Derived Class Outside

private Yes No No

protected Yes Yes No

public Yes Yes Yes

class Demo {
private: int x; // sirf Demo ke andar
protected: int y; // Demo + derived classes
public: int z; // sab access kar sakte
};

• Default: class mein private, struct mein public

20. Constructor
Object banate waqt automatically call hone wala special function. Object initialize karta hai.
class Box {
int length;
public:
Box() { length = 0; } // Default constructor
Box(int l) { length = l; } // Parameterized
Box(Box& b) { length = [Link]; } // Copy constructor
};

Box b1; // Default


Box b2(10); // Parameterized
Box b3(b2); // Copy

• Class jaisa naam, no return type


• Types: Default, Parameterized, Copy

21. Friend Function


Jo function class ka member nahi hai, lekin class ke private aur protected members access kar sake.
class Box {
int side;
public:
Box(int s) : side(s) {}
friend void printSide(Box b); // friend declare inside class
};

void printSide(Box b) {
cout << [Link]; // private member access kar liya
}

int main() {
Box b(5);
printSide(b); // 5
}

• friend keyword class ke andar likhte hain


• Function class ka member nahi hota — this pointer nahi milta

22. Pointer to Pointer


Ek pointer jo doosre pointer ka address store kare. Double asterisk (**) use hota hai.
int x = 10;
int* p = &x; // p stores address of x
int** pp = &p; // pp stores address of p

cout << x; // 10 — direct


cout << *p; // 10 — single dereference
cout << **pp; // 10 — double dereference
**pp = 99;
cout << x; // 99 — x change ho gaya

23. Data Abstraction


User ko sirf zaroori details dikhao, internal implementation chupao. OOP ka ek pillar.
class Stack {
int arr[100], top; // implementation hidden
public:
Stack() { top = -1; }
void push(int x) { arr[++top] = x; }
int pop() { return arr[top--]; }
bool isEmpty() { return top == -1; }
};

// User ko push/pop/isEmpty pata hai


// arr aur top ka internal kaam user ko nahi pata

• Abstraction aur Encapsulation saath kaam karte hain

24. Static vs Dynamic Polymorphism | Early vs Late Binding

Static Polymorphism Dynamic Polymorphism

Time Compile time Runtime

Example Overloading, Op. Overloading Virtual functions

Binding Early binding Late binding

Speed Fast Thoda slow (vtable)

Flexibility Less More


Early binding — compile time pe decide hota hai kaunsa function call hoga.

Late binding — runtime pe decide hota hai (virtual functions ke through).

25. new Operator


Heap pe runtime mein memory allocate karta hai. Stack se alag — tab tak rehti hai jab tak delete nahi karo.
int* p = new int; // single int allocate
int* p = new int(42); // initialize bhi karo
int* arr = new int[10]; // array of 10 ints

delete p; // single free karo


delete[] arr; // array free karo

• Fail hone pe bad_alloc exception throw karta hai


• Modern C++ mein make_unique/make_shared prefer karo

26. Stream in C++


Data ka flow — program aur I/O device ke beech. C++ mein stream-based I/O hota hai.

Stream Type Kaam


cin Input Keyboard se padho

cout Output Screen pe likho

cerr Error Error output (unbuffered)

clog Log Error output (buffered)

ifstream File Input File se padho

ofstream File Output File mein likho

27. tellg() and tellp()


File stream mein current position batate hain.
// tellg() — get pointer ki position (input stream)
ifstream f("[Link]");
streampos pos = [Link]();
cout << "Read position: " << pos;

// tellp() — put pointer ki position (output stream)


ofstream out("[Link]");
streampos wp = [Link]();
cout << "Write position: " << wp;

// seekg/seekp se position change karo


[Link](0, ios::end); // end pe jao
streampos size = [Link](); // file size

28. width() and fill()


width(n) — next output ka minimum field width set karo:
[Link](10);
cout << "Hi"; // " Hi" (8 spaces + Hi)
[Link](10);
cout << 42; // " 42"

fill(ch) — empty space fill karne ka character set karo:


[Link]('*');
[Link](10);
cout << "Hi"; // "********Hi"

[Link]('-');
[Link](8);
cout << 99; // "------99"

29. Function Overriding


Derived class mein base class ka same function redefine karna. Inheritance mein hota hai.
class Animal {
public:
void sound() { cout << "Generic sound"; }
};

class Cat : public Animal {


public:
void sound() { cout << "Meow"; } // overriding — same signature
};

Cat c;
[Link](); // "Meow" — Cat ka version

Animal* a = new Cat();


a->sound(); // "Generic sound" — bina virtual ke base ka version
// virtual add karo to "Meow" milega

• Overloading se alag — same function naam AND same parameters

30. Default Arguments


Function call mein argument na diya jaye to automatically use hone wali value.
void greet(string name = "User", int times = 1) {
for (int i = 0; i < times; i++)
cout << "Hello " << name << "\n";
}

greet(); // Hello User (1 baar)


greet("Rahul"); // Hello Rahul (1 baar)
greet("Rahul", 3); // Hello Rahul (3 baar)

• Default arguments RIGHT side se define hone chahiye


• Declaration mein dete hain, definition mein nahi (generally)

31. Object Oriented Programming (OOP)


Programming paradigm jahan code objects ke around organize hota hai. C++ ek OOP language hai.

Pillar Matlab

Encapsulation Data aur functions ek saath band karo

Abstraction Sirf zaroori cheezein dikhao, baaki chupao

Inheritance Parent class ke properties reuse karo

Polymorphism Same interface, alag behavior


• C++ mein OOP kyun? — Code reusable, maintainable, real-world model aasaan

32. Applications of C++

Area Example

Operating Systems Windows ke parts, Linux kernel modules

Game Development Unreal Engine, game physics

Embedded Systems Robotics, microcontrollers

Compilers GCC, Clang

Databases MySQL, MongoDB

Browsers Chrome ka V8 engine


Finance High-frequency trading systems

Yahi hai poora C++ OOP ka exam-ready syllabus — 32 topics, 4-mark answers.

You might also like