0% found this document useful (0 votes)
22 views81 pages

OOP Manual Update

The document is a practical manual for the Object Oriented Programming using C++ course at the KIT-Kalaignarkarunanidhi Institute of Technology. It includes instructions for laboratory classes, a list of experiments, program outcomes, course outcomes, and educational objectives. The manual outlines various C++ programming tasks and concepts that students are expected to learn and implement during the course.
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)
22 views81 pages

OOP Manual Update

The document is a practical manual for the Object Oriented Programming using C++ course at the KIT-Kalaignarkarunanidhi Institute of Technology. It includes instructions for laboratory classes, a list of experiments, program outcomes, course outcomes, and educational objectives. The manual outlines various C++ programming tasks and concepts that students are expected to learn and implement during the course.
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/ 81

KIT- KALAIGNARKARUNANIDHI INSTITUTE OF TECHNOLOGY

(An Autonomous Institute, Approved by AICTE &


Affiliated to Anna University, Chennai)

Kannampalayam Post, Coimbatore -641 402

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B23CSI302 – OBJECT ORIENTED PROGRAMMING USING C++

PRACTICAL MANUAL

NAME :
ROLL NO. :
REGISTER NO. :
BRANCH : B.E CSE
YEAR : II
SEMESTER : III SEM
KIT- KALAIGNARKARUNANIDHI INSTITUTE OF TECHNOLOGY
(An Autonomous Institute, Approved by AICTE & Affiliated to Anna University, Chennai)
Kannampalayam Post, Coimbatore -641 402

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

BONAFIDE CERTIFICATE

Record Work of B23CSI302 – OBJECT ORIENTED PROGRAMMING USING C++


certified that this record is the bonafide work done by

Name: ………………………………………………………………………………………………

Class[Year/Sem]: …………………………………. Register Number: ……………………………………

Place: KIT, CBE Faculty In-Charge HOD

Date:

University Register No……………………………………

Submitted for the University Practical Examination held on …………………………...

Internal Examiner External Examiner


Instructions for Laboratory Classes
1. Enter the lab with the record workbook & necessary things.

2. Enter the lab without bags and footwear.

3. Footwear should be kept in the outside shoe rack neatly.

4. Maintain silence during the Lab Hours.

5. Read and follow the work instructions inside the laboratory.

6. Handle the computer systems with care.

7. Shutdown the Computer properly and arrange chairs in order before leaving the lab.

8. The program should be written on the left side pages of the record work book.

9. The record workbook should be completed in all aspects and submitted in the very next class itself.

10. Experiment number with date should be written at the top left corner of the record work book page.

11. Strictly follow the uniform dress code for Laboratory classes.

12. Maintain punctuality for lab classes.

13. Avoid eatables inside and maintain the cleanliness of the lab.
OBSERV PERFOR VIVA- TOTA
SL. PAGE SIGNATUR
ATION MANCE VOCE L (75
NO DATE NAME OF THE EXPERIMENT NUMB E OF
(25 (40 (10 MARK
. ER FACULTY
MARKS) MARKS) MARKS) S)

1. A. Write a C++ program to find the sum of


1.
individual digits of a positive integer.

1. B. Write a C++ program to generate the first


2.
n terms of the sequence.

2. A. Write a C++ program to generate all the


