Introduction to OOP in C++
Introduction
Object-Oriented Programming (OOP) organizes software as a collection of objects that combine data and behavior. C++ supports both procedural (POP) and object-oriented paradigms.
OOP focuses on objects rather than procedures, improving modularity, reusability, and maintainability.
The Four Pillars
Fig 1.1 β The four pillars of Object-Oriented Programming.
POP vs OOP
Procedural (POP)
- Emphasis on functions/procedures
- Top-down approach
- Global data sharing
- Less data hiding
Object-Oriented (OOP)
- Emphasis on objects
- Bottom-up approach
- Data hiding/encapsulation
- Reusability via inheritance
Definition
OOP: A paradigm that models software as interacting objects, each bundling data and the methods operating on it.
C++ Code Example
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
void set(string n, int a){ name=n; age=a; }
void show(){ cout<<name<<" "<<age<<endl; }
};
int main(){
Student s; s.set("Ravi",20); s.show();
return 0;
}Important Questions
Short Questions
- Define OOP.
- List the four pillars of OOP.
- Difference between POP and OOP?
- What is an object?
Long Questions
- Explain OOP features with examples.
- Compare procedural and object-oriented programming.
MCQ Practice
1. OOP stands for:
- Object-Oriented Programming
- Object-Only Programming
- Ordered Object Programming
- Open Object Programming
Answer: Object-Oriented Programming.
2. Which is NOT a pillar of OOP?
- Encapsulation
- Inheritance
- Polymorphism
- Compilation
Answer: Compilation.
3. POP follows which approach?
- Top-down
- Bottom-up
- Object-first
- None
Answer: top-down.
4. C++ was developed by:
- Bjarne Stroustrup
- Dennis Ritchie
- James Gosling
- Guido van Rossum
Answer: Bjarne Stroustrup.
5. An object is:
- An instance of a class
- A function
- A variable type
- A keyword
Answer: an instance of a class.
Quick Revision
- OOP = objects bundling data + behavior.
- Pillars: encapsulation, abstraction, inheritance, polymorphism.
- POP: top-down, functions; OOP: bottom-up, objects.
- C++ supports both paradigms.