C++/Class/static
Содержание
- 1 Accessing static members using non-static member functions.
- 2 Accessing static members without an object.
- 3 Calculate salary using static members.
- 4 Reference static method along with class name
- 5 static counter
- 6 static field is shared among instances
- 7 static functions and ID numbers for objects
- 8 Static member data.
- 9 Static member functions.
- 10 static members in classes
- 11 Static member variables and functions
- 12 Update static field in member method
- 13 Using a static data member in a class
Accessing static members using non-static member functions.
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
virtual int GetHowMany() { return count; }
private:
int itsAge;
static int count;
};
int Cat::count = 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 << CatHouse[i]->GetHowMany();
cout << CatHouse[i]->GetAge()+2;
delete CatHouse[i];
CatHouse[i] = 0;
}
return 0;
}
Accessing static members without an object.
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int count;
private:
int itsAge;
};
int Cat::count = 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;
}
void TelepathicFunction()
{
cout << "There are ";
cout << Cat::count << " cats alive!\n";
}
Calculate salary using static members.
#include <iostream>
using namespace std;
class salary {
public:
void init(const int b) {b_sal = b; your_bonus = 0;}
void calc_bonus(const double perc){ your_bonus = b_sal * perc; }
static void reset_all(const int p){ all_bonus = p; }
int comp_tot() const{ return (b_sal + your_bonus + all_bonus); }
private:
int b_sal;
int your_bonus;
static int all_bonus; //declaration
};
int salary::all_bonus = 100;
int main()
{
salary w1, w2;
w1.init(1000);
w2.init(2000);
w1.calc_bonus(0.2);
w2.calc_bonus(0.15);
salary::reset_all(400);
cout << " w1 " << w1.comp_tot() << " w2 "
<< w2.comp_tot() << endl;
}
Reference static method along with class name
#include <iostream>
using namespace std;
class SomeClass
{
public:
static void message(void) { cout << "Hello, world!\n"; } ;
};
int main(void)
{
SomeClass::message();
}
static counter
#include <iostream>
using namespace std;
class SomeClass
{
public:
static int count;
SomeClass(int value)
{
count++;
my_data = value;
};
SomeClass(int value, int static_value)
{
count = static_value;
my_data = value;
};
~SomeClass(void) { count--; };
int my_data;
};
int SomeClass::count;
int main(void)
{
SomeClass One(1, 999);
cout << "One: " << One.my_data << " " << One.count << endl ;
SomeClass Two(2);
cout << "Two: " << Two.my_data << " " << Two.count << endl ;
SomeClass Three(3);
cout << "Three: " << Three.my_data << " " << Three.count << endl ;
}
#include <iostream>
using namespace std;
class SomeClass
{
public:
SomeClass(int value) { count++; my_data = value; };
SomeClass(int value, int static_value)
{
count = static_value; my_data = value;
};
~SomeClass(void) { count--; };
void show_values(void) { cout << my_data << " " << count << endl; };
private:
static int count;
int my_data;
};
int SomeClass::count;
int main(void)
{
SomeClass One(1, 999);
One.show_values();
SomeClass Two(2, 1000);
Two.show_values();
SomeClass Three(3);
Three.show_values();
}
static functions and ID numbers for objects
#include <iostream>
using namespace std;
class gamma {
private:
static int total;
int id;
public:
gamma(){
total++;
id = total;
}
~gamma(){
total--;
cout << "Destroying ID number " << id << endl;
}
static void showtotal(){
cout << "Total is " << total << endl;
}
void showid(){
cout << "ID number is " << id << endl;
}
};
int gamma::total = 0;
int main(){
gamma g1;
gamma::showtotal();
gamma g2, g3;
gamma::showtotal();
g1.showid();
g2.showid();
g3.showid();
return 0;
}
Static member data.
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int count;
private:
int itsAge;
};
int Cat::count = 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 << Cat::count;
cout << CatHouse[i]->GetAge();
delete CatHouse[i];
CatHouse[i] = 0;
}
return 0;
}
Static member functions.
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int GetHowMany() { return count; }
private:
int itsAge;
static int count;
};
int Cat::count = 0;
void TelepathicFunction();
int main()
{
const int MaxCats = 5;
Cat *CatHouse[MaxCats]; int i;
for (i = 0; i<MaxCats; i++)
{
CatHouse[i] = new Cat(i);
cout << "There are " << Cat::GetHowMany() << " cats alive!\n";
}
for ( i = 0; i<MaxCats; i++)
{
delete CatHouse[i];
cout << "There are " << Cat::GetHowMany() << " cats alive!\n";
}
return 0;
}
static members in classes
#include <iostream>
using namespace std;
class CDummy {
public:
static int n;
CDummy () { n++; };
~CDummy () { n--; };
};
int CDummy::n=0;
int main () {
CDummy a;
CDummy b[5];
CDummy * c = new CDummy;
cout << a.n << endl;
delete c;
cout << CDummy::n << endl;
return 0;
}
Static member variables and functions
#include <iostream>
using namespace std;
class MyClass
{
public:
static int s_Total;
MyClass(int level = 0);
static int GetTotal();
private:
int myLevel;
};
int MyClass::s_Total = 0;
MyClass::MyClass(int level):myLevel(level)
{
cout << "born!" << endl;
++s_Total;
}
int MyClass::GetTotal()
{
return s_Total;
}
int main()
{
cout << MyClass::s_Total << "\n\n";
MyClass myObject1, myObject2, myObject3;
cout << MyClass::GetTotal() << "\n";
return 0;
}
Update static field in member method
#include <iostream>
using namespace std;
class SomeClass
{
public:
SomeClass(int value) { some_value = value; };
void show_data(void) { cout << data << " " << some_value << endl; };
static void set_data(int value) { data = value; };
private:
static int data;
int some_value;
};
int SomeClass::data;
int main(void)
{
SomeClass my_class(1001);
my_class.set_data(5005);
my_class.show_data();
}
Using a static data member in a class
#include <iostream>
using namespace std;
class CBox
{
public:
static int objectCount;
CBox(double lv, double bv = 1.0, double hv = 1.0): m_Length(lv),
m_Breadth(bv),
m_Height(hv)
{
cout << "Constructor called.";
objectCount++;
}
CBox()
{
cout << "Default constructor called.";
m_Length = m_Breadth = m_Height = 1.0;
objectCount++;
}
double Volume() const
{
return m_Length*m_Breadth*m_Height;
}
private:
double m_Length;
double m_Breadth;
double m_Height;
};
int CBox::objectCount = 0;
int main()
{
CBox boxes[5];
CBox cigar(8.0, 5.0, 1.0);
cout << CBox::objectCount;
cout << boxes[2].objectCount;
return 0;
}