[go: up one dir, main page]

0% found this document useful (0 votes)
26 views29 pages

REVISED OOPM Lab Expt 1 To 11

The document contains instructions for students to complete 5 programming experiments related to object-oriented programming concepts in C++ and Java. The experiments cover implementing classes and objects, operator overloading, inheritance, abstraction and encapsulation. For each experiment, students are asked to write the code, show the output, and answer related conceptual questions. The document provides guidelines for teachers to evaluate students' work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views29 pages

REVISED OOPM Lab Expt 1 To 11

The document contains instructions for students to complete 5 programming experiments related to object-oriented programming concepts in C++ and Java. The experiments cover implementing classes and objects, operator overloading, inheritance, abstraction and encapsulation. For each experiment, students are asked to write the code, show the output, and answer related conceptual questions. The document provides guidelines for teachers to evaluate students' work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Mahavir Education Trust's

SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE


Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Name of Student:

Class/DIV: Semester:

Roll Number:

Academic Year: 2023-2024

Subject: Skill base Lab – OOPM: (C++ and Java)

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

EXPT 1: Implement a program that prints prime numbers till a given number

Date of Performance: Date of Submission:

Program Execution Documentation Timely Viva Experiment Teacher


(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

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
Experiment Number: 1
Aim: Implement a program that prints prime numbers till a given number and store all prime numbers
in an array.

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:

*********************************************************

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

EXPT 2: Implement a program to create a class and give values to TWO object attributes of that
class.

Date of Performance: Date of Submission:

Program Execution Documentation Timely Viva Experiment Teacher


(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

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

Experiment Number: 2
Implement a program to create a class and give values to TWO object attributes of that class.

Aim: Write a program to make two classes: Driver and Vehicle


Class driver must have attributes: Name, Age, Salary,
Class Vehicle must have attributes: Model, Plate_No, Capacity, and Driver Name.
Make two objects of each class

Theory: C++ has the following conditional statements:


● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed, if the same condition is false
(Write answers of questions:
1) Explain with example what are Classes and Objects in C++?
2) Object-oriented programming has several advantages over procedural programming: Comment.
)

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 :2022 2023

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:

Program Documentation Timely Viva Experiment Teacher


Execution Submission Answer Marks Signature
(02)
(04)
(02) (02) (10)

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
Experiment Number: 3
Implement a program to perform abstraction on certain class members. Write a method to
perform encapsulation to access the abstract class members

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

Theory: (Write answers of the questions:


Explain
1) Class methods 2) Data Abstraction in C++and C++ Access Specifiers
3) C++ Encapsulation and its importance

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

EXPT 4: Implement a program to perform operator overloading on unary and Binary


operators

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

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
Experiment Number: 4

Implement a program to perform operator overloading on unary and Binary operators

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

b. Write a program to apply distance negation by overloading the ‘-’ operator


Theory: Things to Remember in C++ Operator Overloading

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:

Conclusion: This C++ Program is used to

*********************************************

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

EXPT 5: Implement a program that implements inheritance

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

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

Experiment Number: 5

Implement a program that implements inheritance

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:

Conclusion: This C++ practical is used to

******************************************

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

EXPT 6: Implement a program to perform polymorphism for methods in the


same class.

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Experiment Number: 6

Implement a program to perform polymorphism for methods in same


class.

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:

Conclusion: This C++ practical is used to

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
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

EXPT 7: Implement a program to perform polymorphism or methods in child class


and parent class.

Date of Performance: Date of Submission:


Program Documentation Timely Viva Experiment Teacher
Execution Submission Answer Marks Signature
(02)
(04)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Experiment Number: 7

Implement a program to perform polymorphism for


methods in child class and parent class.

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:

Conclusion: This C++ practical is used to

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
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

EXPT 8: Implement a program that assigns value to objects using constructors.

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Experiment Number: 8

Implement a program that assigns value to objects using constructors.

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:

Conclusion: This C++ practical is used to

**********************************************************

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
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
EXPT 9: Implement a program to execute runtime and compile time
polymorphism using virtual base class

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Experiment Number: 9

Implement a program to execute runtime and


compile time polymorphism using virtual base class

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:

Conclusion: This C++ practical is used to

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
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

EXPT 10: Implement a program that implements file handling.


(Beyond syllabus)

Date of Performance: Date of Submission:


Program Execution Documentation Timely Viva Experiment Teacher
(04) Submission Answer Marks Signature
(02)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment Number: 10
Implement a program that implements Exceptions in C++
(Beyond syllabus)

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.

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Throwing Exceptions
Exceptions can be thrown anywhere within a code block using throw statement. The operand
of the throw statement determines a type for the exception and can be any expression and the
type of the result of the expression determines the type of exception thrown.

Following is an example of throwing an exception when dividing by zero condition occurs −


double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}

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
}

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
The following is an example, which throws a division by zero exception and we catch it in catch
block.

Live Demo
#include <iostream>
using namespace std;

double division(int a, int b) {


if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}

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!

C++ Standard Exceptions:

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science

Sr.No C++ Standard Exceptions & Description

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.

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
10 std::out_of_range
This can be thrown by the 'at' method, for example a std::vector and
std::bitset<>::operator[]().

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 −

Define New Exceptions


Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024
P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
You can define your own exceptions by inheriting and overriding exception class functionality.
Following is the example, which shows how you can use std::exception class to implement your
own exception in standard way −
Live Demo
#include <iostream>
#include <exception>
using namespace std;

struct MyException : public exception {


const char * what () const throw () {
return "C++ Exception";
}
};
int main() {
try {
throw MyException();
} catch(MyException& e) {
std::cout << "MyException caught" << std::endl;
std::cout << e.what() << std::endl;
} catch(std::exception& e) {
//Other errors
}
}

This would produce the following result −


Output:
MyException caught
C++ Exception

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Here, ‘what ( )’ is a public method provided by exception class and it has been overridden by
all the child exception classes. This returns the cause of an exception.

Conclusion: This C++ practical is used to ….

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
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

EXPT 11: Implement friend function in a program (subject GAP)

Date of Performance: Date of Submission:


Program Documentation Timely Viva Experiment Teacher
Executio Submission Answer Marks Signature
(02)
n (04)
(02) (02) (10)

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics and Computer Science
Experiment Number: 11

Implement friend function in a


program (subject GAP)
Aim: Write a program to access the private class driver using a friend function and
put details of driver name, age and salary.
Theory: (What is friend and inline function in C++? )

Flowchart / Algorithm:

Program:

Output:

Conclusion: This C++ practical is used to

Skill base Lab – OOPM: (C++ and Java), SEM 3, 2023-2024


P

You might also like