listing 1
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
};
// Note: sum() is not a member function of any class.
int sum(myclass x)
{
/* Because sum() is a friend of myclass, it can
directly access a and b. */
return x.a + x.b;
}
int main()
{
myclass n(3, 4);
cout << sum(n);
return 0;
}
listing 3
// Use a friend function.
#include <iostream>
using namespace std;
const int IDLE=0;
const int INUSE=1;
class C2; // forward declaration
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);
};
void C1::set_status(int state)
{
status = state;
}
void C2::set_status(int state)
{
status = state;
}
// idle( ) is a friend of C1 and C2.
int idle(C1 a, C2 b)
{
if([Link] || [Link]) return 0;
else return 1;
}
int main()
{
C1 x;
C2 y;
x.set_status(IDLE);
y.set_status(IDLE);
if(idle(x, y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
x.set_status(INUSE);
if(idle(x, y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
return 0;
}
listing 4
/* A function can be a member of one class and
a friend of another. */
#include <iostream>
using namespace std;
const int IDLE=0;
const int INUSE=1;
class C2; // forward declaration
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);
};
void C1::set_status(int state)
{
status = state;
}
void C2::set_status(int state)
{
status = state;
}
// idle() is member of C1, but friend of C2.
int C1::idle(C2 b)
{
if(status || [Link]) return 0;
else return 1;
}
int main()
{
C1 x;
C2 y;
x.set_status(IDLE);
y.set_status(IDLE);
if([Link](y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
x.set_status(INUSE);
if([Link](y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
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); }
// seconds specified as integer
timer(int t) { seconds = t; }
// time specified in minutes and seconds
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
} ;
void timer::run()
{
clock_t t1;
t1 = clock();
while((clock()/CLOCKS_PER_SEC - t1/CLOCKS_PER_SEC) < seconds);
cout << "\a"; // ring the bell
}
int main()
{
timer a(10), b("20"), c(1, 10);
[Link](); // count 10 seconds
[Link](); // count 20 seconds
[Link](); // count 1 minute, 10 seconds
return 0;
}
listing 6
int n = strlen(str);
double arc = sin(theta);
float d = 1.02 * count / deltax;
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); }
// seconds specified as integer
timer(int t) { seconds = t; }
// time specified in minutes and seconds
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
} ;
void timer::run()
{
clock_t t1;
t1 = clock();
while((clock()/CLOCKS_PER_SEC - t1/CLOCKS_PER_SEC) < seconds);
cout << "\a"; // ring the bell
}
int main()
{
timer a(10);
[Link]();
cout << "Enter number of seconds: ";
char str[80];
cin >> str;
timer b(str); // initialize at run time
[Link]();
cout << "Enter minutes and seconds: ";
int min, sec;
cin >> min >> sec;
timer c(min, sec); // initialize at run time
[Link]();
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;
[Link](10, 20);
[Link](0, 0);
cout << "ob1 before assignment: \n";
[Link]();
cout << "ob2 before assignment: \n";
[Link]();
cout << '\n';
ob2 = ob1; // assign ob1 to ob2
cout << "ob1 after assignment: \n";
[Link]();
cout << "ob2 after assignment: \n";
[Link]();
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; }
};
void display(myclass ob)
{
cout << [Link]() << '\n';
}
int main()
{
myclass a(10);
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;
}
// This will cause a problem.
void display(myclass ob)
{
cout << [Link]() << '\n';
}
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;
}
/* This will NOT cause a problem.
Because ob is now passed by reference, no
copy of the calling argument is made and thus,
no object goes out-of-scope when display()
terminates.
*/
void display(myclass &ob)
{
cout << [Link]() << '\n';
}
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); }
};
// Return an object of type sample.
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr;
[Link](instr);
return str;
}
int main()
{
sample ob;
// assign returned object to ob
ob = input();
[Link]();
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);
}
// Return an object of type sample.
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr;
[Link](instr);
return str;
}
int main()
{
sample ob;
// assign returned object to ob
ob = input(); // This causes an error!!!!
[Link]();
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;
*p = *obj.p; // copy value
cout << "Copy constructor called.\n";
}
// Normal Constructor.
myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
*p = i;
}
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}
// This function takes one object parameter.
void display(myclass ob)
{
cout << [Link]() << '\n';
}
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;
*p = *ob.p; // copy value
cout << "Copy constructor allocating p.\n";
}
// 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
myclass b = a; // calls copy 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
return ob; // implicitly invoke copy constructor
}
int main()
{
myclass a; // invoke normal constructor
a = f(); // invoke copy 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;
}