Create a class Distance with feet and inches. Write an overloaded function to copy the value of one distance object into another distance object using = operator overloading
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Distance{
public:
static int count;
double feet, inches;
Distance()
{
}
Distance(double feet, double inches)
{
this->feet = feet;
this->inches = inches;
}
Distance operator=(const Distance &d)
{
Distance d1;
d1.feet = d.feet;
d1.inches = d.inches;
return d1;
}
void display()
{
cout<<"Value in feet : "<<this->feet<<endl;
cout<<"Value in inches : "<<this->inches<<endl;
}
};
int Distance::count=0;
int main()
{
cout<<"Enter the value in feet : ";
int f;
cin>>f;
cout<<"Enter the value in inches : ";
int in;
cin>>in;
Distance obj1(f, in);
cout<<endl<<"Values of object1"<<endl;
obj1.display();
cout<<endl<<"After copying value of object1 in object2 using operator overloading"<<endl;
cout<<"------------------------------------------------------"<<endl;
Distance obj2 = obj1;
cout<<endl<<"Values of object2"<<endl;
obj2.display();
}
Comments
Leave a comment