0% found this document useful (0 votes)
15 views2 pages

C++ Lab Assignments for OOP Course

The document outlines lab assignments for an Object-Oriented Programming course using C++. It includes various programming exercises such as creating enums, swapping integers, working with vectors and matrices, and implementing function overloading. Each exercise is designed to enhance students' understanding of C++ programming concepts and practices.

Uploaded by

akkianand041526
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)
15 views2 pages

C++ Lab Assignments for OOP Course

The document outlines lab assignments for an Object-Oriented Programming course using C++. It includes various programming exercises such as creating enums, swapping integers, working with vectors and matrices, and implementing function overloading. Each exercise is designed to enhance students' understanding of C++ programming concepts and practices.

Uploaded by

akkianand041526
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

LAB ASSIGNMENTS

Object-Oriented Programming with


C++
(CSE 3943)

Department of Computer Science & Information Technology


Faculty of Engineering & Technology (ITER)
Siksha ‘O’ Anusandhan Deemed To Be University
Bhubaneswar, Odisha - 751030
Lab Assignment-1

1. Programming Excerices on C++

1.1 WAP to create an enum (Day) containing 7 day names as its attributes. Prompt the user to
enter a day number as int type. Assign this day number to a variable of enum Day. Using
switch display the corresponding day name of the given day number. Pass the variable of Day
in switch and make the cases as attributes of Day.
1.2 Write a function using reference variables as arguments to swap the values of a pair of integers.
1.3 Write a function that creates a vector of user given size M using new operator.
1.4 Write a program to read a matrix of size m*n from the keyboard using new operator.
1.5 Write a program to read a matrix of size m*n from the keyboard and display the same on the
screen using function.
1.6 Rewrite the Problem no. 1.5 to make the row parameter of the matrix as a default argument.
1.7 Write a macro that obtains the largest of the three numbers.
1.8 Write a function power() to raise a number m to power n. The function takes a double value
for m and int value for n and returns the result correctly. Use a default value of 2 for n to
make the function to calculate the squares when this argument is omitted. Write a main that
gets the values of m and n from the user to test the function.
1.9 Write a function that performs the same operation as that of Problem no. 1.8 but takes an int
value for m. Both the functions should have the same name. Write a main that calls both the
functions. Use the concept of function overloading.

Common questions

Powered by AI

Macro definitions, while convenient for inline operations, can introduce pitfalls such as lack of type checking, scope management issues, and unexpected behavior due to changes in input during expansion. In lab assignment 1.7, a macro is used to find the largest of three numbers. Macros do not respect scope, so any variable changes outside the intended operation can lead to incorrect results. Additionally, macros do not evaluate their inputs before applying operations, which can cause unexpected results if inputs have side effects or differ in type .

Operator overloading allows functions to handle different types of input parameters in a seamless manner by defining multiple behaviors for the same function name. This is crucial for achieving polymorphism in C++ programs. In lab assignments 1.8 and 1.9, two versions of the power() function are overloaded: one accepts a double for the base and an int for the exponent, and the other accepts the base as an int. This allows the same function name to be used for different types of data, enhancing code reuse and flexibility while keeping implementation details abstracted away for users .

Default arguments in functions allow for more flexible function calls by providing default values for parameters if none are supplied. This simplifies function use, as it reduces the need for the caller to provide a value under certain circumstances. In lab assignment 1.6, making the row parameter a default argument allows the programmer to omit specifying this parameter unless it is necessary, reducing complexity and improving the convenience of repeated function calls where the default is commonly used .

Using reference variables in functions has the advantage of enabling the function to modify the actual parameters with which it is called, as references are aliases for the variables they refer to. This is particularly useful for swapping values without requiring additional memory for copying values, as exemplified in lab assignment 1.2. The function takes reference variables as arguments, allowing it to directly swap the values of the integers without needing to return or copy large data structures .

Function overloading enhances the usability of software by allowing multiple functions with the same name to exist, each tailored to different parameter types. This leads to more intuitive interfaces for users, as they can use the same function name regardless of the types of arguments they need to work with. In lab assignments 1.8 and 1.9, overloading is demonstrated by having two power() functions: one for a double base and one for an int base. This allows users to easily raise numbers to powers regardless of the numeric type, without having to remember different function names or perform type-specific conversions .

Dynamic memory allocation is beneficial when working with vectors because it allows the program to allocate memory at runtime based on the actual needs of the application, rather than having to commit to a fixed amount of memory at compile time. In lab assignment 1.3, using 'new' enables the program to create a vector of a user-defined size M, allowing for flexible memory management and efficient use of system resources, especially in situations where the data size varies or is large and unpredictable .

Function overloading allows the same function name to be used with different parameter types, promoting polymorphism by enabling a single function name to handle operations for diverse data types. This results in cleaner and more understandable code. In the lab assignments, power functions are overloaded to work with both int and double types. This polymorphism allows the programmer to use the function uniformly across different types, ensuring that operations are performed correctly depending on the type and automatically selecting the appropriate function implementation. It enhances code flexibility, maintainability, and usability while preserving the integrity of function usage across applications .

The use of an enum in programming facilitates better code readability by allowing programmers to assign descriptive names to integral values, which makes the code more intuitive and easier to understand. It reduces errors by providing a limited and defined set of values that a variable can take, minimizing unexpected inputs and behaviors. In lab assignment 1.1, this is applied by creating an enum called 'Day' with seven day names, allowing the user to input an integer corresponding to a day. The switch statement uses these enum values, improving clarity and reducing potential errors that may arise from using arbitrary integers without description .

Using the 'new' operator to read a matrix supports efficient memory management by allowing allocation of memory during runtime specifically tailored to the matrix size, ensuring that memory is used only when necessary and preventing the waste associated with static allocation. In lab assignment 1.4, this approach allows for creating matrices of varying sizes based on user input, promoting adaptability and efficient resource use in applications where data sizes are not known at compile time .

Reusing functions with different default parameters allows creating a single function that can adjust its behavior based on the presence or absence of particular arguments, leading to more streamlined and maintainable code. In the context of lab assignments, default parameters enable functions to handle both simple and complex cases without needing separate implementations. This flexibility reduces redundancy and simplifies changes, as a single function can cater to multiple scenarios by altering only its default settings as needed .

You might also like