Module 13: Object-Oriented Programming (OOP) in C++
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects and classes. It helps create modular, reusable, secure, and maintainable applications.
The four main principles of OOP are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
In this module, you'll learn the building blocks of OOP.
1. Class
A class is a blueprint for creating objects. It defines the data (attributes) and functions (methods) that objects will have.
Syntax
1class ClassName 2{ 3public: 4 // Data members 5 // Member functions 6};
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 string name; 8 int age; 9}; 10 11int main() 12{ 13 Student s1; 14 15 s1.name = "Ankit"; 16 s1.age = 22; 17 18 cout << s1.name << endl; 19 cout << s1.age; 20 21 return 0; 22}
Output
Ankit
22
2. Object
An object is an instance of a class. Objects occupy memory and use the properties and methods defined in the class.
Example
1Student student1; 2Student student2;
Each object has its own copy of the class's data members.
3. Members
A class contains two types of members:
Data Members
Store data.
1string name; 2int age;
Member Functions
Perform operations on the data.
1void display() 2{ 3 cout << name << " " << age; 4}
Complete Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 string name; 8 int age; 9 10 void display() 11 { 12 cout << name << " " << age; 13 } 14}; 15 16int main() 17{ 18 Student s; 19 20 s.name = "Ankit"; 21 s.age = 22; 22 23 s.display(); 24 25 return 0; 26}
4. Access Specifiers
Access specifiers control how class members are accessed.
| Access Specifier | Accessible Inside Class | Outside Class | Derived Class |
|---|---|---|---|
public | ✅ | ✅ | ✅ |
private | ✅ | ❌ | ❌ |
protected | ✅ | ❌ | ✅ |
Example
1class Student 2{ 3private: 4 int marks; 5 6public: 7 string name; 8};
5. Constructors
A constructor is a special member function that is automatically called when an object is created.
Characteristics:
- Same name as the class.
- No return type.
- Initializes object data.
Default Constructor
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 Student() 8 { 9 cout << "Constructor Called"; 10 } 11}; 12 13int main() 14{ 15 Student s; 16 17 return 0; 18}
Output
Constructor Called
Parameterized Constructor
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 string name; 8 9 Student(string n) 10 { 11 name = n; 12 } 13}; 14 15int main() 16{ 17 Student s("Ankit"); 18 19 cout << s.name; 20 21 return 0; 22}
Output
Ankit
6. Destructor
A destructor is automatically called when an object is destroyed.
Characteristics:
- Begins with
~ - No return type
- No parameters
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 Student() 8 { 9 cout << "Constructor\n"; 10 } 11 12 ~Student() 13 { 14 cout << "Destructor"; 15 } 16}; 17 18int main() 19{ 20 Student s; 21 22 return 0; 23}
Output
Constructor
Destructor
7. this Pointer
The this pointer refers to the current object.
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6private: 7 string name; 8 9public: 10 void setName(string name) 11 { 12 this->name = name; 13 } 14 15 void display() 16 { 17 cout << name; 18 } 19}; 20 21int main() 22{ 23 Student s; 24 25 s.setName("Ankit"); 26 27 s.display(); 28 29 return 0; 30}
Output
Ankit
8. Friend Function
A friend function can access the private and protected members of a class.
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6private: 7 int marks = 95; 8 9 friend void show(Student); 10}; 11 12void show(Student s) 13{ 14 cout << s.marks; 15} 16 17int main() 18{ 19 Student s; 20 21 show(s); 22 23 return 0; 24}
Output
95
9. Friend Class
A friend class can access all private and protected members of another class.
Example
1#include <iostream> 2using namespace std; 3 4class Student; 5 6class Teacher 7{ 8public: 9 void display(Student&); 10}; 11 12class Student 13{ 14private: 15 int marks = 90; 16 17 friend class Teacher; 18}; 19 20void Teacher::display(Student& s) 21{ 22 cout << s.marks; 23} 24 25int main() 26{ 27 Student s; 28 Teacher t; 29 30 t.display(s); 31 32 return 0; 33}
Output
90
10. Static Members
A static data member is shared by all objects of a class.
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 static int count; 8 9 Student() 10 { 11 count++; 12 } 13}; 14 15int Student::count = 0; 16 17int main() 18{ 19 Student s1; 20 Student s2; 21 Student s3; 22 23 cout << Student::count; 24 25 return 0; 26}
Output
3
11. Constant Members
A const member function guarantees that it will not modify the object's data members.
Example
1#include <iostream> 2using namespace std; 3 4class Student 5{ 6public: 7 void display() const 8 { 9 cout << "Constant Member Function"; 10 } 11}; 12 13int main() 14{ 15 Student s; 16 17 s.display(); 18 19 return 0; 20}
Output
Constant Member Function
Best Practices
- Keep data members
private. - Use
publicmember functions to access data. - Initialize objects using constructors.
- Free resources in destructors when managing dynamic memory.
- Use
thisonly when necessary. - Avoid excessive use of friend functions.
- Use
staticmembers for data shared across all objects. - Mark functions
constwhenever they do not modify object state.
Practice Problems
- Create a
Studentclass with name and age. - Implement default and parameterized constructors.
- Create a class with private data and public getter/setter methods.
- Demonstrate the use of the
thispointer. - Write a friend function to access private members.
- Create a friend class.
- Count the number of objects using a static member.
- Create a const member function.
Interview Questions
- What is a class in C++?
- What is an object?
- What are data members and member functions?
- What is the difference between
public,private, andprotected? - What is a constructor?
- What is a destructor?
- What is the purpose of the
thispointer? - What is a friend function?
- What is a friend class?
- What are static members?
- What is a const member function?
Summary
In this module, you learned:
- The fundamentals of Object-Oriented Programming in C++.
- How to create classes and objects.
- How access specifiers provide encapsulation.
- How constructors and destructors manage object initialization and cleanup.
- How the
thispointer refers to the current object. - How friend functions and friend classes access private members.
- How static members are shared across objects.
- How const member functions protect object state.
Next Module: Module 14: Inheritance — Single, Multiple, Multilevel, Hierarchical, and Hybrid Inheritance, Constructor Order, Method Overriding, and Access Control in Inheritance.