C++: Static variable

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

A static variable takes its first initiated value and remains unchanged.

#include "stdafx.h"

#include <iostream>

using namespace std;

void main()
{
	for (int i = 2; i<5; ++i)
	{
		static int a = i;
		cout << a << endl;
	}

	system("pause");
}

static

C++: Addition of 2 numbers

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

Addition of 2 numbers, using system(“pause”) to let the console stay.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int a, b, c;

	cout << "Enter two numbers to add\n";
	cin >> a >> b;

	c = a + b;
	cout << "Sum of entered numbers = " << c << endl;

	system("pause");
	return 0;
}

add

C++: Friend Class

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

A class can be allowed to access the private and protected members of another class by declaring the class a friend.

The friend is allowed to access all members in the class where the friend is defined, but not the other way around.

Consider the following example.

In the class Music, we declare Rock to be a friend of Music.  Now Rock class could access the private members of Music such as singer and title.

#include <iostream>

using namespace std;

class Music {
public:

    void setSinger(string s) {
        singer = s;
    }

    void setTitle(string t) {
        title = t;
    }

protected:
    string singer;
    string title;

    friend class Rock;
};

class Rock {
public:

    int Print(Music m) {

        cout << m.singer << " singing " << m.title;
        return 0;
    }
};

int main(void) {

    Rock rock;
    Music music;

    music.setSinger("Rod Stewart");
    music.setTitle("Infactuation");

    rock.Print(music);

    return 0;
}

friendclass

C++: Friend functions

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

Sometimes, you might have an independent function and you might want this function to access the private members of a class.

You can declare this independent function as friend of the class. A friend can access any of the members of a class object, regardless of their access specification.

In the example below, we have an independent function called CurrentWeek.  We have declared CurrentWeek within the Album class that it is a friend function.

With this declaration, the function can access the private variable top10 of the Album class.

#include <iostream>

using namespace std;

class Album {
    
private:
    int top10;

public:

    Album(void) : top10(0) {

    }

    void show(void) {

        cout << top10 << endl;
    }

    friend void CurrentWeek(Album &);

};

void CurrentWeek(Album &alb) {

    alb.top10 = alb.top10 + 5;
}

int main() {

    Album album;
    album.show();

    CurrentWeek(album);
    album.show();

}

friend

C++: Pure virtual function turns a class into abstract class

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

In C++, once you have a pure function in a class, you turn that class into an abstract class.  There is no need to define a class as abstract.

This is unlike other programming languages such as C# shown in this post here.

Abstract class in OOP is meant for us to define a general or generic class so that we can derive a more specific class from it.

There is no actual method implementation in the class.  In our example here, Music class has a pure virtual function and so it is now an abstract class.

The actual implementation of the display method is done by the derived class Rock or Country.

#include <iostream>

using namespace std;

class Music {
public:
    virtual void display() = 0;  //Pure virtual function
    
};

class Rock : public Music {
public:
    void display(); // OVERRIDE!
    
};

class Country : public Music {
public:
    void display(); // OVERRIDE!

};

void Rock::display() {

    cout << "Janie Got A Gun By Aerosmith";

}

void Country::display() {
    cout << "Strawberry Wine by Deana Carter";
}

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

    Rock rock;
    Country country;

    rock.display();
    cout << endl;
    country.display();

    return 0;
    
}

abstract