OOPS C++
OOPS C++
ROLL
2314104658
NUMBER
SEMESTER II
A. Function Overloading:
Function overloading in C++ allows the definition of multiple functions with the
same name in the same scope, differing in their parameter lists. This provides
flexibility in handling operations that share a conceptual purpose but operate on
different types or numbers of arguments.
Key Differences:
Scope:
Function Overloading: Functions share the same name but have different
parameter lists.
Function Overriding: The overridden function in the derived class has the same
signature as the base class function.
i. For Loop:
#include <iostream>
int main() {
for (int i = 0; i < 5; ++i) {
std::cout <<
}
return 0;
}
The for loop initializes, checks a condition, and updates a variable in a concise
structure.
#include <iostream>
int main() {
int i = 0;
while (i < 5) {
std::cout <<
++i;
}
retu
return 0;
}
The while loop repeats as long as a condition is true, providing flexibility in loop
control.
In Summary:
Both for and while loops are essential for iteration in C++, with for loops
offering a compact structure for a fixed number of iterations and while loops
providing flexibility for variable iterations based on a condition. Function
overloading and overriding enhance code expressiveness and reusability,
especially in object-oriented programming contexts. These concepts collectively
contribute to building robust and versatile C++ programs.
QUES:- 2:-
Method Overloading:
Method overloading is a form of compile-time polymorphism where multiple
functions in the same scope have the same name but different parameter lists. The
compiler determines which function to call based on the number or types of
arguments.
Example:
In this example, the print method is overloaded to handle both integers and doubles,
demonstrating compile-time polymorphism.
Method Overriding:
Method overriding is a form of runtime polymorphism where a base class method is redefined in
a derived class with the same signature. The actual function called is determined at runtime,
allowing flexibility in the implementation.
Example:
#include <iostream>
class Animal {
public:
virtual void sound() const {
std::cout << "Animal makes a sound" << std::endl;
}
};
int main() {
Animal* animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->sound();
animalPtr = &cat;
animalPtr->sound();
return 0;
}
In this example, the sound method in the base class Animal is overridden in the derived classes
Dog and Cat. The appropriate method is called based on the actual type of the object during
runtime.
QUES:- 3:-
ANS:- Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a
class (derived or child class) to inherit properties and behaviors from another class (base or
parent class). It facilitates code reusability, promotes the creation of a hierarchy of classes, and
enables the establishment of relationships between them.
Types of Inheritance:
i. Single Inheritance:
In single inheritance, a derived class inherits from only one base class.
Syntax: };
class Base1 {
// Base1 class members class Swimming {
}; public:
void swim() const {
class Base2 { std::cout << "Object
// Base2 class members swims" << std::endl;
}; }
};
class Derived : public Base1,
public Base2 { class FlyingFish : public
// Derived class members Flying, public Swimming {
}; // FlyingFish class members
Example: };
Syntax: };
class Base { Example:
// Base class members
}; #include <iostream>
v. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance (single,
multiple, multi-level, or hierarchical) within a program.
QUES:- 4:-
ANS:- 4:- In C++, the fstream library provides classes that allow us to work
with files. Opening a file using a constructor simplifies the file handling process
by associating the file stream with a file during object creation. The constructors
of classes like ofstream (output file stream) and ifstream (input file stream) can be
used to open a file at the moment the object is created.
Let's consider an example where we want to open a file for writing using the
ofstream class constructor. The program will create an object of the ofstream
class, and during the creation of this object, the constructor will open the
specified file.
#include <iostream>
#include <fstream>
int main() {
// File name
const char* fileName = "example.txt";
return 0;
}
In this example:
QUES:- 5:-
ANS:- 5 :- A. Class to Basic Type Conversion:
Example:
In this example, the Distance class has a conversion operator (operator float()
const) that allows an object of the Distance class to be implicitly converted to a
float. This enables using a Distance object in a context where a float is expected.
Example:
#include <iostream> }
};
class MyNumber {
private: int main() {
int value; MyNumber num(5);
MyNumber negNum = -
public: num;
MyNumber(int v) : value(v)
{} std::cout << "Original
number: " << num.getValue()
MyNumber operator-() << std::endl;
const { std::cout << "Negated
return MyNumber(- number: " <<
value); negNum.getValue() <<
} std::endl;
In this example, the unary minus operator (operator-() const) is overloaded for
the MyNumber class. When the unary minus is applied to a MyNumber object, a
new MyNumber object with the negated value is returned. The overloaded unary
minus operator allows users to apply the negation operation to objects of the
MyNumber class.
QUES:- 6:-
ANS:- 6;- A. File Mode:
File mode in C++ refers to the mode in which a file is opened, determining the
type of operations that can be performed on the file. When you open a file, you
specify a file mode to indicate whether you want to read, write, or both, and
whether the file should be created if it doesn't exist.
There are several file mode options available, and they are specified as a
combination of flags. The common file modes include:
These modes can be combined using the bitwise OR operator (|) to open files in
multiple modes. For
example:
#include <fstream>
int main() {
// Open a file for writing and append to the end
std::ofstream outFile("example.txt", std::ios::out | std::ios::app);
return 0;
}
Vector:
A std::vector is a dynamic array that can grow or shrink in size. It is part of the
C++ Standard Template Library (STL) and provides dynamic array-like features
along with additional functionalities. Vectors offer constant-time random access
to elements and efficient insertion and deletion at the end.
Example:
Deque:
A std::deque (double-ended queue) is a sequence container that allows fast
insertion and deletion at both ends. It provides constant-time random access and
is implemented as a dynamic array of fixed-size chunks. Deques are suitable for
scenarios where frequent insertion and deletion at both ends are required.
Example:
#include <iostream> myDeque.push_back(10);
#include <deque> myDeque.push_front(5);
Both std::vector and std::deque are versatile sequence containers with different
characteristics. The choice between them depends on the specific requirements of
the application, such as the type and frequency of operations performed on the
container.