3. prime numbers between 1 and n, where n is a
value supplied by the user.
2. B. Write a C++ program to find both the
4. largest and smallest number in a list of
integers.
3. Create a C++ program with a class named
“Student” to store student’s details
5. A. Implement an array of objects of the class
to store details of multiple students.
3. Create a C++ program with a class named
“Student” to store student’s details
6. B. Write functions to input data for each
student and display details of all students.
4. Develop a C++ program with class named
"Shape" with member variables representing
geometric shapes and member functions to
7.
calculate area and perimeter.
a) Use pointers to members to access and
modify member variables dynamically.
4. Develop a C++ program with class named
"Shape" with member variables representing
geometric shapes and member functions to
8. calculate area and perimeter.
b) Implement member functions to perform
various operations on shapes, such as
scaling or rotating.
5. Create a C++ program to implement a class
named "MyString" to represent strings with
basic string manipulation functionalities.
9. a) Overload operators such as +
(concatenation), == (comparison), +=
(append), and << (output) to perform string
manipulation operations.
5. Create a C++ program to implement a class
named "MyString" to represent strings with
basic string manipulation functionalities.
10.
b) Implement lambda expressions to define
custom comparison functions for sorting
strings.
5. Create a C++ program to implement a class
named "MyString" to represent strings with
basic string manipulation functionalities.
11. c) Demonstrate the usage of overloaded
operators and lambda expressions for string
manipulation within the main program.
VIVA-
OBSERV PERFOR TOTAL
SL. VOCE
ATION MANCE (75 SIGNATURE
NO DATE NAME OF THE EXPERIMENT Pg.No (10
(25 (40 MARKS OF FACULTY
. MARKS
MARKS) MARKS) )
)
6. Define a class named "Complex" to
represent complex numbers with real and
imaginary parts.
12. a) Overload binary operators such as addition,
subtraction, multiplication, and division to
perform arithmetic operations on complex
numbers.
6. Define a class named "Complex" to
represent complex numbers with real and
imaginary parts.
13.
b) Overload unary operators such as negation
and increment to perform specific operations
on complex numbers.
6. Define a class named "Complex" to
represent complex numbers with real and
imaginary parts.
14. c) Implement operator overloading functions
as member functions or friend functions as
appropriate.
7. Write a C++ program to create a base class
named "Shape" with a virtual function named
15. calculateArea().
a) Derive classes like "Rectangle" and "Circle"
from the Shape class.
7. Write a C++ program to create a base class
named "Shape" with a virtual function named
calculateArea().
16.
b) Implement the calculateArea() function in
each derived class to calculate the area of the
respective shape.
7. Write a C++ program to create a base class
named "Shape" with a virtual function named
calculateArea().
17.
c) Demonstrate polymorphic behaviour by
creating an array of Shape pointers and
assigning derived class objects to them.
7. Write a C++ program to create a base class
named "Shape" with a virtual function named
calculateArea().
18. d) Define a pure virtual function in the base
class "Shape" called draw(), which has no
implementation.
8. Create a class named "Point" to represent
2D points with attributes x and y. in C++ to
19.
A) Implement member functions to set and
display the coordinates of a point.
8. Create a class named "Point" to represent
2D points with attributes x and y. in C++ to
20. B) Utilize this pointer to access the member
variables within member functions.
OBSERV PERFOR VIVA- TOTAL
SL.
DA Pg. ATION MANCE VOCE (75 SIGNATURE
NO NAME OF THE EXPERIMENT
TE No (25 (40 (10 MARKS OF FACULTY
.
MARKS) MARKS) MARKS) )
8. Create a class named "Point" to represent
2D points with attributes x and y. in C++ to
21. C) Demonstrate the usage of the this pointer
to differentiate between local variables and
member variables.
9. Write a C++ program that randomly
generates complex numbers (use previously
designed Complex class) and writes them two
per line in a file along with an operator (+,-, *,
or /). The numbers are written to file in the
22.
format (a + ib). Write another program to read
one line at a time from this file, perform the
corresponding operation on the two complex
numbers read, and write the result to another
file (one per line)
10. Write a C++ program to implement
23.
exception handling for file operations.

Average:
Programme Outcomes (POs)
Students graduating from Computer Science and Engineering should be able to:
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of Computer Science and Business Systems problems.
2. Problem Analysis: Identify, formulate, research literature, and analyze complex engineering problems reaching
substantiated conclusions using first principles of mathematics, natural sciences, and Computer Science and Business
Systems.
3. Design/Development of Solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public health and safety,
and the cultural, societal, and environmental considerations in the field of Computer Science and Business Systems.
4. Conduct Investigations of Complex Problems: Using research-based knowledge and Computer Science and
Business Systems oriented research methodologies including design of experiments, analysis and interpretation of
data, and synthesis of the information to provide valid conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern engineering and
IT tools including prediction and modeling to complex Computer Science and Business Systems Engineering
activities with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess societal, health,
safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional Computer Science and Business
Systems Engineering solutions in societal and environmental contexts, and demonstrate the knowledge, and need for
the sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or leader in diverse teams
and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering community
and with society at large, such as, being able to comprehend and write effective reports and design documentation,
make effective presentations, and give and receive clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of the Computer Science and
Business Systems engineering and management principles and apply these to one’s own work, as a member and
leader in a team and, to manage projects in multidisciplinary environments.
12. Lifelong Learning: Recognize the need for, and have the preparation and ability to engage in independent and
life-long learning in the broadest context of technological change.
PROGRAM SPECIFIC OUTCOMES (PSOs)
After the successful completion of the U.G. programme in Computer Science and Engineering, Graduates will
be able to:
PSO1: Categorize the basic engineering knowledge to solve the problems in Computer Science and Engineering
according to the environmental needs.
PSO2: Apply the modern tools to design and develop the software system ethically to the industrial needs.
PROGRAM EDUCATIONAL OBJECTIVES (PEOs)
PEO1: Graduate will be successful in their profession by taking part actively in the field of software and
technology.
PEO2: Graduate will be proficient in analyzing and facing the challenge in computer science and engineering.
PEO3: Graduate will engage in lifelong learning activities by adapting to the advanced software technologies
for continuous professional development.

COURSE OUTCOMES
At the end of this course, the student will be able to:
Course Outcomes Knowledge
Level
CO1 Summarize the basics of Object-Oriented Programming concepts. K2
CO2 Make use objects, classes and functions in applications K3
CO3 Apply the concepts of constructors and destructors for object K3
creation and deletion
CO4 Analyse the use of inheritance, polymorphism through virtual, K4
overriding function and abstract class in programs.
CO5 Examine the concept of I/O operations and file streams using C++ K4
programming.

PO PO1 PO1 PO1


COs PO1 PO2 PO4 PO5 PO6 PO7 PO8 PO9 PSO 1 PSO 2
3 0 1 2
CO1 3 3 2 - - - - - 1 1 - 2 3 2
CO2 3 3 1 - - - - - 1 1 - 1 2 2
CO3 3 3 2 - - - - - 1 1 - 2 3 2
CO4 3 3 2 - - - - - 1 1 - 2 3 2
CO5 3 3 2 - 1 - - - 1 1 - 2 3 2
WEIG
HTED
3 3 2 - 1 - - - 1 1 - 2 3 2
AVER
AGE
SYLLABUS

LIST OF EXPERIMENTS:

Ex. No. Name of the Experiment(s)


a) Write a C++ program to find the sum of individual digits of a positive integer.
1. b) Write a C++ program to generate the first n terms of the sequence.

a) Write a C++ program to generate all the prime numbers between 1 and n, where
2. n is a value supplied by the user.
b) Write a C++ program to find both the largest and smallest number in a list of
integers.

Create a C++ program with a class named “Student” to store student’s details
3.
a) Implement an array of objects of the class to store details of multiple students.
b) Write functions to input data for each student and display details of all students.

Develop a C++ program with class named "Shape" with member variables
representing geometric shapes and member functions to calculate area and
perimeter.
4.
a) Use pointers to members to access and modify member variables dynamically.
b) Implement member functions to perform various operations on shapes, such as
scaling or rotating.

Create a C++ program to implement a class named "MyString" to represent strings


with basic string manipulation functionalities.

a) Overload operators such as + (concatenation), == (comparison), += (append),


and << (output) to perform string manipulation operations.
5.
b) Implement lambda expressions to define custom comparison functions for
sorting strings.
c) Demonstrate the usage of overloaded operators and lambda expressions for
string manipulation within the main program.

Define a class named "Complex" to represent complex numbers with real and
imaginary parts.

a) Overload binary operators such as addition, subtraction, multiplication, and


