Return to the C++ Demos Home Page
SAMS Teach Yourself C++ in 21 Days
Chapter Fourteen
Prog 1 : Static data members in Classes
// STYC++ in 21 Chapter 14 prog 1
//
// Example of a static data member of a class
//
//
#include <iostream.h>
class Cat
{
public:
Cat(int age): itsAge(age) { howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int getAge() { return itsAge; }
virtual void setAge(int age) { itsAge = age; }
static int howManyCats;
private:
int itsAge;
};
int Cat::howManyCats = 0;
int main()
{
const int maxCats = 5; int i;
Cat* catHouse[maxCats];
for (i=0; i<maxCats; i++)
catHouse[i] = new Cat(i);
for (i=0; i<maxCats; i++)
{
cout << "There are " << Cat::howManyCats << " cats left!!\n";
cout << "Deleting the one which is " << catHouse[i]->getAge();
cout << " years old\n";
delete catHouse[i];
catHouse[i] = 0;
}
return 0;
} // eos
Prog 2 : Accessing Static Data members w/o a class...
// STYC++ in 21 Chapter 14 prog 2
//
// Accessing static data mebers w/o a class
//
//
#include <iostream.h>
class Cat
{
public:
Cat(int age): itsAge(age) { howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int getAge() { return itsAge; }
virtual void setAge(int age) { itsAge = age; }
static int howManyCats;
private:
int itsAge;
};
int Cat::howManyCats = 0;
void telepathicFunction();
int main()
{
const int maxCats = 5; int i;
Cat* catHouse[maxCats];
for (i=0; i<maxCats; i++)
{
catHouse[i] = new Cat(i);
telepathicFunction();
}
for (i=0; i<maxCats; i++)
{
delete catHouse[i];
telepathicFunction();
}
return 0;
} // end of main
void telepathicFunction()
{
cout << "There are " << Cat::howManyCats << " alive!\n";
}
Prog 3 : Getting Static data with a member func
// STYC++ in 21 Chapter 14 prog 3
//
// Accessing static data w/ non static member funcs
//
//
#include <iostream.h>
class Cat
{
public:
Cat(int age): itsAge(age) { howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int getAge() { return itsAge; }
virtual void setAge(int age) { itsAge = age; }
virtual int getHowMany() { return howManyCats; }
private:
int itsAge;
static int howManyCats;
};
int Cat::howManyCats = 0;
int main()
{
const int maxCats = 5; int i;
Cat* catHouse[maxCats];
for (i=0; i<maxCats; i++)
catHouse[i] = new Cat(i);
for (i=0; i<maxCats; i++)
{
cout << "There are " << catHouse[i]->getHowMany() << " cats left!!\n";
cout << "Deleting the one which is " << catHouse[i]->getAge()+2;
cout << " years old\n";
delete catHouse[i];
catHouse[i] = 0;
}
return 0;
} // end of main & source
Prog 4 : Static Member Fuctions
// STYC++ in 21 Chapter 14 prog 4
//
// Accessing static data mebers w/ a static member function
//
//
#include <iostream.h>
class Cat
{
public:
Cat(int age): itsAge(age) { howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int getAge() { return itsAge; }
virtual void setAge(int age) { itsAge = age; }
static int getHowMany() { return howManyCats; }
private:
int itsAge;
static int howManyCats;
};
int Cat::howManyCats = 0;
void telepathicFunction();
int main()
{
const int maxCats = 5; int i;
Cat* catHouse[maxCats];
for (i=0; i<maxCats; i++)
{
catHouse[i] = new Cat(i);
telepathicFunction();
}
for (i=0; i<maxCats; i++)
{
delete catHouse[i];
telepathicFunction();
}
return 0;
} // end of main
void telepathicFunction()
{
cout << "There are " << Cat::getHowMany() << " alive!\n";
}
Prog 5 : Pointers to Functions
// STYC++ in 21 Chapter 14 prog 5
//
// An example of pointers to functions
//
//
#include <iostream.h>
void square(int&, int&);
void cube(int&, int&);
void swap(int&, int&);
void getVals(int&, int&);
void printVals(int, int);
int main()
{
void (* pFunc) (int &, int &);
bool fQuit = false;
int valOne = 1, valTwo = 2;
int choice;
while (fQuit == false)
{
cout << "(0) Quit (1) Change Vals (2) Square (3) Cube (4) Swap\n";
cin >> choice;
switch(choice)
{
case 1 : pFunc = getVals; break;
case 2 : pFunc = square; break;
case 3 : pFunc = cube; break;
case 4 : pFunc = swap; break;
default : fQuit = true; break;
} // end of switch
if (fQuit) break;
printVals(valOne, valTwo);
pFunc(valOne, valTwo);
printVals(valOne, valTwo);
} // end of while
return 0;
} // end of main
void printVals( int v1, int v2)
{
cout << "The value of X : " << v1 << " and Y : " << v2 << endl;
}
void square( int& v1 , int& v2)
{
v1 *= v1;
v2 *= v2;
}
void cube(int &v1,int& v2)
{
v1 = (v1*v1*v1);
v2 = (v2*v2*v2);
}
void swap(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
void getVals( int& v1, int& v2)
{
cout << "Enter a new value for X : ";
cin >> v1;
cout << "Enter a new value for Y : ";
cin >> v2;
}
// end of source