30.1 Static variable
Write a program that checks how many times a function was called from the main program. To do this, we will use a static variable inside a function which will be incremented each time the function is called in main():
#include <iostream>
void myfunction()
{
static int counter = 0;
counter++;
std::cout << "The function is called " << counter << " time(s)." << '
';
}
int main()
{
myfunction();
myfunction();
for (int i = 0; i < 5; i++)
{
myfunction();
}
}
30.2 Static data member
Write a program that defines a class with one static data member of type std::string. Make the data member public. Define the static data member outside the class. Change the static data member value from the main() function:
#include <iostream>
#include <string>
class MyClass
{
public:
static std::string name;
};
std::string MyClass::name = "John Doe";
int main()
{
std::cout << "Static data member value: " << MyClass::name << '
';
MyClass::name = "Jane Doe";
std::cout << "Static data member value: " << MyClass::name << '
';
}
30.3 Static member function
Write a program that defines a class with one static member function and one regular member function. Make the functions public. Define both member functions outside the class. Access both functions in the main():
#include <iostream>
#include <string>
class MyClass
{
public:
static void mystaticfunction();
void myfunction();
};
void MyClass::mystaticfunction()
{
std::cout << "Hello World from a static member function." << '
';
}
void MyClass::myfunction()
{
std::cout << "Hello World from a regular member function." << '
';
}
int main()
{
MyClass::mystaticfunction();
MyClass myobject;
myobject.myfunction();
}
30.4 Function Template
Write a program that defines a template for a function that sums two numbers. Numbers are of the same generic type T and are passed to function as arguments. Instantiate the function in main() using int and double types:
#include <iostream>
template <typename T>
T mysum(T x, T y)
{
return x + y;
}
int main()
{
int intresult = mysum<int>(10, 20);
std::cout << "The integer sum result is: " << intresult << '
';
double doubleresult = mysum<double>(123.456, 789.101);
std::cout << "The double sum result is: " << doubleresult << '
';
}
30.5 Class Template
Write a program that defines a simple class template that has one data member of a generic type, a constructor, a getter function of a generic type, and a setter member function. Instantiate a class in the main() function for int and double types:
#include <iostream>
template <typename T>
class MyClass
{
private:
T x;
public:
MyClass(T xx)
: x{ xx }
{}
T getx() const
{
return x;
}
void setx(T ax)
{
x = ax;
}
};
int main()
{
MyClass<int> o{123};
std::cout << "The value of the data member is: " << o.getx() << '
';
o.setx(456);
std::cout << "The value of the data member is: " << o.getx() << '
';
MyClass<double> o2{ 4.25 };
std::cout << "The value of the data member is: " << o2.getx() << '
';
o2.setx(6.28);
std::cout << "The value of the data member is: " << o2.getx() << '
';
}
30.6 Scoped Enums
Write a program that defines a scoped enum representing days of the week. Create an object of that enum, assign it a value, check if the value is Monday, if it is, change the object value to another enum value:
#include <iostream>
enum class Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main()
{
Days myday = Days::Monday;
std::cout << "The enum value is now Monday." << '
';
if (myday == Days::Monday)
{
myday = Days::Friday;
}
std::cout << "Nobody likes Mondays. The value is now Friday.";
}
30.7 Enums in a switch
Write a program that defines an enum. Create an object of that enum as use it in a switch statement. Use the switch statement to print the value of an object:
#include <iostream>
enum class Colors
{
Red,
Green,
Blue
};
int main()
{
Colors mycolors = Colors::Green;
switch (mycolors)
{
case Colors::Red:
std::cout << "The color is Red." << '
';
break;
case Colors::Green:
std::cout << "The color is Green." << '
';
break;
case Colors::Blue:
std::cout << "The color is Blue." << '
';
break;
default:
break;
}
}