Sunday, April 11, 2021

simple program using operator overloading (OOP) (65)

#include<iostream>

using namespace std;

class Distance

{

private:

int num;

public:

void getdata()

{

cout<<"please enter the  number here:";

cin>>num;

}

void display()

{

cout<<"subtraction of two objects is="<<num<<endl;

}

Distance operator -(Distance d)

{

Distance result;

result.num = num-d.num;

result.num = num- d.num;

return result;

}

};

int main()

{

Distance dis1,dis2,dis3;

cout<<"first number(should be large then second number):"<<endl;

dis1.getdata();

cout<<"second number:"<<endl;

dis2.getdata();

dis3=dis1-dis2;

cout<<"after subtraction:"<<endl;

dis3.display();

} 

No comments:

Post a Comment