UNIT 1
Q.Explain the need for Object Oriented Programming language.
Object-Oriented Programming (OOP) is a programming paradigm that organizes
software design around objects, which represent real-world entities with attributes (data)
and methods (functions) that define their behavior.
The need for OOP arises due to the following reasons:
1. Modularity and Reusability
o OOP allows breaking down complex problems into smaller, manageable
objects (modules), making the code easier to understand and maintain.
o Classes can be reused across different programs, reducing redundancy.
2. Data Encapsulation and Security
o OOP bundles data and methods within a single unit (class), preventing direct
access to data and ensuring better security.
3. Inheritance
o Enables new classes to inherit properties and behaviors from existing
classes, promoting reusability and reducing code duplication.
4. Polymorphism
o Allows the same function or method to work differently for different objects,
improving flexibility in programming.
5. Improved Maintainability
o Since OOP code is modular and well-structured, it is easier to update, debug,
and troubleshoot errors.
6. Scalability
o OOP allows easy modification and expansion of systems since the design is
based on small, self-contained objects.
These features make OOP an essential approach for developing complex and efficient
software systems.
Q.Explain Namespaces with the help of Programming example.
A namespace is a way to group identifiers (such as variables, functions, and
classes) to avoid name conflicts in large programs. It helps organize code and
makes it more manageable.
Example Without Namespace:
#include <iostream>
,int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Here, we use std::cout because cout belongs to the std (standard) namespace.
Example With Namespace:
#include <iostream>
using namespace std; // Now we don't need to use std::
int main() {
cout << "Hello, World!" << endl;
return 0;
}
By using using namespace std;, we can write cout instead of std::cout.
Conclusion:
Namespaces prevent naming conflicts and help in organizing code efficiently, especially
in large programs.
Q.Explain the concept of class & object in C++ with example.
Class:
A class is a blueprint or template for creating objects. It defines data members
(attributes) and methods (functions) but does not store actual data itself.
Object:
An object is an instance of a class. It holds actual data and interacts with other objects or
functions using methods. Each object has its own attributes (variables) and can perform
actions (functions).
#include <iostream>
#include <string>
using namespace std;
// Define a class named 'Person'
class Person {
public:
string name; // Data member (attribute)
int age;
// Method (function) inside the class
void introduce() {
cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl;
}
};
, int main() {
Person person1; // Create an object of the Person class
// Assign values to object attributes
person1.name = "John";
person1.age = 25;
// Call the method to introduce the person
person1.introduce();
return 0;
}
Output: Hi, my name is John and I am 25 years old.
Class Definition (Person)
o The Person class has two data members: name and age.
o It has a method introduce(), which prints a message.
Creating an Object (person1)
o The person1 object is created from the Person class.
o Attributes (name and age) are assigned values.
o The introduce() method is called to display the information.
Conclusion:
A class is like a blueprint, and an object is a real-world entity created from it.
Objects allow us to work with real data using the structure defined in a class.
Q.Explain following OOP features: a) Data Abstraction b) Data
Encapsulation 5. Explain following OOP features: a) Inheritance. d)
Polymorphism
a) Data Abstraction - Example: Student Report
Explanation:
A student sees only the final marks in the report card.The internal calculation (assignments,
exams, weightage) is hidden from the student.This is Abstraction—only important details
(total marks) are shown, while internal processing is hidden.
Example:
#include <iostream>
using namespace std;
class Student {
private:
int marks1 = 85, marks2 = 90, marks3 = 80; // Internal data (hidden)
int calculateTotal() { // Hidden method
return marks1 + marks2 + marks3;
}
public:
, void showTotalMarks() { // Only necessary details are shown
cout << "Total Marks: " << calculateTotal() << endl;
}
};
int main() {
Student s1;
s1.showTotalMarks(); // Student can see only total marks
return 0;
}
Output: Total marks- 225
b) Data Encapsulation - Example: Student Marks in a Class
Explanation:
A teacher records marks for each student in a system.
The marks are private and cannot be changed by students.
Students can only see their marks through a public method (e.g., view report).
This is Encapsulation—marks are protected, and only the teacher can modify them.
Example:
#include <iostream>
using namespace std;
class Student {
private:
int marks = 90; // Private data (can't be accessed directly)
public:
void showMarks() { // Public method to access marks
cout << "Your marks: " << marks << endl;
}
void updateMarks(int newMarks) { // Only authorized modification
if (newMarks >= 0 && newMarks <= 100) {
marks = newMarks;
cout << "Marks updated successfully!" << endl;
} else {
cout << "Invalid marks!" << endl;