Friday, October 14, 2011

C++ Tutorial: Some Lab assignments on Operator Overloading

Question:Write a class to store x, y, and z coordinates of a point in three-dimensional space. Using operator overloading, write friend functions to add, and subtract the vectors.
Solution:
#include<iostream>

using namespace std;

class dimension
{
private:
float xco, yco, zco;
public:
dimension():xco(0),yco(0),zco(0){}
void getco_ordinate();
friend dimension operator +(dimension, dimension);
friend dimension operator -(dimension, dimension);
void display(){cout<<"("<< xco<<" , "<<yco<< " , "<<zco<<")";}
};

void dimension::getco_ordinate()
{

cout<<"Enter the x-coordinate: ";
cin>>xco;
cout<<"Enter the y-coordinate: ";
cin>>yco;
cout<<"Enter the z-coordinate: ";
cin>>zco;
}

dimension operator +(dimension d1,  dimension d2 )
{
dimension temp;
temp.xco = d1.xco + d2.xco;
temp.yco = d1.yco + d2.yco;
temp.zco = d1.zco + d2.zco;
return temp; //nameless object
}

dimension operator -(dimension d1, dimension d2)
{
dimension temp;
temp.xco = d1.xco - d2.xco;
temp.yco = d1.yco - d2.yco;
temp.zco = d1.zco - d2.zco;
return temp;
}

int main()
{
dimension d1, d2, d3, d4;
d1.getco_ordinate();
d2.getco_ordinate();
d1.display();
cout<<" + or - ";
d2.display();
d3 = d1 + d2;
d4 = d1 - d2;
cout<<" = ";
d3.display();
cout<<" or ";
d4.display();
return 0;

}




Question:Compare the two object that contains integer values that demonstrate the overloading of equality (= =), less than (<), greater than (>), not equal (!=),greater than or equal to (>=) and less than or equal to(<=) operators.

Solution:
using namespace std;
class logical
{
private:
int x;
public:
logical():x(0){}
logical(int x1):x(x1){}

bool operator == (logical);
bool operator < (logical);
bool operator > (logical);
bool operator != (logical);
bool operator >= (logical);
bool operator <= (logical);
void display()
{
cout<<x;
}
};

bool logical::operator == (logical c1)
{
if(x == c1.x)
return true;
else
return false;
}

bool logical::operator < (logical c1)
{
if(x < c1.x)
return true;
else
return false;
}

bool logical::operator > (logical c1)
{
if(x > c1.x)
return true;
else
return false;
}

bool logical::operator != (logical c1)
{
if(x != c1.x)
return true;
else
return false;
}
bool logical::operator <= (logical c1)
{
if(x <= c1.x)
return true;
else
return false;
}
bool logical::operator >= (logical c1)
{
if(x >= c1.x)
return true;
else
return false;
}

int main()
{
int x, y;
cout<<"Enter Value of X: ";
cin>>x;
cout<<"Enter the value of Y: ";
cin>>y;
logical l1(x), l2(y);
if(l1 == l2)
cout<<"Both objects are equal "<<endl;
if(l1 < l2)
cout<<"Oject 1 is less than object 2"<<endl;
if(l1 > l2)
cout<<"Oject 1 is greater than object 2"<<endl;
if(l1!=l2)
cout<<"Object 1 is not equal to object 2"<<endl;
if(l1 <= l2)
cout<<"Object 1 is less than or equal to object 2"<<endl;
if(l1 >= l2)
cout<<"Object 1 is greater than or equal to object 2"<<endl;
return 0;
}


Question: Write a class Date that uses pre increment and post increment operators to add 1 to the day in the Date object, while causing appropriate increments to the month and year. (use the appropriate condition for leap year). The increment operators in your Date class should behave exactly as the built in increment operators.

Solution:
#include <iostream>

using namespace std;

class Date
{
private:
int day;
int month;
int year;
static const int days[];
static const char months[][12];
public:
Date(int d = 1 , int m = 1, int yy = 2000);
void setDate(int, int , int);
Date operator ++ ();
Date operator ++(int );
bool check_leapyear(int );
bool check_endofmonth(int );
void helpIncrement();
void display()
{
cout<<day<<" "<<months[month-1]<<" "<<year;
}
};

const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char Date::months[][12] = { "January, ", "Fabruary", "March", "April", "May","June","July","August","September", "October", "November", "December"};

Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}

void Date::setDate(int mm , int dd, int yy)
{
month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;
if ( month == 2 && check_leapyear(year  ))
day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
else
day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
}

void Date::helpIncrement()
{
if(!check_endofmonth(day))
day++;
else
if(month < 12)
{
month++;
day =1;
}
else
{
year++;
month = 1;
day = 1;
}
}

Date Date::operator++()
{
Date temp;
helpIncrement();
temp.day = this->day;
temp.month = this->month;
temp.year = this->year;
return temp;
}

Date Date::operator++(int)
{
Date temp;
temp.day = day;
temp.month = month;
temp.year = year;
helpIncrement();
return temp;
}

bool Date::check_leapyear( int testYear )
{
if ( testYear % 400 == 0 ||
( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true;
else
return false;
}

bool Date::check_endofmonth( int testDay )
{   if ( month == 2 && check_leapyear( year ) )
return testDay == 29;
else
return testDay == days[ month ];
}

int main()
{
Date d1;
Date d2( 2, 28, 1992 ),d4(2 ,28, 1992), d5,d6;

cout <<"The given date is ";
d2.display();
cout<<endl;
d5 = d2++;
cout<<"The date after overloading pre ++,  is ";
d5.display();
cout<<endl;
d6 = ++d4;
cout<<"The date after overloading post ++,  is ";
d6.display();
return 0;
}

1 comment: