Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Lab Performance-2 #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions Lab Performance-2
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,146 @@ Task-2:
An operator overloading allows custom behavior to be defined for built-in operators like addition (+), subtraction (-), when we used with user-defined types.
When overloading binary operators as friend functions, external functions can access private members of a class.
Provide examples how binary operator overloading is implemented using friend functions.
Task 1:-
#include<iostream>
using namespace std;
class Shape{
public:
void area(){

}
void perimeter(){

}
};
class Circle:public Shape{
double r,area_value,perimeter_value;
public:
Circle(){
cout<<"Enter value of radius of circle: ";
cin>>r;
area();
perimeter();
}
void area(){
area_value=3.14*r*r;
}
void perimeter(){
perimeter_value=2*3.14*r;
}
void display(){
cout<<"Area of circle:"<<area_value<<endl;
cout<<"Perimeter of circle:"<<perimeter_value<<endl;
}
};
class Rectangle:public Shape{
double len,bre,area_value,perimeter_value;
public:
Rectangle(){
cout<<"Enter length of rectangle:";
cin>>len;
cout<<"Enter breadth of rectangle:";
cin>>bre;
area();
perimeter();
}
void area(){
area_value=len*bre;
}
void perimeter(){
perimeter_value=2*(len+bre);
}
void display(){
cout<<"Area of rectangle:"<<area_value<<endl;
cout<<"Perimeter of rectangle:"<<perimeter_value<<endl;
}
};
class Triangle:public Shape{
double side1,side2,side3,hei,area_value,perimeter_value;
public:
Triangle(){
cout<<"Enter height of triangle:";
cin>>hei;
cout<<"Enter base or side1 of triangle:";
cin>>side1;
cout<<"Enter side2 of triangle:";
cin>>side2;
cout<<"Enter side3 of triangle:";
cin>>side3;
area();
perimeter();
}
void area(){
area_value=0.5*hei*side1;
}
void perimeter(){
perimeter_value=side1+side2+side3;
}
void display(){
cout<<"Area of triangle:"<<area_value<<endl;
cout<<"Perimeter of triangle:"<<perimeter_value<<endl;
}
};
int main(){
cout<<"Name:- Jatin Watts"<<endl;
cout<<"Roll No:- 2310997130"<<endl;
Circle circle;
circle.display();
cout<<endl;
Rectangle rectangle;
rectangle.display();
cout<<endl;
Triangle triangle;
triangle.display();
cout<<endl;
return 0;
}
Task 2:-
#include <iostream>
using namespace std;
class Overloading{
int j,w;
public:
void get(){
cout<<"Enter value of j: ";
cin>>j;
cout<<"Enter value of w: ";
cin>>w;
}
friend Overloading operator+(Overloading &obj, Overloading &obj1);
friend Overloading operator-(Overloading &obj, Overloading &obj1);
void display0(){
cout<<"Addition of J:"<<j<<endl;
cout<<"Addition of W:"<<w<<endl;
}
void display1(){
cout<<"Subtraction of J:"<<j<<endl;
cout<<"Subtraction of W:"<<w<<endl;
}
};
Overloading operator+(Overloading &obj, Overloading &obj1){
Overloading sum;
sum.j=obj.j+obj1.j;
sum.w=obj.w+obj1.w;
return sum;
}
Overloading operator-(Overloading &obj, Overloading &obj1){
Overloading sub;
sub.j=obj.j-obj1.w;
sub.w=obj.w-obj1.w;
return sub;
}
int main() {
cout<<"Name:- Jatin Watts"<<endl;
cout<<"Roll No:- 2310997130"<<endl;
Overloading obj,obj1,sum,sub;
cout<<"Enter values in first object:- "<<endl;
obj.get();
cout<<"Enter values in second object:- "<<endl;
obj1.get();
sum=obj+obj1;
sub=obj-obj1;
sum.display0();
sub.display1();
return 0;
}