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

 

C++: Pure virtual function

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

In the previous post, the Music class has a method called display.  This base class has its own implemention of display() method but it is an implementation that did nothing.

We can declare the Music class to have no implementation at all.  This is done by using pure virtual function, with the syntax.

virtual void display() = 0;

A class with at least one pure virtual function is an abstract class. Such a class cannot be instantiated, meaning that you cannot use it to declare objects, at least not directly.

We will talk about abstract class in the later post.

#include <iostream>

using namespace std;

class Music {
public:
    virtual void display() = 0;

};

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;

    Music *ptr_music;

    ptr_music = &rock;
    ptr_music->display(); // Call Rock::display()
    cout << endl;
    ptr_music = &country;
    ptr_music->display(); // Call Country::sound()

    return 0;

}

pure

C++: Pointer to access methods in derived classes

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

Since we have gone through one example on a pointer, we make some modifications to our earlier example to use pointer.

We have declared only one ptr_music and with the right referencing, we can use the pointer to access the methods in the derived classes.

When you declare a method as virtual, you are basically setting it up to be overridden by derived classes.

#include <iostream>

using namespace std;

class Music {
public:
    virtual void display();  //setting it up to be overridden
    
};

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

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

};

void Music::display() {
}

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) {

    Music music;
    
    Rock rock;
    Country country;

    Music *ptr_music;

    ptr_music = &rock;
    ptr_music->display(); // Call Rock::display()
    cout << endl;
    ptr_music = &country;
    ptr_music->display(); // Call Country::display()

    return 0;
    
}

pointer2

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

C#: Virtual method

This entry is part 57 of 71 in the series C#

When you declare a method as virtual, you are basically laying the path for it to be overridden.

By default, or if there is no declaration, methods are non-virtual. Only virtual methods can be overridden.

using System;
class Music
{
    public virtual void Play()
    {
        Console.WriteLine("I am virtual and I will be overidden.");
    }
}
class Pop : Music
{
    public override void Play()
    {
        Console.WriteLine("My parent method is a virtual method and so I will be printed out.");
    }
}
class Test
{
    static void Main()
    {
        Pop pop = new Pop();

        pop.Play();
        Console.ReadKey();
    }
}

When you declare a member as virtual method,  this method can be changed by an overriding member in a derived class.

During run time, the program will look for a method in the most derived class for a similar method with the override keyword.

When pop.Play() in line 22 is invoked, the method with the override keyword will be called.

virtual