0% found this document useful (0 votes)
40 views

Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering

This document discusses C++ classes and class members. It explains how to define classes with public and private data members and member functions. It also covers static class members, including static member variables that are shared across all class objects and static member functions that can access static data without an object instance. An example shows using static variables and functions to generate unique IDs for class objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering

This document discusses C++ classes and class members. It explains how to define classes with public and private data members and member functions. It also covers static class members, including static member variables that are shared across all class objects and static member functions that can access static data without an object instance. An example shows using static variables and functions to generate unique IDs for class objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Mr.

Fourcan Karim Mazumder


Faculty
Dept. of Computer Science and
Engineering
Program: Class and class members
#include <iostream.h>
#include<string.h>
class Employee
{
public:
char m_strName[25];
int m_nID;
double m_dWage;
// Set the employee information
void SetInfo(char *strName, int nID, double
dWage)
{
strncpy(m_strName, strName, 25);
m_nID = nID;
m_dWage = dWage;
}
// Print employee information to the screen
void Print()
{
cout << "Name: " << m_strName << " Id: " <<
m_nID << " Wage: $" << m_dWage << endl;
}
};
 int main()
{ // Declare two employees Employee cAlex;
cAlex.SetInfo("Alex", 1, 25.00);
Employee cJoe;
cJoe.SetInfo("Joe", 2, 22.25);
// Print out the employee information
cAlex.Print();
cJoe.Print();
return 0;
}
Output:
Name: Alex Id: 1 Wage: $25
Name: Joe Id: 2 Wage: $22.25
Program: Public and private access specifier
#include<iostream.h>
class Access
{
int m_nA; // private by default
int GetA() { return m_nA; } // private by default

private:
int m_nB; // private
int GetB() { return m_nB; } // private
 protected:
int m_nC; // protected
int GetC() { return m_nC; } // protected

public:
int m_nD; // public
int GetD() { return m_nD; } // public
};
int main()
{
Access cAccess;
cAccess.m_nD = 5; // okay because m_nD is
public
cout << cAccess.GetD(); // okay because GetD() is
public
// cAccess.m_nA = 2; // WRONG because m_nA
is private
// cout << cAccess.GetB(); // WRONG because
GetB() is private
return 0;
}
Output:
5
Static member variables:
Static variables keep their values and are not
destroyed even after they go out of scope. For
example:
 
int GenerateID()
{
    static int s_nID = 0;
    return s_nID++;
}
 
int main()
{
    std::cout << GenerateID() << std::endl;
    std::cout << GenerateID() << std::endl;
    std::cout << GenerateID() << std::endl;
    return 0;
}
This program prints:
0
1
2
Note that s_nID has kept it’s value across multiple
function calls.
C++ introduces static member variables. Before we
go into the static keyword as applied to member
variables, first consider the following class:
class Something
{
private:
    int m_nValue;
public:
    Something() { m_nValue = 0; }
};
 
 int main()
{
    Something cFirst;
    Something cSecond;
    return 0;
}
When we instantiate a class object, each object
gets it’s own copy of all normal member variables.
In this case, because we have declared two
Something class objects, we end up with two
copies of m_nValue — one inside cFirst, and one
inside cSecond. cFirst->m_nValue is different than
cSecond->m_nValue.
Member variables of a class can be made static by
using the static keyword. Static member variables
only exist once in a program regardless of how
many class objects are defined! One way to think
about it is that all objects of a class share the static
variables. Consider the following program:
class Something
{
public:
    static int s_nValue;
};
int Something::s_nValue = 1;
 int main()
{
    Something cFirst;
    cFirst.s_nValue = 2;
   
 Something cSecond;
    std::cout << cSecond.s_nValue;
     return 0; }
This program produces the following output:
2
Because s_nValue is a static member variable,
s_nValue is shared between all objects of the class.
Consequently, cFirst.s_nValue is the same as
cSecond.s_nValue. The above program shows that
the value we set using cFirst can be accessed using
cSecond!
Although you can access static members through
objects of the class type, this is somewhat
misleading. In fact, s_nValue exists even if there
are no objects of the class have been instantiated!

