REVISED OOPM Lab Expt 1 To 11
REVISED OOPM Lab Expt 1 To 11
Name of Student:
Class/DIV: Semester:
Roll Number:
EXPT 1: Implement a program that prints prime numbers till a given number
Theory: In this program we are recalling C++ Control structures i.e. branching and looping
structures. Branching structures are if, if-else, decisions. Looping structures arewhile. Do-while
and for loop.
(Write answers of questions: 1) Explain the concept of OOPM. 2) Give syntax of C++ control
structures and loop structures.)
Flowchart / Algorithm:
Program:
Output:
Conclusion:
*********************************************************
EXPT 2: Implement a program to create a class and give values to TWO object attributes of that
class.
Experiment Number: 2
Implement a program to create a class and give values to TWO object attributes of that class.
Flowchart / Algorithm:
Program:
Output:
Conclusion:
*********************************************
EXPT 3: Implement a program to perform abstraction on certain class members. Write a method to
perform encapsulation to access the abstract class members
Date of Performance: Date of Submission:
Aim: Write a program that has the class of driver as private, and use the GET and SET method
of encapsulation to set the name, age and salary of the private class member
Flowchart / Algorithm:
Program:
Output:
Conclusion:
************************************
Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Name of Student: Roll Number :
Class/DIV : Academic Year :2023-2024
Aim: a. Write a program to add two complex numbers with operator overloading, the real and imaginary
part must be added separately and output must be of the form a + ib
1. Two operators = and & are already overloaded by default in C++. For example, to copy objects of the
same class, we can directly use the = operator. We do not need to create an operator function.
2. Operator overloading cannot change the precedence and associativity of operators. However, if we want
to change the order of evaluation, parentheses should be used.
3. There are 4 operators that cannot be overloaded in C++. They are:
:: (scope resolution)
. (member selection)
.* (member selection through pointer to function)
?: (ternary operator)
(Write answers of the questions:
How to overload increment/decrement operator?
What is C++ Binary Operator Overloading/ what is C++ Unary Operator Overloading)
Flowchart / Algorithm:
Program:
Output:
*********************************************
Experiment Number: 5
Aim: Write a program that has a parent class as vehicle with only driver name and liscence_no,
child class must be SUV, Sedan, Mini, each having attribute of cost and capacity
Theory: (Describe C++ Inheritance concept, Explain Multiple and Multilevel Inheritance, which
access specifier is used in inherited class)
Flowchart / Algorithm:
Program:
Output:
******************************************
Experiment Number: 6
Aim: Write a program that implements the carpool method polymorphism, the vehicle
class with capacity = 3 must have three arguments in carpool method, and vehicle class
with capacity = 4 must have 4 arguments.
Theory: (Describe C++ polymorphism concept, why and when to use Inheritance” and
“polymorphism)
Flowchart / Algorithm:
Program:
Output:
Experiment Number: 7
Aim: Write a program that implements the carpool method polymorphism, the vehicle
class with capacity = 3 must have three arguments in carpool method, and vehicle class
with capacity = 4 must have 4 arguments; the methods will only be in child classes.
Theory: (Describe )
Flowchart / Algorithm:
Program:
Output:
Experiment Number: 8
Aim: write a program to create a database of a grocery store with class grains, lentils
and vegetables, each class has attributes: cost, shelf life and amount in stock. Make 3
objects of each class and assign values to object attributes using constructors
Theory: (Describe what constructors are? Where can you define and declare it? What do
you know about constructor parameters? )
Flowchart / Algorithm:
Program:
Output:
**********************************************************
Experiment Number: 9
Aim: Write a program to implement the virtual base class of a parent class student
having two child class tests with marks attributes of 2 tests and sports score and both
child classes has a child class as result. The result class takes test marks average and
adds it with sports score to get the result.
Theory: (Describe runtime and compile time polymorphism, explain virtual base class
requirements and advantages.)
Flowchart / Algorithm:
Program:
Output:
Aim: Write a program to open a file ‘sample.txt’ and write lines in the file and save it.
Theory: C++ Exception Handling
An exception is a problem that arises during the execution of a program. A C++ exception is a
response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
• throw − A program throws an exception when a problem shows up. This is done using
a throw keyword.
• catch − A program catches an exception with an exception handler at the place in a
program where you want to handle the problem. The catch keyword indicates the
catching of an exception.
• try − A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
Assuming a block will raise an exception, a method catches an exception using a combination
of the try and catch keywords. A try/catch block is placed around the code that might generate
an exception. Code within a try/catch block is referred to as protected code, and the syntax for
using try/catch as follows −
try {
// protected code
} catch( ExceptionName e1 ) {
// catch block
} catch( ExceptionName e2 ) {
// catch block
} catch( ExceptionName eN ) {
// catch block
}
You can list down multiple catch statements to catch different type of exceptions in case
your try block raises more than one exception in different situations.
Catching Exceptions
The catch block following the try block catches any exception. You can specify what type of
exception you want to catch and this is determined by the exception declaration that appears in
parentheses following the keyword catch.
try {
// protected code
} catch( ExceptionName e ) {
// code to handle ExceptionName exception
}
Above code will catch an exception of ExceptionName type. If you want to specify that a catch
block should handle any type of exception that is thrown in a try block, you must put an ellipsis,
..., between the parentheses enclosing the exception declaration as follows −
try {
// protected code
} catch(...) {
// code to handle any exception
}
Live Demo
#include <iostream>
using namespace std;
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Because we are raising an exception of type const char*, so while catching this exception, we
have to use const char* in catch block. If we compile and run above code, this would produce
the following result −
Division by zero condition!
1 std::exception
An exception and parent class of all the standard C++ exceptions.
2 std::bad_alloc
This can be thrown by new.
3 std::bad_cast
This can be thrown by dynamic_cast.
4
std::bad_exception
This is useful device to handle unexpected exceptions in a C++ program.
5 std::bad_typeid
This can be thrown by typeid.
6 std::logic_error
An exception that theoretically can be detected by reading the code.
7 std::domain_error
This is an exception thrown when a mathematically invalid domain is used.
8 std::invalid_argument
This is thrown due to invalid arguments.
9 std::length_error
This is thrown when a too big std::string is created.
11 std::runtime_error
An exception that theoretically cannot be detected by reading the code.
12 std::overflow_error
This is thrown if a mathematical overflow occurs.
13 std::range_error
This is occurred when you try to store a value which is out of range.
14 std::underflow_error
This is thrown if a mathematical underflow occurs.
C++ provides a list of standard exceptions defined in <exception> which we can use in our
programs. These are arranged in a parent-child class hierarchy shown below −
Here is the small description of each exception mentioned in the above hierarchy −
Flowchart / Algorithm:
Program:
Output: