Home

Static Members of a Class

 

Static Member Variables

A class can have static member variables. To declare such a variable, precede it with the static keyword. Here is an example:

public value class CCar
{
private:
    String ^ Make;
    String ^ Model;
    int      Doors;
    int      Year;
    double   Price;
	
public:
    void Build();
    void Show();

    static int Count;
};

When you declare a member variable as static and when the application starts, the compiler creates a copy of that member. This member would be maintained by the compiler while the program is running. If you declare an instance of a class, like the above vehicle variable, the static member is not part of the object: the compiler creates and maintains the static member, whether you use it or not, whether you declare a class variable or not.

In a program, if you use a class that has a static member variable, that member variable is initialize with the minimum value of its type. For example, if the member variable is of an integer type, the static member variable is initialized with 0.

As mentioned already, when a class has a static member variable, you don't need to declare an instance of that class in order to access the static member variable. When the application starts, the member variable is automatically available and you can access it when necessary. You can access it either to retrieve its value or to modify it.

To access a static member of the class outside of that class, enter its data type, followed by the name of the class, followed by the :: operator, followed by the name of the member variable. You can then, for example, retrieve the value of the static member variable. Here is an example:

using namespace System;

public value class CCar
{
private:
    String ^ Make;
    String ^ Model;
    int      Doors;
    int      Year;
    double   Price;
	
public:
    void Build();
    void Show();

    static int Count;
};

void CCar::Build()
{
    Console::WriteLine(L"Enter Car Information");
    Console::Write(L"Enter Make:  ");
    Make = Console::ReadLine();
    Console::Write(L"Enter Model: ");
    Model = Console::ReadLine();
    Console::Write(L"# of Doors:  ");
    Doors = int::Parse(Console::ReadLine());
    Console::Write(L"Year Made:   ");
    Year = int::Parse(Console::ReadLine());
    Console::Write(L"Enter Price: ");
    Price = double::Parse(Console::ReadLine());
}

void CCar::Show()
{
    Console::WriteLine(L"\nCar Characteristics");
    Console::WriteLine(L"Make:  {0}", Make);
    Console::WriteLine(L"Model: {0}", Model);
    Console::WriteLine(L"Doors: {0}", Doors);
    Console::WriteLine(L"Year:  {0}", Year);
    Console::WriteLine(L"Value: {0:C}", Price);
}

int main()
{
    Console::Write("Number of Cars: ");
    Console::WriteLine(CCar::Count);

    Console::WriteLine();
    return 0;
}

Notice that you don't need to declare a variable of a class in order to access a static member variable. The above program produces:

Number of Cars: 0

Press any key to continue . . .

Like the other member variables of a class, you can initialize a static member variable as you see fit. Just remember the rule to access it.

When creating a class, you will decide what member(s) you want to make static and which one(s) will stay regular.

Static Methods

Besides regular member functions, a class can also have static methods. To create a static method, type the static keyword to its left. Here is an example:

public value class CCar
{
private:
    String ^ Make;
    String ^ Model;
    int      Doors;
    int      Year;
    double   Price;
	
public:
    void Build();

    static int  Count;
    static void Display();
};

As mentioned for static member variables, a static method doesn't belong to a particular instance of the class. This means that you don't need to declare a handle of a class in order to access a static method. A static method stands on its own so much that, if you want to access another member of the class, you must declare an instance of that class inside the static method. In other words, the other members of the same class are not automatically accessible from within the static method. As done with the other methods, you can implement a static method outside of the class. If you decide to do this, you must omit the static keyword. Here are examples of static methods:

using namespace System;

public value class CCar
{
private:
    String ^ Make;
    String ^ Model;
    int      Doors;
    int      Year;
    double   Price;
	
public:
    static int  Count;

    static void Build(CCar ^ vehicle);
    static void Display(const CCar ^ car);
};

void CCar::Build(CCar ^ mobile)
{
    Console::WriteLine(L"Enter Car Information");
    Console::Write(L"Enter Make:  ");
    mobile->Make = Console::ReadLine();
    Console::Write(L"Enter Model: ");
    mobile->Model = Console::ReadLine();
    Console::Write(L"# of Doors:  ");
    mobile->Doors = int::Parse(Console::ReadLine());
    Console::Write(L"Year Made:   ");
    mobile->Year = int::Parse(Console::ReadLine());
    Console::Write(L"Enter Price: ");
    mobile->Price = double::Parse(Console::ReadLine());
}

