Bsee21036 Oop Lab 13

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Electrical Engineering Department - ITU

CS152L: Object Oriented Programming Lab

Course Instructor: Nadir Abbas Dated:

Lab Engineer: Usama Riaz Semester: Summer 2023

Session: Batch:

Lab 13. Solving Problems by Utilization of Structs – Operator


Overloading

Obtained Marks/100
Name Roll number

Muhammad Sami Ullah BSEE21036

Checked on: ____________________________

Signature: ____________________________
Objective
The objective of this lab is to observe the basic knowledge of programming in C++.
Equipment and Component
Component Description Value Quantity
Computer Available in lab 1

Conduct of Lab
• Students are required to perform this experiment individually.
• In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab
attendants.

Theory and Background


Structures in C++ provide a convenient way to group related variables together into a single
entity. By defining a structure, you can create a custom data type that represents a specific
concept or object in your program. The member variables within a structure can have different
data types, allowing you to store and organize diverse information. Structures are commonly
used to represent real-world entities such as people, products, or coordinates.

Operator overloading is supported in structures in certain programming languages. Operator overloading


allows you to define how operators behave when applied to objects of a particular class or structure.

In languages such as C++ or C#, you can overload operators within a structure to define custom behavior
for those operators. This means that you can define how operators like +, -, *, /, etc., work when used
with instances of your structure.

Lab Task
Part A [Marks: 5]

Please follow the following steps before starting below tasks:

• Create function.h file for declaration of all functions


• Create function.cpp file to define define all declared functions.
• Create main.cpp file for driving code/call functions.

Note: Make a menu driven program (compulsory).

Part B: Implementation of Structure with Pointers & with Arrays (DMA) [Marks:
35]

Create a structure called Rectangle to represent rectangles in a 2D plane. The Rectangle


structure should have the following properties: length, width, and area.
Implement operator overloading for the Rectangle structure to support the following
operations:
Addition (+): Add two rectangles to create a new rectangle with a combined area.
Subtraction (-): Subtract the area of one rectangle from another rectangle to obtain
a new rectangle with the remaining area.
Multiplication (*): Multiply a rectangle by a scalar value to scale its area.
Division (/): Divide the area of a rectangle by a scalar value to reduce its area.
Write a C++ program that demonstrates the usage of the Rectangle structure and its
overloaded operators. Your program should perform the following tasks:

Create two instances of the Rectangle structure, representing two rectangles with
different dimensions.

Perform addition, subtraction, multiplication, and division operations on the rectangles


using the overloaded operators.

Display the original rectangles, the result of each operation, and their respective areas.

// Paste your code here


