OBJECT ORIENTED PROGRAMMING IN C++
OBJECT ORIENTED PROGRAMMING IN C++
MODULE 1:
Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design software.
Objects can represent real-world entities and encapsulate data and behaviors (methods). OOP promotes
concepts such as encapsulation, inheritance, polymorphism, and abstraction.
Structure of a C++ Program: A typical C++ program consists of the following components:
#include <iostream>
class MyClass {
public:
void display() {
};
int main() {
MyClass obj;
obj.display();
return 0;
• Encapsulation: Bundling data and methods that operate on the data within a single unit (class).
• Inheritance: Mechanism to create new classes from existing classes, enabling code reuse.
• Polymorphism: Ability to process objects differently based on their data type or class.
• Abstraction: Hiding complex implementation details and exposing only the necessary parts of an
object.
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
};
int main() {
myCar.brand = "Toyota";
myCar.year = 2020;
return 0;
A friend function is a function that is not a member of a class but has access to its private and protected
members. It is declared inside the class with the friend keyword.
Example:
class Box {
private:
int length;
public:
};
void printLength(Box b) {
cout << "Length: " << b.length << endl; // Accessing private member
int main() {
Box box(10);
return 0;
5. What are Constructors and Destructors? Explain the Scope Resolution Operator with an Example.
Constructors are special member functions called when an object is created, used to initialize the object.
Destructors are called when an object is destroyed, used for cleanup.
The scope resolution operator (::) is used to define or access members of a class outside its body.
Example:
class Example {
public:
Example() { // Constructor
~Example() { // Destructor
};
int main() {
Objects can be passed to functions by value or by reference. Passing by value creates a copy, while
passing by reference allows the function to modify the original object.
Example:
class Rectangle {
public:
};
// Pass by value
// Pass by reference
rect.width *= 2;
rect.height *= 2;
int main() {
Rectangle rect(3, 4);
printArea(rect); // Area: 12
scale(rect);
printArea(rect); // Area: 24
return 0;
Parameterized constructors allow passing parameters to initialize object attributes at the time of
creation.
Example:
class Circle {
public:
double radius;
double area() {
};
int main() {
cout << "Area: " << circle.area() << endl; // Output: Area: 78.5
return 0;
#include <iostream>
int a, b, c;
cout << "The largest number is: " << largest << endl;
return 0;
#include <iostream>
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
};
int main() {
Student student;
student.name = "Alice";
student.age = 20;
return 0;
MODULE 2:
An array of pointers to objects is a collection where each element is a pointer pointing to an object of a
class. This allows you to manage multiple objects dynamically.
Example:
#include <iostream>
class Student {
public:
string name;
Student(string n) : name(n) {}
void display() {
};
int main() {
string name;
cout << "Enter name for student " << i + 1 << ": ";
students[i]->display();
return 0;
You can access member functions of a class using pointers to the object. The -> operator is used for this
purpose.
Example:
#include <iostream>
class Rectangle {
public:
int area() {
}
};
int main() {
cout << "Area: " << rect->area() << endl; // Accessing member function
return 0;
Pointers to class members allow you to point to a member variable or member function of a class.
Example:
#include <iostream>
class MyClass {
public:
void display() {
};
int main() {
MyClass obj;
return 0;
Example:
#include <iostream>
class MyClass {
public:
int value;
MyClass(int v) : value(v) {}
void show() {
cout << "Value: " << this->value << endl; // Using this pointer
};
int main() {
MyClass obj(10);
return 0;
Upcasting is the process of converting a pointer or reference of a derived class to a pointer or reference
of a base class. It is safe and allows you to treat derived class objects as base class objects.
Example:
#include <iostream>
public:
void show() {
};
public:
void show() {
};
int main() {
Derived d;
return 0;
#include <iostream>
class Math {
public:
return a + b;
}
double add(double a, double b) {
return a + b;
};
int main() {
Math math;
cout << "Integer addition: " << math.add(5, 3) << endl; // Output: 8
cout << "Double addition: " << math.add(5.5, 3.3) << endl; // Output: 8.8
return 0;
A user-defined copy constructor is defined to initialize an object using another object of the same class.
Example:
#include <iostream>
class MyClass {
public:
int value;
// Parameterized constructor
MyClass(int v) : value(v) {}
value = obj.value;
}
void display() {
};
int main() {
return 0;
Ambiguity in function overloading occurs when the compiler cannot determine which function to call.
Example:
#include <iostream>
class Ambiguity {
public:
void show(int a) {
void show(double a) {
};
int main() {
Ambiguity obj;
// Ambiguous call
return 0;
8. Develop a C++ Program Using Operator Overloading for Overloading Unary Minus Operator
#include <iostream>
class Number {
public:
int value;
Number(int v) : value(v) {}
Number operator-() {
return Number(-value);
void display() {
};
int main() {
Number num(5);
return 0;