void CCar::Display(const CCar ^ car)
{
    Console::WriteLine(L"\nCar Characteristics");
    Console::WriteLine(L"Make:  {0}", car->Make);
    Console::WriteLine(L"Model: {0}", car->Model);
    Console::WriteLine(L"Doors: {0}", car->Doors);
    Console::WriteLine(L"Year:  {0}", car->Year);
    Console::WriteLine(L"Value: {0:C}", car->Price);
}

int main()
{
    CCar ^ vehicle = gcnew CCar;

    CCar::Build(vehicle);
    CCar::Display(vehicle);

    Console::Write("Number of Cars: ");
    Console::WriteLine(CCar::Count);

    Console::WriteLine();
    return 0;
}

Here is an example of running the program:

Enter Car Information
Enter Make:  Audi
Enter Model: A4 Quattro 3.0 Litre
# of Doors:  4
Year Made:   2003
Enter Price: 25.950

Car Characteristics
Make:  Audi
Model: A4 Quattro 3.0 Litre
Doors: 4
Year:  2003
Value: $25.95
Number of Cars: 0

Press any key to continue . . .

Notice that, inside of main(), you don't need a CCar object (an instance of the CCar class) in order to access the static methods. Also, as mentioned already, you cannot implicitly access the non-static regular members of the class inside of the static method. To access a static member from a static method, you can qualify it. Here is an example:

void CCar::Build(CCar ^ mobile)
{
    Console::WriteLine(L"Enter Car Information");
    Console::Write(L"Enter Make:  ");
    mobile->Make = Console::ReadLine();
    Console::Write(L"Enter Model: ");
    mobile->Model = Console::ReadLine();
    Console::Write(L"# of Doors:  ");
    mobile->Doors = int::Parse(Console::ReadLine());
    Console::Write(L"Year Made:   ");
    mobile->Year = int::Parse(Console::ReadLine());
    Console::Write(L"Enter Price: ");
    mobile->Price = double::Parse(Console::ReadLine());

    CCar::Count++;
}

But you don't have to qualify it: its name would be enough. Here is an example:

using namespace System;

public value class CCar
{
private:
    String ^ Make;
    String ^ Model;
    int      Doors;
    int      Year;
    double   Price;
	
public:
    static int  Count;

    static void Build(CCar ^ vehicle);
    static void Display(const CCar ^ car);
};

void CCar::Build(CCar ^ mobile)
{
    Console::WriteLine(L"Enter Car Information");
    Console::Write(L"Enter Make:  ");
    mobile->Make = Console::ReadLine();
    Console::Write(L"Enter Model: ");
    mobile->Model = Console::ReadLine();
    Console::Write(L"# of Doors:  ");
    mobile->Doors = int::Parse(Console::ReadLine());
    Console::Write(L"Year Made:   ");
    mobile->Year = int::Parse(Console::ReadLine());
    Console::Write(L"Enter Price: ");
    mobile->Price = double::Parse(Console::ReadLine());

    CCar::Count++;
}

void CCar::Display(const CCar ^ car)
{
    Console::WriteLine(L"\nCar Characteristics");
    Console::WriteLine(L"Make:  {0}", car->Make);
    Console::WriteLine(L"Model: {0}", car->Model);
    Console::WriteLine(L"Doors: {0}", car->Doors);
    Console::WriteLine(L"Year:  {0}", car->Year);
    Console::WriteLine(L"Value: {0:C}", car->Price);
    Console::Write("Number of Cars: {0}", Count++);
}

int main()
{
    CCar ^ vehicle = gcnew CCar;

    CCar::Build(vehicle);
    CCar::Display(vehicle);

    Console::WriteLine();
    return 0;
}

Here is an example of running the program:

Enter Car Information
Enter Make:  Jeep
Enter Model: Wrangler Sport
# of Doors:  3
Year Made:   2000
Enter Price: 15500

Car Characteristics
Make:  Jeep
Model: Wrangler Sport
Doors: 3
Year:  2000
Value: $15,500.00
Number of Cars: 1
Press any key to continue . . .

 

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next