operator overloading,friend class,ostream,istream
it seems not very complex I think,I hope you understand it rectangleaType.h
#ifndef RECTANGLETYPE_H_INCLUDED
#define RECTANGLETYPE_H_INCLUDED
#include<iostream>
#include"square.h"
using namespace std;
class rectangleType
{
friend class square;
friend ostream& operator<<(ostream&,const rectangleType&);
friend istream& operator>>(istream&,rectangleType&);
public:
rectangleType(double m=4,double n=5);
void print() const;
void area() const;
void setLenght(double);
rectangleType(rectangleType & p);
void sq(square&) const;
// void perimeter() const();
rectangleType operator+(const rectangleType&) const;
private:
double lenght;
double width;
};
#endif // RECTANGLETYPE_H_INCLUDED
rectangleType.cpp
#include<iostream>
#include"rectangleType.h"
using namespace std;
rectangleType::rectangleType(double l,double w):lenght(l),width(w)
{
}
rectangleType::rectangleType(rectangleType & p) //copy
{
lenght=p.lenght;
width=p.width;
}
void rectangleType::area() const
{
cout<<"area is: "<<lenght*width<<endl;
}
/*void rectangleType::perimeter() const
{
cout<<"perimeter is: "<<(2*lenght)+(2*width)<<endl;
}*/
ostream& operator<<(ostream& osObject,const rectangleType&
rectangle)
{
osObject << "Length = " << rectangle.lenght << "; Width = " <<
rectangle.width;
return osObject;
}
istream& operator>>(istream& isObject,rectangleType& rectangle)
{
isObject >rectangle.lenght;
isObject >rectangle.width;
return isObject;
}
void rectangleType::setLenght(double a)
{
lenght=a;
}
void rectangleType::sq(square& s) const
{
if(lenght==width && lenght==s.a && width==s.a)
cout<<"equal"<<endl;
else
cout<<"not equal";
}
rectangleTypeMain.cpp
#include<iostream>
#include"rectangleType.h"
#include"square.h"
using namespace std;
int main()
{
rectangleType rec1(5.0,5.0),rec2(4.0,3.0);
rectangleType rec3,rec4;
square s(4.0);
rec1.area();
rec2.area();
cout<<"rec1: "<<rec1<<endl;
rec3=rec1;
rec3.area();
rec3.setLenght(20);
rec1.area();
rec3.area();
cout<<"enter the lenght and width:n";
cin>>rec3;
cout<<endl;
rec3.area();
cout<<"enter lenght and width for rectangle:"<<endl;
cin>>rec4;
rec4.sq(s);
return 0;
}
square.h
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include<iostream>
#include"rectangleType.h"
using namespace std;
class square
{
friend class rectangleType;
public:
square(double n=2)
{
a=n;
}
private:
double a;
};
#endif // SQUARE_H_INCLUDED