C++ and Object-Oriented Programming
Guide
1. Features of C++ and Advantages of OOP
Core Features of C++
• Mid-Level Language: Combines low-level (pointers) and high-level features.
• Object-Oriented: Supports Encapsulation, Abstraction, Inheritance, and Polymorphism.
• Rich Library Support: Includes the Standard Template Library (STL).
• Speed and Efficiency: Compiled language with high execution speed.
• Memory Management: Direct control using new and delete.
Advantages of OOP
• Modularity: Code is divided into independent, manageable objects.
• Data Hiding: Secure data representation via access specifiers.
• Reusability: Inheritance allows reusing existing code.
• Maintenance: Easier to debug and modify modular code.
2. Differentiate Between C and C++
Feature C C++
Paradigm Procedural Programming Object-Oriented Programming
Approach Top-down Bottom-up
Data Security Lower (Global data access) Higher (Data hiding/Encapsulation)
Overloading Not supported Supports Function & Operator overloading
Memory Mgmt malloc(), free() new, delete
3. Classes and Objects
A Class is a blueprint or user-defined data type. An Object is an instance of a class.
Page 1
Example:
class Car {
public:
string brand;
void drive() { cout << brand << " is driving."; }
};
int main() {
Car obj; // Object
[Link] = "Tesla";
[Link]();
}
4. Access Specifiers
• public: Members accessible from outside the class.
• private: Members accessible only within the class.
• protected: Members accessible within the class and its derived classes.
5. Constructors and Types
A constructor initializes objects. Types include:
1. Default: No parameters.
2. Parameterized: Takes arguments to initialize members.
3. Copy: Initializes an object using another object of the same class.
6. Destructors
A destructor (e.g., ~ClassName()) is called when an object is destroyed. It releases memory and
resources.
Constructor vs. Destructor
Constructor Destructor
Used to initialize objects. Used to destroy objects.
Can have parameters. Cannot have parameters.
Page 2
Can be overloaded. Cannot be overloaded.
7. Function Overloading
Multiple functions with the same name but different parameters.
void print(int i) { cout << i; }
void print(string s) { cout << s; }
8. Operator Overloading
Giving special meaning to an operator (like + or -) for user-defined classes.
9. Inheritance
• Single: One child from one parent.
• Multilevel: A child of a child (A -> B -> C).
• Multiple: One child from multiple parents (A, B -> C).
• Hierarchical: Multiple children from one parent.
• Hybrid: Combination of multiple types.
10. Polymorphism
• Compile-time: Resolved during compilation (Function overloading).
• Runtime: Resolved during execution (Virtual functions).
11. Virtual and Pure Virtual Functions
Virtual Function: Overridden in derived classes to achieve runtime polymorphism.
Pure Virtual Function: Has no body (= 0). Makes a class Abstract.
12. File Handling
Uses ifstream (read) and ofstream (write).
Page 3
Modes: ios::in, ios::out, ios::app (Append), ios::ate, ios::binary.
13. Short Notes
Inline Functions: Suggests compiler to replace function call with body to save time.
Default Arguments: Parameters that take a value if none is provided.
Static Members: Shared by all objects of a class; belongs to the class itself.
This Pointer: A pointer to the current object calling the member function.
Scope Resolution (::): Accesses global variables or defines class members outside the class.
Page 4