// Justin C. Miller
// University of Wisconsin Oshkosh
// Made for: http://www.geocities.com/neonprimetime.geo/index.html
// Date: 2001
// Borland Builder 4.0
// COPY CONSTRUCT and OVERLOADING == and !=
#include
#include
class Cat
{
private:
char * name ;
char * owner ;
int age ;
public:
Cat() ; // USED FOR...
void SetCatInfo(char * , char * , int ) ; // ------------
const Cat & operator=(const Cat & mycat); // Cat1 = Cat2
bool operator==(const Cat & mycat) const ; // Cat1 == Cat2
bool operator!=(const Cat & mycat) const ; // Cat1 != Cat2
} ;
Cat::Cat()
{
name = new char[30] ;
owner = new char[30] ;
age = 0 ;
}
void Cat::SetCatInfo(char * n , char * o , int a)
{strcpy(name, n); strcpy(owner, o) ; age = a ;}
const Cat & Cat::operator=(const Cat & mycat)
{
strcpy(name, mycat.name) ;
strcpy(owner,mycat.owner) ;
age = mycat.age ;
return * this ;
}
bool Cat::operator==(const Cat & mycat) const
{
if(strcmp(name, mycat.name) == 0)
if(strcmp(owner, mycat.owner) == 0)
if(age == mycat.age)
return true ;
return false ;
}
bool Cat::operator!=(const Cat & mycat) const
{
if(strcmp(name, mycat.name) != 0)
return true ;
if(strcmp(owner, mycat.owner) != 0)
return true ;
if(age != mycat.age)
return true ;
return false;
}
int main()
{
Cat fuzzy ;
fuzzy.SetCatInfo("fuzz", "john", 5) ;
Cat tigger ;
tigger.SetCatInfo("tigger", "janet", 9) ;
Cat kitty ;
kitty.SetCatInfo("kitty", "bob", 1) ;
Cat fuzzy2 ;
fuzzy2 = fuzzy ; // uses overloaded =
// also called COPY CONSTRUCTOR
if(fuzzy2 == fuzzy) // uses overloaed ==
cout << "fuzzy2 and fuzzy are the same cats" << endl ;
else
cout << "fuzzy2 and fuzzy are not the same cat" << endl ;
if(tigger != kitty) // uses overloaded !=
cout << "tigger is a different cat than kitty" << endl ;
else
cout << "tigger is the same cat as kitty" << endl ;
getch() ;
return 0 ;
}
// OUTPUT
//
// fuzzy2 and fuzzy are the same cats
// tigger is a different cat than kitty
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)