//Function.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include<iostream>
#include"Functions.h"
using namespace std;
template <typename A>// teplate function
struct Rectangle{// struct
A Length;// declaration
A Width;// declaration
A Area;// declaration
Rectangle(A leng, A widt) : Length(leng), Width(widt), Area(leng * widt) {}

Rectangle operator+(const Rectangle& sa) const {


A AddedLength = Length + sa.Length;
A AddedWidth = Width + sa.Width;
return Rectangle(AddedLength, AddedWidth);
}

Rectangle operator-(const Rectangle& sa) const {// subtracted operater


A SubtractedLength = Length - sa.Length;
A SubtractedWidth = Width - sa.Width;
return Rectangle(SubtractedLength, SubtractedWidth);
}

Rectangle operator*(A scalar) const {// multiplicative opertater


A scaledLength = Length * scalar;
A scaledWidth = Width * scalar;
return Rectangle(scaledLength, scaledWidth);
}

Rectangle operator/(A scalar) const {// division operater


A reducedLength = Length / scalar;
A reducedWidth = Width / scalar;
return Rectangle(reducedLength, reducedWidth);
}
};
template <typename A>// template function
ostream& operator<<(ostream& os, const Rectangle<A>& rect) {
os << "Length: " << rect.Length << ", Width: " << rect.Width << ", Area: " << rect.Area;
return os;// returnig the value
}
#endif
//Function.cpp
#include"Functions.h"
#include<iostream>
#include<iomanip>
using namespace std;
//main.cpp
#include"Functions.h"
#include<iostream>
using namespace std;//cout library
int main() {// start of main function
int x;
cout<<"OPTINS:-\n 1)Task 01\n 2)Task02\n";
cout<<"Enter a number = ";
cin>>x;
switch(x)
{
case 1:
{// start of m
double L1, W1, L2, W2, a, s;// declaration
cout<<"Enter the Length of 1st rectangle"<<endl;// displaying the output and moving to next line
cin>>L1;// taking input from user
cout<<"Enter the Width of 1st rectangle"<<endl;// displaying the output and moving to next line
cin>>W1;// taking input from user
cout<<"Enter the Length of 2nd rectangle"<<endl;// displaying the output and moving to next line
cin>>L2;// taking input from user
cout<<"Enter the Width of 2nd rectangle"<<endl;// displaying the output and moving to next line
cin>>W2;// taking input from user
cout<<"Enter the scaler value"<<endl;// displaying the output and moving to next line
cin>>a;// taking input from user
cout<<"Enter the divisor value"<<endl;// displaying the output and moving to next line
cin>>s;// taking input from user
Rectangle<double> rect1(L1, W1);// passing the value by parameter
Rectangle<double> rect2(L2, W2);// passing the value by parameter
cout << "rectangle 1:\n" << rect1 << "\n\n";// displaying the output and moving to next line
cout << "rectangle 2:\n" << rect2 << "\n\n";// displaying the output and moving to next line

Rectangle<double> addition = rect1 + rect2;// combining the result of both triangles


cout << "rectangle 1 + rectangle 2:\n" << addition << "\n\n";// displaying the output and moving to
next line

Rectangle<double> subtractionResult = rect1 - rect2;// subtracting the resukt of both triangle


cout << "rectangle 1 - rectangle 2:\n" << subtractionResult << "\n\n";// displaying the output and
moving to next line
double scalar = a;// intializing
Rectangle<double> multiplicationResult = rect1 * scalar;// multiplycation
cout << "rectangle 1 * " << scalar << ":\n" << multiplicationResult << "\n\n";// displaying the output
and moving to next line
double divisor = s;// intializing
Rectangle<double> divisionResult = rect1 / divisor;// division
cout << "rectangle 1 / " << divisor << ":\n" << divisionResult << "\n\n";// displaying the output and
moving to next line

return 0;// end of the main function


}
/*break;
}
case 2:
{

break;
}*/
default:
{
cout<<"You eneterd a wrong number !!";
}

}
return 0;
}
// Paste your output here

Assessment Rubric for Lab

Performance Max
Task CLO Description Exceeds expectation Meets expectation Does not meet expe
metric marks
Executes without errors Executes without errors, user
Does not execute du
excellent user prompts, prompts are understandable,
errors, runtime error
1. Realization good use of symbols, minimum use of symbols or
1 1 Functionality 40 prompts are mislead
of experiment spacing in output. Through spacing in output. Some
existent. No testing h
testing has been completed testing has been completed
completed (0-19)
(35-40) (20-34)
Actively engages and Cooperates with other group
Distracts or discoura
Group cooperates with other member(s) in a reasonable
2. Teamwork 1 3 5 group members from
Performance group member(s) in manner but conduct can be
the experiment (0-1)
effective manner (4-5) improved (2-3)
On Spot Able to make changes (8- Partially able to make
1 1 10 Unable to make chan
3. Conducting Changes 10) changes (5-7)
experiment Answered all questions (8- Unable to answer all
1 1 Viva/Quiz 10 Few incorrect answers (5-7)
10) (0-4)
4. Laboratory
Comments are added and Comments are added and
safety and Code
1 3 5 does help the reader to does not help the reader to Comments are not ad
disciplinary commenting
understand the code (4-5) understand the code (2-3)
rules
Excellent use of white
space, creatively organized Includes name, and
Poor use of white sp
work, excellent use of assignment, white space
5. Data (indentation, blank l
1 3 Code Structure 5 variables and constants, makes the program fairly easy
collection code hard to read, di
correct identifiers for to read. Title, organized work,
and messy (0-1)
constants, No line-wrap (4- good use of variables (2-3)
5)
Solution is efficient, easy A logical solution that is easy
6. Data A difficult and ineffi
1 4 Algorithm 20 to understand, and maintain to follow but it is not the most
analysis solution (0-5)
(15-20) efficient (6-14)
Documentation
7. Computer
1 2 & GitHub 5 Timely (4-5) Late (2-3) Not done (0-1)
use
Submissions
  Max Marks (total): 100 Obtained M

You might also like