Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
cpp operator over loading tutorials       Share
2008-09-28 |  RatheeshTR  | Viewed: 112  |    0

#include <iostream.h>

class Point

{

public:

Po
int (int x, int y):myX(x), myY(y) {}
Po
int (const Point & rhs):
myX(rhs.myX),

myY(rhs.myY)

{

cout << "In Point's copy constructor\n";

}

~Point(){}


int GetX() const { return myX; }
void SetX(int x) { myX = x; }
int GetY() const { return myY; }
void SetY(int y) { myY = y; }

const Po
int & operator++();
const Po
int & operator++(int);
const Po
int & operator--();
const Po
int & operator--(int);

private:

int myX;
int myY; 
};



const Po
int & Point::operator++()
{

++myX;

++myY;

return *this;

}


const Po
int & Point::operator++(int)
{

Po
int temp(*this); //  hold  the  current  value
++myX;

++myY;

return temp;

}



const Po
int & Point::operator--()
{

--myX;

--myY;

return *this;

}


const Po
int & Point::operator--(int)
{

Po
int temp(*this); //  hold  the  current  value
--myX;

--myY;

return temp;

}




class Shape

{

public:

Shape(){}

~Shape(){}

virtual Shape * Clone() const { return new Shape(*this); }

};


class Rectangle : public Shape

{

public:

Rectangle(Po
int upperLeft, Point lowerRight):
myUpperLeft ( new Point(upperLeft)),

myLowerRight(new Point(lowerRight))

{}


Rectangle(
int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY):
myUpperLeft(new Point(upperLeftX,upperLeftY)),

myLowerRight(new Point(lowerRightX,lowerRightY))

{}


Rectangle( const Rectangle & rhs ):

myUpperLeft(new Point(*myUpperLeft)),

myLowerRight(new Point(*myLowerRight))

{

cout << "\nIn Rectangle's copy constructor...\n";

}


Shape * Clone() const { return new Rectangle(*this); }


~Rectangle(){ cout << "\nIn destructor..." << endl; delete myUpperLeft; delete myLowerRight; }


Rectangle & operator=(const Rectangle & rhs);

void Expand() { --(*myUpperLeft); ++(*myLowerRight); }

int GetWidth() { return myLowerRight->GetX() - myUpperLeft->GetX(); }
int GetHeight() { return myLowerRight->GetY() - myUpperLeft->GetY(); }

private:

Po
int * myUpperLeft;
Po
int * myLowerRight;
};


Rectangle & Rectangle::operator=(const Rectangle & rhs)

{

if ( this == &rhs )  //  protect  against  a  =  a
return *this;


delete myUpperLeft;

delete myLowerRight;

myUpperLeft = new Point(*rhs.myUpperLeft);

myLowerRight= new Point(*rhs.myLowerRight);

return *this;

}


int main()
{

Po
int ul(10,10);
Po
int lw(20,30);
Rectangle myRect(ul,lw);


cout << "\nmyRect measures ";

cout << myRect.GetWidth();

cout << " by " << myRect.GetHeight() << endl;


myRect.Expand();


cout << "\nmyRect measures ";

cout << myRect.GetWidth();

cout << " by " << myRect.GetHeight() << endl;



return 0;

}



Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Cpp - Access a Class Member Function Without Creating a Class Object
Cpp - Writing Macro Definition
Cpp - using namespace std
Cpp - Using enum
Cpp - Pointers in C++
Cpp - How to use DLL file in the C++ Program
Cpp - creating and Linking DLL Files to C++ program
Cpp - 1) vector method and iterator (simple Example)
Cpp - 2) Sort and Binary search
Cpp - 3) Vector and Copy vector
Cpp - 4) for_each ( Vector )
Cpp - 5) List and Iterator
Cpp - 6) list and Vector example.2
Cpp - 7) example"fill, copy, list" statement
Cpp - 8) list and set

Related References
cpp - operator overloading
cpp - default arguments
cpp - c++ interview tips
cpp - virtual destructors
cpp - c++ destructors
cpp - csc interview questions
object oriented programming paradigm
elements of object oriented programming
basic concepts of oops and structure of c++ program
exception (runtime error) handling - cpp
cpp function pointers
cpp - access specifiers between base and derived classes
cpp - inautix interview questions
comparing popular programming languages

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us