6. division to perform arithmetic operations on complex numbers.
b) Overload unary operators such as negation and increment to perform specific
operations on complex numbers.
c) Implement operator overloading functions as member functions or friend
functions as appropriate.

Write a C++ program to create a base class named "Shape" with a virtual function
7.
named calculateArea().
a) Derive classes like "Rectangle" and "Circle" from the Shape class.
b) Implement the calculateArea() function in each derived class to calculate the
area of the respective shape.
c) Demonstrate polymorphic behaviour by creating an array of Shape pointers and
assigning derived class objects to them.
d) Define a pure virtual function in the base class "Shape" called draw(), which has
no implementation.

Create a class named "Point" to represent 2D points with attributes x and y in C++
to
A) Implement member functions to set and display the coordinates of a point.
8.
B) Utilize this pointer to access the member variables within member functions.
C) Demonstrate the usage of the this pointer to differentiate between local variables
and member variables.

D) Write a C++ program that randomly generates complex numbers (use


previously designed Complex class) and writes them two per line in a file along
with an operator (+,-, *, or /). The numbers are written to file in the format (a + ib).

Write another program to read one line at a time from this file, perform the
9.
corresponding operation on the two complex numbers read, and write the result to
another file (one per line).

10. Write a C++ program to implement exception handling for file operations.

S. NO EXPERIMENT PREREQUISITES LEARNING OBJECTIVES


1. Basic of C++
FOR ALL PROGRAMMING 2. Understand concepts of OOP
1.
EXPERIMENTS IN C
Ex. No. 1.A
SUM OF DIGITS OF A POSITIVE INTEGER
Date

AIM:

To calculate the sum of the digits of a given positive integer using a C++ program.

PROCEDURE:
1. Start the program.
2. Declare integer variables number and sum, initialize sum = 0.
3. Prompt the user to input a positive integer.
4. Read the input number.
5. If number ≤ 0, display error and terminate.
6. While number > 0:
7. Get last digit using number % 10.
8. Add digit to sum.
9. Remove last digit using number /= 10.
10. Display the result.
11. End the program.

CODE:
SAMPLE OUTPUT:
INFERENCE:

VIVA VOCE:

1. What does the program do?

2. What will happen if a non-positive integer is entered?

3. How does the program extract and sum the digits?


RESULT:

SL. Particular Maximum Marks


NO Marks Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
.
Ex. No. 1.B
GENERATE THE FIRST N TERMS OF A SEQUENCE
Date

AIM:

To write a C++ program to print the first n natural numbers using a loop.

PROCEDURE:

1. Start the program.


2. Declare an integer variable n.
3. Prompt user to input number of terms n.
4. Read n from user.
5. Use a for loop from i = 1 to i <= n:
6. Print the value of i.
7. Print a newline.
8. End the program.
CODE:

SAMPLE OUTPUT:
INFERENCE:

VIVA VOCE:

1 . What is a for loop?

2 . Why is a for loop used in this program?

3. What is the syntax of a for loop in C++?

RESULT:

SL. NO Particular Marks Marks Obtained


1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. 2.A
No.
PRIME NUMBERS
Date

AIM:
To write a C++ program to print all prime numbers from 1 to n.
PROCEDURE:

Start the program.


Declare integer n.
Prompt and read input for n.
For every integer i from 2 to n:
Set isPrime = true.
For each j from 2 to √i:
o If i % j == 0, set isPrime = false and break.
If isPrime, print i.
Print newline.
End the program.
CODE:
INFERENCE:

VIVA VOCE:

1. What is the purpose of the modulo operator (%) in this program?

2. What does the condition j * j <= i in the inner loop do?

2. Explain the use of the break statement in the program.

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. No. 2.B
LARGEST AND SMALLEST NUMBER
Date

AIM:
To write a C++ program to find the smallest and largest numbers in a list.

PROCEDURE:

1. Start the program.


2. Declare integer n and array nums[n].
3. Prompt user to input n, the number of integers.
4. Read n values into array nums.
5. Use sort(nums, nums + n) to sort the array.
6. Print:
a. nums[0] as smallest,
b. nums[n - 1] as largest.
7. End the program.
CODE:

