OOPs CPP Detailed Notes
OOPs CPP Detailed Notes
1. Introduction to OOP
- OOP (Object Oriented Programming): A style where we model real-world objects.
- Key idea: Data + Functions = Object.
- Benefits: Code reuse, better security (data hiding), easy maintenance, easy to expand.
2. Features of OOP
- Data Hiding: Private data can't be accessed directly from outside class.
Example:
class Demo { private: int x; };
4. Inheritance
Types:
- Single Inheritance: One base to One derived.
- Multiple: 2 or more base to One derived.
- Multilevel: A to B to C
- Hierarchical: A to B and A to C
- Hybrid: Mix of types
Example:
class A { };
class B: public A { };
class C: public A, public B { };
- Ambiguity: Same member in multiple bases. Resolved using Virtual base class.
5. Polymorphism
- Compile Time:
Function Overloading (Same name, diff args)
Operator Overloading (Change operator meaning)
- Run Time:
Virtual Functions
6. Templates
- Template = Generic code
- Same function/class for different data types.
Function Template:
template <class T>
T add(T a, T b) { return a + b; }
Class Template:
template <class T>
class Demo { T x; };
7. Exception Handling
- Handle run-time errors without crashing.
Keywords:
- try { code } -> Risky code
- catch { } -> Handle error
- throw -> Raise exception
Advanced:
- Rethrow: throw;
- Restrict exception: void func() throw(int);
- terminate(), unexpected() handle uncaught exceptions
9. Streams in C++
- cin: Input
- cout: Output
- cerr: Error (unbuffered)
- clog: Log (buffered)
Formatted I/O:
cout.width(), cout.precision()
Flags:
- left, right
- scientific
- fixed
- hex, oct