Professional Documents
Culture Documents
Chapter 20
Chapter 20
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
listing 2
if(count > lowerbound) return count--;
listing 3
CounterNameSpace::upperbound = 10;
listing 4
CounterNameSpace::counter ob;
listing 5
// Demonstrate a namespace.
#include <iostream>
using namespace std;
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
int main()
{
CounterNameSpace::upperbound = 100;
CounterNameSpace::lowerbound = 0;
CounterNameSpace::counter ob1(10);
int i;
do {
i = ob1.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
CounterNameSpace::counter ob2(20);
do {
i = ob2.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
ob2.reset(100);
CounterNameSpace::lowerbound = 90;
do {
i = ob2.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
return 0;
}
listing 6
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}
listing 7
using CounterNameSpace::lowerbound; // only lowerbound is visible
lowerbound = 10; // OK because lowerbound is visible
listing 8
// Demonstrate using.
#include <iostream>
using namespace std;
namespace CounterNameSpace {
int upperbound;
int lowerbound;
class counter {
int count;
public:
counter(int n) {
if(n <= upperbound) count = n;
else count = upperbound;
}
void reset(int n) {
if(n <= upperbound) count = n;
}
int run() {
if(count > lowerbound) return count--;
else return lowerbound;
}
};
}
int main()
{
// use only upperbound from CounterNameSpace
using CounterNameSpace::upperbound;
CounterNameSpace::counter ob1(10);
int i;
do {
i = ob1.run();
cout << i << " ";
} while(i > CounterNameSpace::lowerbound);
cout << endl;
counter ob2(20);
do {
i = ob2.run();
cout << i << " ";
} while(i > lowerbound);
cout << endl;
ob2.reset(100);
lowerbound = 90;
do {
i = ob2.run();
cout << i << " ";
} while(i > lowerbound);
return 0;
}
listing 9
using namespace std;
listing 10
// Use explicit std:: qualification.
#include <iostream>
int main()
{
double val;
return 0;
}
listing 11
// Bring only a few names into the global namespace.
#include <iostream>
int main()
{
double val;
return 0;
}
listing 12
#include <iostream>
using namespace std;
int main()
{
void (*p)(int i);
return 0;
}
void hline(int i)
{
for( ;i; i--) cout << "-";
cout << "\n";
}
void vline(int i)
{
for( ; i; i--) cout << "|\n";
}
listing 13
(*p)(4);
listing 14
p(4);
listing 15
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
char str[] = "Function pointers provide flexibility.";
return 0;
}
listing 16
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num[] = {10, 4, 3, 6, 5 ,7 ,8};
int i;
qsort(num, 7, sizeof(int), comp);
return 0;
}
listing 17
/* Illustrate assigning function pointers to
overloaded functions. */
#include <iostream>
using namespace std;
int main()
{
/* Create a pointer to void function with
one int parameter. */
void (*fp1)(int);
return 0;
}
listing 18
#include <iostream>
using namespace std;
class ShareVar {
static int num;
public:
void setnum(int i) { num = i; };
void shownum() { cout << num << " "; }
};
int main()
{
ShareVar a, b;
a.shownum(); // prints 0
b.shownum(); // prints 0
a.shownum(); // prints 10
b.shownum(); // also prints 10
return 0;
}
listing 19
class X {
int some_var;
public:
int f1() const; // const member function
};
listing 20
/*
Demonstrate const member functions.
This program won't compile.
*/
#include <iostream>
using namespace std;
class Demo {
int i;
public:
int geti() const {
return i; // ok
}
int main()
{
Demo ob;
ob.seti(1900);
cout << ob.geti();
return 0;
}
listing 21
// Demonstrate mutable.
#include <iostream>
using namespace std;
class Demo {
mutable int i;
int j;
public:
int geti() const {
return i; // ok
}
int main()
{
Demo ob;
ob.seti(1900);
cout << ob.geti();
return 0;
}
listing 22
#include <iostream>
using namespace std;
class myclass {
int a;
public:
myclass(int x) { a = x; }
int geta() { return a; }
};
int main()
{
myclass ob(4);
return 0;
}
listing 23
myclass ob = 4; // automatically converts into myclass(4)
listing 24
myclass ob(4);
listing 25
#include <iostream>
using namespace std;
class myclass {
int a;
public:
explicit myclass(int x) { a = x; }
int geta() { return a; }
};
listing 26
myclass ob(110);
listing 27
#include <iostream>
using namespace std;
class myclass {
int num;
public:
myclass(int i) { num = i; }
int getnum() { return num; }
};
int main()
{
myclass o(10);
return 0;
}
listing 28
o = 1000;
listing 29
#include <iostream>
using namespace std;
class myclass {
int numA;
int numB;
public:
/* Initialize numA and numB inside the myclass constructor
using normal syntax. */
myclass(int x, int y) {
numA = x;
numB = y;
}
int getNumA() { return numA; }
int getNumB() { return numB; }
};
int main()
{
myclass ob1(7, 9), ob2(5, 2);
return 0;
}
listing 30
#include <iostream>
using namespace std;
class myclass {
const int numA; // const member
const int numB; // const member
public:
// Initialize numA and numB using initialization syntax.
myclass(int x, int y) : numA(x), numB(y) { }
int main()
{
myclass ob1(7, 9), ob2(5, 2);
return 0;
}
listing 31
#include <iostream>
using namespace std;
int main()
{
myCfunc();
return 0;
}
// This will link as a C function.
void myCfunc()
{
cout << "This links as a C function.\n";
}
listing 32
// Pointer-to-member example.
#include <iostream>
using namespace std;
class myclass {
public:
int sum;
void myclass::sum_it(int x);
};
void myclass::sum_it(int x) {
int i;
sum = 0;
for(i=x; i; i--) sum += i;
}
int main()
{
int myclass::*dp; // pointer to an integer class member
void (myclass::*fp)(int x); // pointer to member function
myclass c;
return 0;
}
listing 33
#include <iostream>
using namespace std;
class myclass {
public:
int sum;
void myclass::sum_it(int x);
};
void myclass::sum_it(int x) {
int i;
sum = 0;
for(i=x; i; i--) sum += i;
}
int main()
{
int myclass::*dp; // pointer to an integer class member
void (myclass::*fp)(int x); // pointer to member function
myclass *c, d; // c is now a pointer to an object
return 0;
}
listing 34
operator int() { return x * y * z; }
listing 35
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-D coordinates
public:
three_d(int a, int b, int c) { x = a; y = b; z = c; }
temp.x = x+op2.x;
temp.y = y+op2.y;
temp.z = z+op2.z;
return temp;
}
int main()
{
three_d a(1, 2, 3), b(2, 3, 4);
return 0;
}