Object-Oriented Programming (OOP) is one of the most important programming paradigms in C++. It helps developers write code that is modular, reusable, scalable, and easier to maintain. C++ Object-Oriented Programming practice questions with solutions help to build concepts.
Instead of focusing only on functions, OOP organizes programs around objects, which contain both data (variables) and behavior (functions).
The four pillars of Object-Oriented Programming are:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
The basic building blocks of OOP are:
- Class
- Object
- Data Members
- Member Functions
Class
A class is a blueprint used to create objects.
Example:
class Student
{
public:
int rollNumber;
string name;
};
Object
An object is an instance of a class.
Example:
Student student1;
Objects occupy memory and can access the data members and member functions of their class.
Object-Oriented Programming is widely used in:
- Banking Systems
- Hospital Management Software
- Inventory Applications
- Game Development
- Mobile Applications
- Desktop Applications
- Enterprise Software
In this chapter, you’ll solve beginner-friendly OOP practice questions with complete explanations.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Input
- Sample Output
- Explanation
- Concepts Covered
Let’s begin.
1. C++ Program to Create a Class and Object
Problem Statement
Write a C++ program to create a class named Student and create its object.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
};
int main()
{
Student student;
student.rollNumber = 101;
student.name = "Rahul";
cout << "Roll Number: "
<< student.rollNumber << endl;
cout << "Name: "
<< student.name;
return 0;
}
Sample Output
Roll Number: 101
Name: Rahul
Explanation
The class acts as a blueprint, while the object stores actual data.
Concepts Covered
- Class
- Object
- Data Members
2. C++ Program to Accept Student Details Using Class
Problem Statement
Write a C++ program to accept and display student details using a class.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
float marks;
};
int main()
{
Student student;
cout << "Enter Roll Number: ";
cin >> student.rollNumber;
cin.ignore();
cout << "Enter Name: ";
getline(cin, student.name);
cout << "Enter Marks: ";
cin >> student.marks;
cout << "\nStudent Details\n";
cout << "Roll Number: "
<< student.rollNumber << endl;
cout << "Name: "
<< student.name << endl;
cout << "Marks: "
<< student.marks;
return 0;
}
Sample Input
Enter Roll Number: 102
Enter Name: Amit Kumar
Enter Marks: 91.5
Sample Output
Student Details
Roll Number: 102
Name: Amit Kumar
Marks: 91.5
Explanation
The object stores all student-related information inside one variable.
Concepts Covered
- Class
- Object
- User Input
3. C++ Program to Create Member Functions
Problem Statement
Write a C++ program to demonstrate member functions inside a class.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
string name;
void display()
{
cout << "Student Name = "
<< name;
}
};
int main()
{
Student student;
student.name = "Priya";
student.display();
return 0;
}
Sample Output
Student Name = Priya
Explanation
A member function belongs to a class and can directly access its data members.
Concepts Covered
- Member Function
- Object
- Function Call
4. C++ Program to Calculate Employee Salary Using Class
Problem Statement
Write a C++ program to calculate the total salary of an employee using a class.
C++ Solution
#include <iostream>
using namespace std;
class Employee
{
public:
float basicSalary;
float hra;
float da;
float totalSalary()
{
return basicSalary + hra + da;
}
};
int main()
{
Employee employee;
employee.basicSalary = 30000;
employee.hra = 5000;
employee.da = 4000;
cout << "Total Salary = "
<< employee.totalSalary();
return 0;
}
Sample Output
Total Salary = 39000
Explanation
The member function performs salary calculation using the object’s data members.
Concepts Covered
- Class
- Member Function
- Return Value
5. C++ Program to Create Multiple Objects of a Class
Problem Statement
Write a C++ program to create multiple objects of a class.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
};
int main()
{
Student student1;
Student student2;
student1.rollNumber = 101;
student1.name = "Rahul";
student2.rollNumber = 102;
student2.name = "Priya";
cout << "Student 1\n";
cout << student1.rollNumber
<< " "
<< student1.name << endl;
cout << "\nStudent 2\n";
cout << student2.rollNumber
<< " "
<< student2.name;
return 0;
}
Sample Output
Student 1
101 Rahul
Student 2
102 Priya
Explanation
A single class can create multiple independent objects.
Concepts Covered
- Multiple Objects
- Class
- Object Creation
6. C++ Program to Demonstrate Public and Private Access Specifiers
Problem Statement
Write a C++ program to demonstrate the use of public and private access specifiers.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
private:
int rollNumber;
public:
void setRollNumber(int number)
{
rollNumber = number;
}
void display()
{
cout << "Roll Number = "
<< rollNumber;
}
};
int main()
{
Student student;
student.setRollNumber(101);
student.display();
return 0;
}
Sample Output
Roll Number = 101
Explanation
Private members cannot be accessed directly from outside the class. Public member functions provide controlled access.
Concepts Covered
- Public
- Private
- Data Hiding
7. C++ Program to Demonstrate Getter and Setter Functions
Problem Statement
Write a C++ program to demonstrate getter and setter methods.
C++ Solution
#include <iostream>
using namespace std;
class Employee
{
private:
float salary;
public:
void setSalary(float amount)
{
salary = amount;
}
float getSalary()
{
return salary;
}
};
int main()
{
Employee employee;
employee.setSalary(45000);
cout << "Salary = "
<< employee.getSalary();
return 0;
}
Sample Output
Salary = 45000
Explanation
Setter functions modify private data, while getter functions return private data.
Concepts Covered
- Getter Function
- Setter Function
- Encapsulation
8. C++ Program to Store Product Details Using a Class
Problem Statement
Write a C++ program to store and display product information using a class.
C++ Solution
#include <iostream>
using namespace std;
class Product
{
public:
int productId;
string productName;
float price;
void display()
{
cout << "\nProduct Details\n";
cout << "Product ID: " << productId << endl;
cout << "Product Name: " << productName << endl;
cout << "Price: " << price;
}
};
int main()
{
Product product;
cout << "Enter Product ID: ";
cin >> product.productId;
cin.ignore();
cout << "Enter Product Name: ";
getline(cin, product.productName);
cout << "Enter Price: ";
cin >> product.price;
product.display();
return 0;
}
Sample Input
Enter Product ID: 501
Enter Product Name: Laptop
Enter Price: 65000
Sample Output
Product Details
Product ID: 501
Product Name: Laptop
Price: 65000
Explanation
A class groups product information and behavior into a single reusable unit.
Concepts Covered
- Class
- Object
- Member Function
9. C++ Program to Store Multiple Objects in an Array
Problem Statement
Write a C++ program to store multiple student records using an array of objects.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
};
int main()
{
Student students[3];
for (int i = 0; i < 3; i++)
{
cout << "Enter Roll Number: ";
cin >> students[i].rollNumber;
cout << "Enter Name: ";
cin >> students[i].name;
}
cout << "\nStudent Records\n";
for (int i = 0; i < 3; i++)
{
cout << students[i].rollNumber
<< " "
<< students[i].name << endl;
}
return 0;
}
Sample Input
101 Rahul
102 Amit
103 Priya
Sample Output
Student Records
101 Rahul
102 Amit
103 Priya
Explanation
An array of objects stores multiple instances of the same class efficiently.
Concepts Covered
- Array of Objects
- Loops
- Object Management
10. C++ Program to Compare Two Objects
Problem Statement
Write a C++ program to compare the marks of two students using objects.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
string name;
float marks;
};
int main()
{
Student student1, student2;
student1.name = "Rahul";
student1.marks = 88;
student2.name = "Priya";
student2.marks = 94;
if (student1.marks > student2.marks)
{
cout << student1.name
<< " scored higher marks.";
}
else
{
cout << student2.name
<< " scored higher marks.";
}
return 0;
}
Sample Output
Priya scored higher marks.
Explanation
Two separate objects are compared based on one of their data members.
Concepts Covered
- Multiple Objects
- Comparison
- Decision Making
11. C++ Program to Count the Number of Objects Created
Problem Statement
Write a C++ program to count the total number of objects created using a static data member.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
static int count;
Student()
{
count++;
}
};
int Student::count = 0;
int main()
{
Student student1;
Student student2;
Student student3;
cout << "Total Objects = "
<< Student::count;
return 0;
}
Sample Output
Total Objects = 3
Explanation
The static data member belongs to the class rather than individual objects. Every time an object is created, the constructor increments the count.
Concepts Covered
- Static Data Members
- Constructor
- Object Counting
12. C++ Program to Demonstrate Encapsulation
Problem Statement
Write a C++ program to demonstrate encapsulation using private data members and public member functions.
C++ Solution
#include <iostream>
using namespace std;
class BankAccount
{
private:
double balance;
public:
void setBalance(double amount)
{
balance = amount;
}
void displayBalance()
{
cout << "Account Balance = "
<< balance;
}
};
int main()
{
BankAccount account;
account.setBalance(25000);
account.displayBalance();
return 0;
}
Sample Output
Account Balance = 25000
Explanation
The balance variable is hidden from outside the class. It can only be modified using public member functions.
Concepts Covered
- Encapsulation
- Data Hiding
- Private Members
13. C++ Program to Create a Simple Calculator Using a Class
Problem Statement
Write a C++ program to create a simple calculator using class member functions.
C++ Solution
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int multiply(int a, int b)
{
return a * b;
}
};
int main()
{
Calculator calculator;
cout << "Addition = "
<< calculator.add(10, 5) << endl;
cout << "Subtraction = "
<< calculator.subtract(10, 5) << endl;
cout << "Multiplication = "
<< calculator.multiply(10, 5);
return 0;
}
Sample Output
Addition = 15
Subtraction = 5
Multiplication = 50
Explanation
The calculator class groups related mathematical operations into reusable member functions.
Concepts Covered
- Class
- Member Functions
- Object-Oriented Design
14. C++ Program to Demonstrate the this Pointer
Problem Statement
Write a C++ program to demonstrate the use of the this pointer.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
private:
int rollNumber;
public:
void setRollNumber(int rollNumber)
{
this->rollNumber = rollNumber;
}
void display()
{
cout << "Roll Number = "
<< rollNumber;
}
};
int main()
{
Student student;
student.setRollNumber(101);
student.display();
return 0;
}
Sample Output
Roll Number = 101
Explanation
The this pointer refers to the current object. It helps distinguish between local variables and class data members with the same name.
Concepts Covered
thisPointer- Current Object
- Member Functions
15. C++ Program to Demonstrate Basic OOP Concepts
Problem Statement
Write a C++ program that combines a class, object, data members, and member functions.
C++ Solution
#include <iostream>
using namespace std;
class Rectangle
{
public:
int length;
int width;
int area()
{
return length * width;
}
};
int main()
{
Rectangle rectangle;
rectangle.length = 8;
rectangle.width = 5;
cout << "Area = "
<< rectangle.area();
return 0;
}
Sample Output
Area = 40
Explanation
This example combines the basic building blocks of Object-Oriented Programming by creating a class, an object, data members, and a member function.
Concepts Covered
- Class
- Object
- Member Function
- OOP Fundamentals
Chapter Summary
In this chapter, you learned the fundamentals of Object-Oriented Programming (OOP) in C++. You explored how classes and objects help organize code into reusable components. You also practiced creating member functions, using access specifiers, implementing encapsulation with getter and setter methods, managing multiple objects, working with arrays of objects, using static data members, understanding the this pointer, and building simple class-based applications. These concepts form the foundation for advanced OOP topics such as constructors, inheritance, polymorphism, and abstraction.
Key Takeaways
- A class is a blueprint for creating objects.
- An object is an instance of a class.
- Member functions define the behavior of objects.
- Public members are accessible outside the class, while private members are hidden.
- Encapsulation protects data using private members and public methods.
- Arrays of objects allow efficient storage of multiple records.
- Static data members are shared among all objects of a class.
- The
thispointer refers to the current object. - OOP improves code reusability, readability, and maintainability.
- Mastering classes and objects is essential before learning constructors, inheritance, and polymorphism.
Frequently Asked Questions (FAQs)
1. What is Object-Oriented Programming (OOP) in C++?
Object-Oriented Programming is a programming paradigm that organizes code using classes and objects, making programs more modular and reusable.
2. What is the difference between a class and an object?
A class is a blueprint, while an object is an actual instance created from that blueprint.
3. What are the four pillars of OOP?
The four pillars of OOP are:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
4. What is encapsulation?
Encapsulation is the process of hiding data inside a class and allowing controlled access through public member functions.
5. Why are private data members used?
Private data members protect sensitive information and prevent direct modification from outside the class.
6. What is a static data member?
A static data member belongs to the class itself and is shared by all objects of that class.
7. What is the purpose of the this pointer?
The this pointer refers to the current object and is commonly used to resolve naming conflicts between member variables and function parameters.
8. Why is OOP important in software development?
OOP helps developers create scalable, reusable, maintainable, and organized applications, making it the preferred programming approach for modern software development.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
