OOP lab 7 task solution
#include<iostream>
using namespace std;
class position{
public:
int x;
int y;
position(){
x = 0;
y = 0;
}
position operator + (position &p_any){
position p1;
p1.x = this->x + p_any.x;
p1.y = this->y + p_any.y;
return p1;
}
};
int main(){
position p1;
cout<<"Enter the intial values of x and y cordinates: ";
cin>>p1.x>>p1.y;
position p2;
cout<<"Enter the new values for x and y to be shifted: ";
cin>>p2.x>>p2.y;
position p3;
p3 = p1+p2;
cout<<"The initial values were: "<<p1.x<< " "<<p1.y<<endl;
cout<<"The new values are: "<<p3.x<<" "<<p3.y<<endl;
}
// from now onward this is the second task
#include<iostream>
//#include<conio.h>
using namespace std;
class Time
{
int hr, min, sec;
public:
// default constructor
Time()
{
hr=0, min=0; sec=0;
}
// overloaded constructor
Time(int h, int m, int s)
{
hr=h, min=m; sec=s;
}
// overloading '<<' operator
friend ostream& operator << (ostream &out, Time &tm);
friend istream& operator>>(istream &in, Time &tm);
};
// define the overloaded function
ostream& operator << (ostream &out, Time &tm)
{
out << "Time is: " << tm.hr << " hour : " << tm.min << " min : " << tm.sec << " sec";
return out;
}
istream& operator >> (istream &in, Time &tm)
{
in>>tm.hr>>tm.min>>tm.sec;
return in;
}
int main()
{
Time tm;
cout<<"Enter the values of time: ";
cin>>tm;
cout << tm;
}
Comments
Post a Comment