INFERENCE:
VIVA VOCE:

1. What is the purpose of the sort() function in this program?

2. How does the program determine the smallest and largest numbers after sorting?

3. What would happen if the sort() function was not used?

RESULT

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex .No. 3.A
CLASS FOR STORING STUDENT DETAILS
Date

AIM:

To write a C++ program using a class named Student to store and display the details of multiple students using an
array of objects.

PROCEDURE:

 Start the program.


 Define a class Student with data members: name, rollNo, and marks.
 Define member functions:
o inputDetails() to input student data.
o displayDetails() to print student data.
 In main():
o Declare n for the number of students.
o Create an array of Student objects.
o Use a loop to input details of each student.
o Use another loop to display the details.
 End the program.
CODE:

INFERENCE:
VIVA VOCE:

1. What does this program do?

2. How are the student details stored?

3. What is the role of the inputDetails() and displayDetails() functions?

RESULT:

SL. NO Particular Marks Marks


Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10

TOTAL 75
Ex. No. 3.B
DISPLAY DETAILS OF ALL STUDENTS
Date

AIM:
To extend the previous student program by adding functions to input and display data for all students.

PROCEDURE:

1. Define a Student class with appropriate member variables.


2. Implement:
o inputDetails() to read name, roll number, and marks.
o displayDetails() to print all student attributes.
3. In main():
o Read the number of students (n).
o Declare an array of Student objects.
o Loop to input and then loop to display all student data.
4. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1.What is a class in C++ and how is it used in this program?

2.What is the difference between a class and an object?

3.Why is the Student class public in this program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No: 4.A POINTERS TO MEMBERS TO ACCESS AND MODIFY MEMBER
VARIABLES DYNAMICALLY
Date:

AIM:
To use pointers to members in a class Shape to access and modify geometric properties dynamically and
calculate area and perimeter.

PROCEDURE:
1. Define a class Shape with members: radius, length, width.
2. Define member functions for:
a. Area and perimeter of a circle and a rectangle.
3. Declare pointers to member variables and member functions.
4. Read shape parameters using pointers.
5. Invoke member functions using pointers to calculate results.
6. Display area and perimeter.
7. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:
1. What is the purpose of using member function pointers in this program?

2. What is the role of the pointers to member variables (like ptrRadius, ptrLength, and
ptrWidth)?

3. What would happen if the member function pointers were not used in this program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. No.
4.B MEMBER FUNCTIONS TO PERFORM VARIOUS OPERATIONS
ON SHAPES, SUCH AS SCALING OR ROTATING
Date:

AIM:

To write a C++ program with a Shape class that supports operations like scaling and rotating.

PROCEDURE:
1. Define a Shape class with members: radius, length, width.
2. Implement:
o calculateAreaCircle(), calculatePerimeterCircle()
o calculateAreaRectangle(), calculatePerimeterRectangle()
o scale(factor) to multiply dimensions
o rotate() to swap length and width
o displayDetails() to show shape properties
3. In main():
o Read shape dimensions.
o Call displayDetails().
o Scale the shape and show results.
o Rotate the rectangle and show updated results.
4. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What is the purpose of the scale() function in this program?

2. How does the rotate() function affect the rectangle in this program?

3. Why is a constructor used in the Shape class, and what does it do?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10

TOTAL 75
Ex. No: 5.A OPERATOR OVERLOADING TO STRING
MANIPULATION
Date :

AIM:
To implement a class MyString in C++ that supports basic string manipulation using operator overloading for
+, ==, +=, and <<.

PROCEDURE:

1. Define a class MyString with a private member string str.


2. Implement:
o Constructors to initialize the string.
o operator+ to concatenate two strings.
o operator== to compare two strings.
o operator+= to append a string.
o display() function to print the string.
3. In main():
o Read two input strings.
o Perform concatenation, comparison, and appending using overloaded operators.
o Display results using display().
4. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What is the purpose of operator overloading in this program, and how is it used?

2. How does the += operator work in this program, and why does it return a reference to *this?

3. Why is the const keyword used in the member functions of the MyString class (like in operator+
and display)?

RESULT:

SL. NO Particular Marks Marks Obtained


1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No 5.B
LAMBDA EXPRESSIONS TO SORT STRINGS
Date :

AIM:
To write a C++ program that sorts a list of strings in descending order using lambda expressions as custom
comparison functions.

PROCEDURE:

1. Start the program.


2. Declare an array of strings and read the number of elements (n).
3. Read n strings from the user.
4. Use the sort() function with a lambda comparator: [](const string& x, const string& y) { return x > y; }
for descending order.
5. Display the sorted strings (maximum of 100).
6. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. How does the sorting work in this program?

2. What is the purpose of std::min(100, n) in the output loop?

3. What would happen if n is greater than 100?

RESULT:

SL. NO Particular Marks Marks Obtained


1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. 5.C
DEMONSTRATE THE USAGE OF OVERLOADED
No OPERATORS AND LAMBDA EXPRESSIONS
Date :

AIM:

To demonstrate the usage of overloaded operators and lambda expressions for string manipulation
within a C++ program.

PROCEDURE:

1. Define a class MyString with a member str.


2. Implement:
a. operator+, operator==, operator+=
b. toUpper() to convert the string to uppercase.
3. display() to print the string.
4. In main():
a. Read two strings from the user.
b. Create objects of MyString.
5. Use overloaded operators for concatenation, comparison, and appending.
6. Use a lambda expression to convert the result string to uppercase.
7. Display the final result.
8. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What does operator overloading mean in this program?

2. What does the toUpper() function do in the MyString class?

3. What is the use of auto in this code?

RESULT:

SL. NO Particular Marks Marks Obtained

1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No. 6.A OVERLOAD BINARY OPERATORS FOR COMPLEX
Date NUMBER OPERATIONS

AIM:

To implement a C++ class Complex and overload binary operators +, -, *, and / to perform arithmetic
operations on complex numbers.

PROCEDURE:

1. Define a class Complex with private members real and imag.


2. Provide:
3. Default and parameterized constructors.
4. Overload:
a. operator+ for addition.
b. operator- for subtraction.
c. operator* for multiplication.
d. operator/ for division.
5. A display() method to print complex numbers.
6. In main():
7. Get input for two complex numbers.
8. Create Complex objects.
9. Perform operations using overloaded operators.
10. Display the results.
11. End the program.
CODE:
INFERENCE:

VIVA VOCE:

1. What does operator overloading mean in this program?

2. What does the display() function do in the Complex class?

3. What is the purpose of the denom variable in the division operator?

RESULT:

SL. Particular Maximum Marks Obtained


NO Marks
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. No. 6.B
OVERLOAD UNARY OPERATORS (NEGATION,
INCREMENT) FOR COMPLEX NUMBERS
Date

AIM:

To overload unary operators (-, ++) in a Complex class to perform negation and increment operations.

PROCEDURE:
1. Define a Complex class with members real and imag.
2. Overload:
o Unary - to negate both real and imaginary parts.
o Pre-increment ++c to increment both parts and return updated object.
o Post-increment c++ to increment but return original value.
3. In main():
o Read input for one complex number.
o Create a Complex object.
o Apply unary operations and display results.
4. End the program.
CODE:
INFERENCE:

VIVA VOCE:

1. What does operator overloading mean in this program?

2. What is the purpose of the operator++ in this program?

3. How does the display() function work in the Complex class?

RESULT:

SL. Particular Marks Marks Obtained


NO

1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. 6.C
No. OVERLOADING FUNCTIONS AS MEMBER FUNCTIONS
OR FRIEND FUNCTIONS
Date

AIM:

To demonstrate overloading of operators as both member and friend functions for a Complex class.

