C++ Assignment No1
C++ Assignment No1
Assignment No1
1. Define Object-Oriented Programming (OOP). What are its fundamental
principles, and how do they differ from procedural programming paradigms?
Definition:
Object-Oriented Programming (OOP) is a programming paradigm based on the
concept of "objects", which are instances of classes that encapsulate data
(attributes) and functions (methods) that operate on the data. OOP enables
modularity, reusability, and abstraction, making it easier to manage large-scale
software systems.
Fundamental Principles of OOP:
There are four key principles of OOP:
1. Encapsulation:
o Wrapping data (variables) and methods (functions) into a single
unit called a class.
o It restricts direct access to some components of the object,
preventing unintended modifications.
o Achieved using access specifiers (private, protected, public).
2. Abstraction:
o Hiding implementation details and exposing only essential
features.
o Achieved using abstract classes and interfaces (pure virtual
functions in C++).
o Example: A Car class exposes start() but hides how the engine
works.
3. Inheritance:
o Allows a class to derive properties and behaviors from another
class.
o Promotes code reusability and establishes a hierarchy.
o Example: A Dog class inherits from an Animal class.
4. Polymorphism:
o Allows objects to take multiple forms.
o Compile-time (Function Overloading, Operator Overloading) and
Runtime (Virtual Functions, Dynamic Binding).
o Example: A draw() method behaves differently in Circle and
Rectangle classes.
OOP vs. Procedural Programming
2. Discuss the advantages and applications of OOP. How does C++ facilitate
the implementation of OOP concepts?
Advantages of OOP:
1. Modularity:
o Classes and objects help organize code into modular components.
2. Code Reusability:
o Inheritance allows reusing existing classes, reducing redundancy.
3. Maintainability:
o Encapsulation ensures that changes in one part of the system do
not affect unrelated parts.
4. Scalability:
o Suitable for large-scale applications due to structured and modular
design.
5. Security:
o Data hiding prevents direct access to sensitive data.
Applications of OOP:
• Software Development: Used in designing enterprise applications, web
applications, and more.
• Game Development: Unreal Engine and Unity use OOP concepts
extensively.
• GUI Applications: C++ with Qt for GUI development.
• Real-Time Systems: Banking systems, traffic management, etc.
• Machine Learning & AI: TensorFlow (C++ backend).
How C++ Supports OOP?
• Classes & Objects for encapsulation.
• Access Specifiers (private, protected, public).
• Constructors & Destructors for initialization & cleanup.
• Inheritance & Polymorphism via virtual functions.
• Operator Overloading for custom behaviors.
3. Explain the concept of classes and objects in C++. How do they contribute
to data abstraction and encapsulation?
Classes:
A class is a blueprint for creating objects. It defines attributes (data members)
and methods (member functions).
class Car {
private:
string model;
int speed;
public:
void setModel(string m) { model = m; }
void setSpeed(int s) { speed = s; }
void display() { cout << "Model: " << model << ", Speed: " << speed << "
km/h" << endl; }
};
Objects:
An object is an instance of a class.
int main() {
Car myCar;
myCar.setModel("Tesla");
myCar.setSpeed(120);
myCar.display();
return 0;
}
How They Contribute to Abstraction & Encapsulation?
• Encapsulation: private members ensure data hiding.
• Abstraction: User interacts with setModel(), without knowing how data
is stored internally.
4. What are constructors and destructors in C++? Describe their purpose,
types, and the sequence of their invocation during object creation and
destruction.
Constructors:
A constructor is a special function that is automatically called when an object is
created.
Types of Constructors:
Sequence of Invocation:
1. Constructor is called when an object is created.
2. Destructor is called when the object is destroyed or goes out of scope.
5. Illustrate the concept of operator overloading in C++. Provide examples of
how to overload arithmetic and relational operators.
Operator overloading allows defining custom behaviors for operators when
used with user-defined classes.
Overloading Arithmetic Operators
Example: Overloading + for complex numbers.
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
void display() { cout << real << " + " << imag << "i" << endl; }
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.display(); // Output: 6 + 8i
return 0;
}
Overloading Relational Operators
Example: Overloading == for comparing two objects.
class Car {
public:
string model;
Car(string m) { model = m; }
int main() {
Car car1("Tesla"), car2("BMW");
if (car1 == car2)
cout << "Cars are the same" << endl;
else
cout << "Cars are different" << endl;
return 0;
}
Summary
• Operator Overloading allows customizing behaviors of operators.
• Commonly Overloaded Operators: +, -, *, /, ==, <, >, [], ().
Assignment No2
6. Define inheritance in C++. What are the different types of inheritance
supported by C++, and how does inheritance promote code reusability?
Definition
Inheritance is an OOP concept in which one class (derived/child class) acquires
the properties and behaviors of another class (base/parent class). It allows for
code reusability and hierarchical relationships between classes.
Types of Inheritance in C++
C++ supports five types of inheritance:
7. Explain polymorphism in C++. Differentiate between compile-time (static)
and run-time (dynamic) polymorphism with suitable examples.
Definition
Polymorphism means "many forms", allowing the same function or operator
to behave differently based on context.
Types of Polymorphism
(A) Compile-time (Static) Polymorphism
• Achieved using Function Overloading & Operator Overloading.
• Binding happens at compile-time.
Difference Between Compile-time & Run-time Polymorphism
Run-time
Feature Compile-time (Static)
(Dynamic)
Method
Not supported Supported
Overriding
8. Discuss the role of access specifiers (public, private, and protected) in C++.
How do they control access to class members and affect inheritance?
Access Specifiers
C++ has three access specifiers:
1. Private: Accessible only within the class.
2. Protected: Accessible within the class & derived classes.
3. Public: Accessible from anywhere.
Example:
class Parent {
private:
int privateVar;
protected:
int protectedVar;
public:
int publicVar;
};
Effect of Access Specifiers in Inheritance
int main() {
cout << add(5, 3) << endl; // Works for int
cout << add(5.5, 2.2) << endl; // Works for double
}
(B) Class Template
Instead of creating separate classes for int, double, etc., we use a class
template:
template <typename T>
class Box {
T value;
public:
Box(T v) { value = v; }
void show() { cout << "Value: " << value << endl; }
};
int main() {
Box<int> intBox(10);
intBox.show(); // Outputs: Value: 10
Box<double> doubleBox(5.5);
doubleBox.show(); // Outputs: Value: 5.5
}
Advantages of Templates
• Code Reusability: Works for any data type.
• Type Safety: Avoids unnecessary type conversions.
• Performance: No runtime overhead.