Ornek Header File
IntCell.h:
#Bu hatali bir program. Amacim debug islemini ogrenmek
#ifndef _INTCELL_H_
#define _INTCELL_H_
class IntCell
{
public:
/**
* C++ eger tek bir constructor argumani varsa su sekilde yazilima izin veriyor:
* IntCell i = 6;
* Burada derleyici otomatik olarak temp bir IntCell yaratiyor. Atamayi yapiyor ve
* gecici degiskeni yok ediyor.
* explicit kelimesi bunu engellemek icin.
* Sadece IntCell i = IntCell(6);
* ya da IntCell i(6); diyerek yeni bir nesne yaratilabilir.
*/
explicit IntCell(int initValue=0)
{
storedValue = new int(initValue);
}
/**
* Destructor: use delete because the data stored is dynamically allocated
* using new
*/
~IntCell()
{
delete storedValue;
}
/**
* Bu iki IntCell nesneyi karsilastirmak icin
* Ileride verilen findMax icin gerekli bir operator
*/
bool operator<(const IntCell &rhs)
{
return *storedValue < *rhs.storedValue;
}
/**
* Copy assignment operator =
* This is applied to two objects after they have both been constructed
* lhs=rhs is intended to copy the state of rhs to lhs by default
*/
const IntCell & operator=(const IntCell &rhs)
{
if( this != &rhs)
{
storedValue = new int(*(rhs.storedValue));
}
return *this;
}
/**
* Copy constructor
* This constructor is called
* a - when a declaration with initialization is called , but not A=B (this is assignment op)
* b - an object is passed using call by value (instead of by & or const &)
* c - an object returned by value (instead of by & or const &)
*/
IntCell(const IntCell & rhs)
{
*storedValue = *(rhs.storedValue);
}
/**
* Read the stored value: accessor
*/
int read() const
{
return *storedValue;
}
/**
* Change the stored value: mutator
*/
void write(int x)
{
*storedValue = x;
}
private:
int *storedValue;
};
#endif
Etiketler: CPP
Toplam 0 Yorum:
Yorum Gönder
<< Ana Sayfa