Consequently, it is better to think of static


members as belonging to the class itself, not the
objects of the class. Because s_nValue exists
independently of any class objects, it can be
accessed directly using the class name and the
scope operator:
class Something
{
public:
    static int s_nValue;
};
 
int Something::s_nValue = 1;
 
int main()
{
    Something::s_nValue = 2;
    std::cout << Something::s_nValue;
    return 0;
}
In the above example, s_nValue is referenced by class
name rather than through an object. Note that we have
not even instantiated an object of type Something, but
we are still able to access and use
Something::s_nValue. This is the preferred method for
accessing static members.
An example of static member variables
Why use static variables inside classes? One great
example is to assign a unique ID to every instance
of the class. Here’s an example of that:
#include<iostream.h>
class Something
{
private:
static int s_nIDGenerator;
int m_nID;
public:
Something() { m_nID = s_nIDGenerator++; }
int GetID() { return m_nID; }
};
int Something::s_nIDGenerator = 1;
int main()
{
Something cFirst;
Something cSecond;
Something cThird;
cout << cFirst.GetID() << endl;
cout << cSecond.GetID() << endl;
cout << cThird.GetID() << endl;
return 0;
}
This program prints:
1
2
3
Because s_nIDGenerator is shared by all
Something objects, when a new Something object
is created, it’s constructor grabs the current value
out of s_nIDGenerator and then increments the
value for the next object. This guarantees that
each Something object receives a unique id
(incremented in the order of creation). This can
really help when debugging multiple items in an
array.
Static member variables can also be useful when
the class needs to utilize an internal lookup table
(eg. to look up the name of something, or to find a
pre-calculated value). By making the lookup table
static, only one copy exists for all objects, rather
than a copy for each object instantiated. This can
save substantial amounts of memory.
Static member functions
We learned that classes can have static member
variables that are shared across all objects of that
class type. However, what if our static member
variables are private? Consider the following
example:
class Something
{
private:
    static int s_nValue;
 };
int Something::s_nValue = 1; // initializer
 int main()
{
    // how do we access Something::s_nValue?
}

In this case, we can’t access Something::s_nValue


directly from main(), because it is private. Normally
we access private members through public
member functions. While we could create a normal
public member function to access
s_nValue, we’d then need to instantiate an object
of the class type to use the function! We can do
better. In this case, the answer to the problem is
that we can also make member functions static.

Like static member variables, static member


functions are not attached to any particular object.
Here is the above example with a static member
function accessor:
#include<iostream.h>
class Something
{
private:
static int s_nValue;
public:
static int GetValue() { return s_nValue; }
};
int Something::s_nValue = 1; // initializer
void main()
{
cout << Something::GetValue() << endl;
}
Because static member functions are not attached
to a particular object, they can be called directly by
using the class name and the scope operator. Like
static member variables, they can also be called
through objects of the class type, though this is
not recommended.
Static member functions can only access static
member variables. They cannot access non-static
member variables. This is because non-static
member variables must belong to a class object,
and static member functions have no class object
to work with!
Here’s another example using static member
variables and functions:
class IDGenerator
{
private:
    static int s_nNextID;
 
public:
     static int GetNextID() { return s_nNextID++; }
};
 
// We'll start generating IDs at 1
int IDGenerator::s_nNextID = 1;
int main()
{
    for (int i=0; i < 5; i++)
        cout << "The next ID is: " <<
IDGenerator::GetNextID() << endl;
     return 0;
}
This program prints:
The next ID is: 1
The next ID is: 2
The next ID is: 3
The next ID is: 4
The next ID is: 5
Note that because all the data and functions in this
class are static, we don’t need to instantiate an
object of the class to make use of it’s functionality!
This class utilizes a static member variable to hold
the value of the next ID to be assigned, and
provides a static member function to return that ID
and increment it.

You might also like