C++: An experiment on a static variable

This entry is part 47 of 61 in the series C++

Here, we create an Account class and create several instances of the class.  On each call to the constructor, the static variable is incremented.

Though we have a few instances of the same object, as we have defined the variable as static, we are actually incrementing the same variable.

#include <cstdlib>
#include <iostream>

using namespace std;

class Account {
public:

    static int interestRate;

    static int rate() {

        interestRate++;
        
        return interestRate;

    }

    Account(int yr) {
        
        year = yr;
        
        cout << interestRate++ << endl;
        cout << interestRate++ << endl;
        cout << "Constructor called" << endl;
    }

private:

    int year;

};

int Account::interestRate = 10;

int main(int argc, char** argv) {

    Account A1(12);
    Account A2(12);
    Account A3(12);
    
    cout << Account::rate() << endl;
    cout << Account::rate() << endl;   
    cout << Account::rate() << endl;

    return 0;
}

static2

C++: Static variable

This entry is part 46 of 61 in the series C++

Let’s consider the interest rate of a bank.  If the interest rate of a bank is updated today, all calculations should use the new value immediately.

In an OOP, this can be done by declaring a static variable of a class.  If the value of this static variable is updated, all values of its related instances will be updated to the new value.

So, in a way, there is only one interestRate object that will be shared by all the Account objects.

To access the static variable, we use the scope operator :: as shown in the example below.  A static variable can only be assessed by a static function.

#include <cstdlib>
#include <iostream>

using namespace std;

class Account {
public:

    static double rate() {
        return interestRate;
    }
    static double interestRate;
};

double Account::interestRate = 12;

int main(int argc, char** argv) {
    
    cout << Account::rate();

    return 0;
}

static

C++: Inheritance and virtual method

This entry is part 45 of 61 in the series C++

In an Object-Oriented program, a Virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same name.

This concept is an important part of the polymorphism or specialization portion of object-oriented programming (OOP).

#include <iostream>
#include <string.h>

using namespace std;

class Music {
public:

    Music(int iAge, char* sName) {
        this->iAge = iAge;
        strcpy(this->sName, sName);
    }

    virtual char* WhoAmI() {
        return "I am a musician";
    }
private:
    int iAge;
    char sName[32];
};

class Rock : public Music {
public:

    Rock(int iAge, char* sName, char* latestAlbum)
    : Music(iAge, sName) {
        strcpy(this->latest, latestAlbum);
    }

    virtual char* WhoAmI() {
        return "I am a rock star";
    }
private:
    char latest[32];
};

class Country : public Music {
public:

    Country(int iAge, char* sName, char* liveConcert)
    : Music(iAge, sName) {
        strcpy(this->live, liveConcert);
    }

    virtual char* WhoAmI() {
        return "I am a country singer";
    }
private:
    char live[32];
};

int main(int argc, char* argv[]) {

    Music musician(26, "John");
    cout << musician.WhoAmI() << endl;

    Rock rocker(35, "Mary", "In A Band");
    cout << rocker.WhoAmI() << endl;

    Country countrySinger(22, "Sandra", "Live performance");
    cout << countrySinger.WhoAmI() << endl;

    return 0;
}

 

What is important for us to know is that when you declare an object, its constructor is being called.

When you declare an object of a derived class, the constructor from the base class is called first, and the constructor from the derived class is called later.

Rock(int iAge, char* sName, char* latestAlbum)
    : Music(iAge, sName) {
        strcpy(this->latest, latestAlbum);
    }

virtual