PROCEDURE:
1. Define a Complex class with private members real and imag.
2. Overload:
o Unary operators (-, ++, ++(int)) as member functions.
o Binary operators (+, -, *, /) as friend functions.
3. Implement a display() method for formatted output.
4. In main():
o Read two complex numbers.
o Create Complex objects.
o Perform and display results of all operations.
5. End the program.
CODE:
INFERENCE:

VIVA VOCE:
1.What is operator overloading in this program?

2. What is the difference between pre-increment (++c1) and post-increment (c1++) in this
program?

3. Why are friend functions used for the binary operators (+, -, *, /) in this program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. No. 7.A
VIRTUAL FUNCTION
Date

AIM:
To implement a base class Shape with a virtual function calculateArea(), and derive classes like
Rectangle and Circle that override this function.

PROCEDURE:

 Define an abstract class Shape with a pure virtual function calculateArea().


 Derive class Rectangle:
Members: length, width.
Implement calculateArea() as length * width.
 Derive class Circle:
Member: radius.
Implement calculateArea() as π * radius².
 In main():
Ask user to choose shape.
Read dimensions.
Create appropriate object and call calculateArea().
End the program.
CODE:
INFERENCE:

VIVA VOCE:

1. What is the purpose of the calculateArea() function in the Shape class?

2. Why is the Shape class abstract in this program?

3. How does polymorphism work in this program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex .No. 7.B
CALCULATE AREA
Date

AIM:

To demonstrate runtime polymorphism using virtual functions by overriding calculateArea() in derived classes.

PROCEDURE:

base class Shape with virtual function calculateArea().


 Implement:
Rectangle class with length and width.
Circle class with radius.
 In main():
Ask user for shape choice and input dimensions.
Create object and call the appropriate calculateArea() function.
End the program.
CODE:

]
INFERENCE:

VIVA VOCE:
1. What is the purpose of the pure virtual function calculateArea() in the Shape class?

2. Why is Shape class considered abstract in this program?

3. How does polymorphism work in this program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
Ex. No. 7.C
POLYMORPHIC BEHAVIOR BY CREATING AN ARRAY OF
SHAPE POINTERS
Date

AIM:

To demonstrate polymorphic behavior by using an array of base class (Shape) pointers to hold and invoke
methods on derived class objects.

PROCEDURE:

Create abstract base class Shape with calculateArea() as pure virtual.


Derive Rectangle and Circle classes with their own calculateArea() methods.
In main():
 Declare an array of Shape*.
 Dynamically allocate objects of Rectangle and Circle.
 Set dimensions using dynamic casting.
 Call calculateArea() for each object via base class pointer.
Delete allocated objects.
End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What is the role of the virtual keyword in the Shape class?

2.Why is the dynamic_cast used in this program?

3.What is the purpose of the delete operator in the program?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No: 7.D
PURE VIRTUAL FUNCTION
Date:

AIM:
To demonstrate the use of a pure virtual function draw() in an abstract class Shape and provide specific
implementations in derived classes.

PROCEDURE:
1. Create an abstract class Shape with a pure virtual function draw().
2. Derive classes Rectangle and Circle:
o Implement draw() to output shape-specific details.
3. In main():
o Create objects of derived classes.
o Call draw() on each to show polymorphic behavior.
4. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:
1. What is the purpose of using the virtual keyword in the Shape class?

2. What is the role of dynamic_cast in this program?

3. Why is memory allocated using new and deallocated using delete in this program?

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75

RESULT:
Ex. No.
8.A
2D POINTS
Date:

AIM:

To create a class Point in C++ that represents 2D points and includes member functions to set and display the
coordinates.

PROCEDURE:

Define a class Point with private members x and y.


Create:
 A setCoordinates() function to assign values to x and y.
 A displayCoordinates() function to print the point as (x, y).
In main():
 Create a Point object.
 Call setCoordinates() with user input.
 Call displayCoordinates() to show the point.
End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What is the purpose of the setCoordinates() function in the Point class?

2. Why is the displayCoordinates() function marked as const?

3. What happens when you create an object of the Point class, like Point p1;?

RESULT:

SL. Particular Marks Marks


NO Obtained
1. AIM 10
2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10

