Chapter 12
Chapter 12
class cl {
// ...
public:
friend void frnd(cl ob);
// ...
};
listing 2
// Demonstrate a friend function.
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
myclass(int i, int j) { a=i; b=j; }
friend int sum(myclass x); // sum() is a friend of myclass
};
int main()
{
myclass n(3, 4);
return 0;
}
listing 3
// Use a friend function.
#include <iostream>
using namespace std;
class C1 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
friend int idle(C1 a, C2 b);
};
class C2 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
friend int idle(C1 a, C2 b);
};
int main()
{
C1 x;
C2 y;
x.set_status(IDLE);
y.set_status(IDLE);
x.set_status(INUSE);
return 0;
}
listing 4
/* A function can be a member of one class and
a friend of another. */
#include <iostream>
using namespace std;
class C1 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
int idle(C2 b); // now a member of C1
};
class C2 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
friend int C1::idle(C2 b);
};
int main()
{
C1 x;
C2 y;
x.set_status(IDLE);
y.set_status(IDLE);
x.set_status(INUSE);
return 0;
}
listing 5
// Use overloaded constructors.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class timer{
int seconds;
public:
// seconds specified as a string
timer(char *t) { seconds = atoi(t); }
void run();
} ;
void timer::run()
{
clock_t t1;
t1 = clock();
int main()
{
timer a(10), b("20"), c(1, 10);
return 0;
}
listing 6
int n = strlen(str);
listing 7
// Demonstrate dynamic initialization.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class timer{
int seconds;
public:
// seconds specified as a string
timer(char *t) { seconds = atoi(t); }
void run();
} ;
void timer::run()
{
clock_t t1;
t1 = clock();
int main()
{
timer a(10);
a.run();
return 0;
}
listing 8
// Demonstrate object assignment.
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
void setab(int i, int j) { a = i, b = j; }
void showab();
};
void myclass::showab()
{
cout << "a is " << a << '\n';
cout << "b is " << b << '\n';
}
int main()
{
myclass ob1, ob2;
ob1.setab(10, 20);
ob2.setab(0, 0);
cout << "ob1 before assignment: \n";
ob1.showab();
cout << "ob2 before assignment: \n";
ob2.showab();
cout << '\n';
return 0;
}
listing 9
#include <iostream>
using namespace std;
class OBJ {
int i;
public:
void set_i(int x) { i = x; }
void out_i() { cout << i << " "; }
};
void f(OBJ x)
{
x.out_i(); // outputs 10
x.set_i(100); // this affects only local copy
x.out_i(); // outputs 100
}
int main()
{
OBJ o;
o.set_i(10);
f(o);
o.out_i(); // still outputs 10, value of i unchanged
return 0;
}
listing 10
// Constructors, destructors, and passing objects.
#include <iostream>
using namespace std;
class myclass {
int val;
public:
myclass(int i) { val = i; cout << "Constructing\n"; }
~myclass() { cout << "Destructing\n"; }
int getval() { return val; }
};
display(a);
return 0;
}
listing 11
// Demonstrate a problem when passing objects.
#include <iostream>
#include <cstdlib>
using namespace std;
class myclass {
int *p;
public:
myclass(int i);
~myclass();
int getval() { return *p; }
};
myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}
int main()
{
myclass a(10);
display(a);
return 0;
}
listing 12
// One solution to the problem of passing objects.
#include <iostream>
#include <cstdlib>
using namespace std;
class myclass {
int *p;
public:
myclass(int i);
~myclass();
int getval() { return *p; }
};
myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}
int main()
{
myclass a(10);
display(a);
return 0;
}
listing 13
// Returning an object.
#include <iostream>
#include <cstring>
using namespace std;
class sample {
char s[80];
public:
void show() { cout << s << "\n"; }
void set(char *str) { strcpy(s, str); }
};
str.set(instr);
return str;
}
int main()
{
sample ob;
return 0;
}
listing 14
// An error generated by returning an object.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class sample {
char *s;
public:
sample() { s = 0; }
~sample() { if(s) delete [] s; cout << "Freeing s\n"; }
void show() { cout << s << "\n"; }
void set(char *str);
};
// Load a string.
void sample::set(char *str)
{
s = new char[strlen(str)+1];
strcpy(s, str);
}
str.set(instr);
return str;
}
int main()
{
sample ob;
return 0;
}
listing 15
myclass x = y; // y explicitly initializing x
func1(y); // y passed as a parameter
y = func2(); // y receiving a returned object
listing 16
// Use a copy constructor to construct a parameter.
#include <iostream>
#include <cstdlib>
using namespace std;
class myclass {
int *p;
public:
myclass(int i); // normal constructor
myclass(const myclass &ob); // copy constructor
~myclass();
int getval() { return *p; }
};
// Copy constructor.
myclass::myclass(const myclass &obj)
{
p = new int;
// Normal Constructor.
myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}
int main()
{
myclass a(10);
display(a);
return 0;
}
listing 17
// The copy constructor is called for initialization.
#include <iostream>
#include <cstdlib>
using namespace std;
class myclass {
int *p;
public:
myclass(int i); // normal constructor
myclass(const myclass &ob); // copy constructor
~myclass();
int getval() { return *p; }
};
// Copy constructor.
myclass::myclass(const myclass &ob)
{
p = new int;
// Normal constructor.
myclass::myclass(int i)
{
cout << "Normal constructor allocating p.\n";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}
int main()
{
myclass a(10); // calls normal constructor
return 0;
}
listing 18
myclass a(2), b(3);
// ...
b = a;
listing 19
/* Copy constructor is called when a temporary object
is created as a function return value.
*/
#include <iostream>
using namespace std;
class myclass {
public:
myclass() { cout << "Normal constructor.\n"; }
myclass(const myclass &obj) { cout << "Copy constructor.\n"; }
};
myclass f()
{
myclass ob; // invoke normal constructor
int main()
{
myclass a; // invoke normal constructor
return 0;
}
listing 20
class cl {
int i;
void f() { ... };
.
.
.
};
listing 21
i = 10;
listing 22
this->i = 10;
listing 23
#include <iostream>
using namespace std;
class cl {
int i;
public:
void load_i(int val) { this->i = val; } // same as i = val
int get_i() { return this->i; } // same as return i
} ;
int main()
{
cl o;
o.load_i(100);
cout << o.get_i();
return 0;
}