OOP Micro (ONS) For Scoring
OOP Micro (ONS) For Scoring
Operator overloading is a feature of C++ that allows us to redefine the way operators work
with user-defined data types such as classes or objects. Normally, operators like +, -, *, ==,
etc., work only with built-in types like int or float. Operator overloading lets these operators
work with objects by giving them new meanings
A friend function is a function that is not a member of a class but can access its private and
protected members. It is declared inside the class using the keyword friend.
Inheritance Type Base Class Public Base Class Protected Base Class Private
Members Members Members
Public Public Protected Not accessible
Inheritance
Protected Protected Protected Not accessible
Inheritance
Private Private Private Not accessible
Inheritance
#include<iostream> public:
using namespace std; void display(){
class Base1{ Base1::show();
public: Base2::show();
void show(){cout<<"Base1 show"<<endl;} }
}; };
class Base2{ int main(){
public: Derived d;
void show(){cout<<"Base2 show"<<endl;} d.display();
}; return 0;}
class Derived:public Base1,public Base2{
U5 Q. Explain Namespace in C++ with example.
Definition: Namespace in C++ groups variables, functions, classes, etc., under a unique
name to avoid naming conflicts.
Namespace in C++ is a way to group related variables, functions, and classes under a
unique name to prevent naming conflicts in large programs. It allows different parts of a
program or different libraries to use the same names without confusion. Namespaces help
organize code better and avoid errors caused by duplicate names. You access elements
inside a namespace using the scope resolution operator ::.
Advantages: Prevents naming conflicts, organizes code, useful in large projects.
Types: 1. Named 2. Unnamed 3. Nested
Example:
#include<iostream>
using namespace std;
namespace MySpace {
int x = 10;
void show() { cout << "Value: " << x << endl; }
}
int main() {
MySpace::show();
return 0;
}
Output: Value: 10
Exception handling is used to manage errors that occur during program execution without
crashing the program. It uses try block to write the code that may cause an error, throw to
raise an exception, and catch to handle that exception. When an error occurs inside try,
control transfers to catch to handle it smoothly.
#include<iostream> };
using namespace std; int main(){
template<class T1,class T2> Pair<int,float> p1(10,15.5);
class Pair{ Pair<string,int> p2("Age",25);
T1 first; T2 second; p1.display();
public: p2.display();
Pair(T1 a,T2 b){ first=a; second=b; return 0;
}
}
void display(){ cout<<"First:
"<<first<<", Second:
"<<second<<endl; }
--output= First: 10, Second: 15.5 First: Age, Second: 25
Q: What is a user-defined exception? Write down the scenario where we require user-
defined exceptions.
A stream in C++ is a flow of data used to perform input and output operations. It connects
the program with devices like keyboard, screen, and files. Streams help in reading data into
the program and writing data out of the program. Types of
streams in C++:
1. Input Stream: Reads data into the program. Example: cin reads from keyboard.
2. Output Stream: Sends data out of the program. Example: cout displays on screen.
3. File Stream: Used to read from and write to files.
o ifstream: Input file stream for reading files.
o ofstream: Output file stream for writing files.
o fstream: For both reading and writing files.
U6 Q. Write a program using the open(), eof(), and getline() member functions to open and
read a file content line by line.
In C++, we can read a file line by line using the ifstream class with the member functions
open(), eof(), and getline(). The open() function is used to open the file, eof() checks whether
the end of the file is reached, and getline() reads one line at a time from the file.
In file handling, errors can occur while opening, reading, or writing files. To avoid program
crashes and unexpected behavior, error handling is important.
File Mode:
File mode defines how a file is opened and accessed in C++. It specifies the purpose of file
operation such as reading, writing, appending, or binary mode. File modes determine
whether the file is opened for input, output, or both, and how the data is handled.
1. ios::in
Opens the file for reading (input). The file must exist. Example:
Q Difference between Opening a File with Constructor and open() Function in C++