TOTAL 75
Ex. No: 8.B ‘THIS’ POINTER TO ACCESS THE MEMBER
VARIABLES WITHIN MEMBER FUNCTIONS
Date :

AIM:
To demonstrate the use of the this pointer to access member variables inside member functions in a class
Point.

PROCEDURE:

Define a Point class with members x and y.


In the setCoordinates() function:
 Use this->x and this->y to differentiate member variables.
Implement displayCoordinates() to show the values.
In main():
 Create a Point object and input values using the this pointer inside the method.
 Display the coordinates.
End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What is the purpose of the this pointer in the Point class?

2. Why do we use the const keyword in the displayCoordinates function?

3. What happens if you don't use a constructor to initialize the Point object in this program?

RESULT:

SL. NO Particular Marks Marks Obtained


1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No 8.C
“THIS” POINTER TO DIFFERENTIATE BETWEEN
Date : LOCAL VARIABLES AND MEMBER VARIABLES

AIM:

To use the this pointer in a C++ program to differentiate between local variables and member variables having
the same names.

PROCEDURE:
Define a Point class with private members x and y.
In setCoordinates(int x, int y):
 Use this->x = x; and this->y = y; to distinguish member variables from parameters.
Add a display() function to show x and y.
In main():
 Create a Point object.
 Call setCoordinates() with values.
 Call display() to print the point.
End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

2. What is the purpose of using the modulo operator (%) in this program?

3. How does the program remove the last digit from the number?

4. What happens if the user enters a non-positive integer, and how does the program handle it?

RESULT:

SL. NO Particular Marks Marks Obtained


1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No 9
Date : COMPLEX NUMBER OPERATIONS FROM FILE

AIM:

To write a C++ program that randomly generates complex numbers and arithmetic operators, writes
them to a file, and then reads the file to perform the operations and write the results to another file.

PROCEDURE:

Part 1: Write Complex Numbers and Operators to File


1. Define a class Complex with real and imaginary parts.
2. Overload operators +, -, *, / for complex number operations.
3. In main():
o Open an output file in write mode.
o Randomly generate two complex numbers and an operator (+, -, *, /) per line.
o Format: (a + bi) op (c + di)
o Write multiple such expressions to the file.
o Close the file.
🔹 Part 2: Read From File and Write Results
1. Open the input file in read mode.
2. Open another file in write mode for results.
3. For each line:
o Parse two complex numbers and the operator.
o Perform the appropriate operation using overloaded operators.
o Write the result in the format (result).
4. Close both files.
5. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:
1. What is the role of operator overloading in this program?

2. How does the program generate random complex numbers?

3. What is the significance of the writeToFile function in the Complex class?

RESULT:

SL. NO Particular Marks Marks Obtained

1. AIM 10

2. PROCEDURE / ALGORITHM 10

3. OBSERVATION 25

4. OUTPUT / RESULT 20

5. VIVA 10

TOTAL 75
Ex. No :10
EXCEPTION HANDLING FOR FILE OPERATIONS
Date:

AIM:
To write a C++ program that demonstrates exception handling during file operations such as opening,
reading, and writing to files.

PROCEDURE:

1. Start the program.


2. Include the required headers: iostream, fstream, and exception.
3. Use try-catch blocks to handle file exceptions.
4. Inside the try block:
o Attempt to open a file using ifstream or ofstream.
o Check if the file opened successfully:
 If not, throw an exception.
o Perform read/write operations as required.
o Close the file after operations are complete.
5. In the catch block:
o Display an appropriate error message indicating the exception type (e.g., file not found, unable to
read/write).
6. End the program.
CODE:
INFERENCE:

VIVA QUESTIONS:

1. What functions are used for file handling in this program?

2. How does the program handle errors?

3. Why is fclose() used?

SL. NO Particular Marks Marks Obtained


1. AIM 10
2. PROCEDURE / ALGORITHM 10
3. OBSERVATION 25
4. OUTPUT / RESULT 20
5. VIVA 10
TOTAL 75
RESULT:

You might also like