Answer to Question #193021 in C++ for Ajith M

Question #193021

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


1
Expert's answer
2021-05-15T13:52:41-0400


#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();
	
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog