|
ex5-1.cpp - Example 5-1 The this pointer |
// File: ex5-1.cpp
#include <iostream>
#include <cstring>
using namespace std;
class thing
{
private:
int eger;
double mint;
public:
thing(int x = 0, double y = 0.0); // constructor
void copy_a_thing(thing&);
void show_a_thing(void) { cout << eger << ' ' << mint << endl; return;}
};
thing::thing(int x, double y)
{
eger = x;
mint = y;
}
void thing::copy_a_thing(thing& z)
{
if (this == &z)
{
cout << "Don't copy me to myself\n";
return;
}
eger = z.eger;
mint = z.mint;
return;
}
int main(void)
{
thing a(5,3.14);
thing b(1);
thing c;
a.show_a_thing();
b.show_a_thing();
c.show_a_thing();
c.copy_a_thing(a); // copy thing-a to c
c.show_a_thing();
b.copy_a_thing(b); // copy thing-b to b
b.show_a_thing();
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley