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++: Pointers in class

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

Pointers in C allow dynamic memory allocation.  In a dynamic variable like a pointer, the compiler will not need to pre-allocate a chunk of memory.  Memory is allocated when needed during run time.

An example of a predefined memory allocation is in declaring an array where a fix set of memory will be allocated for the variable.

The same goes to defining pointers for a class.  The memory space will be allocated dynamically in real time when the program is running.  Now you do not have to know the memory needed in advance.

#include <iostream>
#include <string>

using namespace std;

class Rock {
public:
    int Show();
    
};

int Rock::Show() {

    cout << "Wonderwall by Oasis";
    cout << endl;
    return 0;
}
 
int main() {

    Rock *ptrRock; // instance as a pointer declaration
    ptrRock->Show();
    return 0;
}

pointer

C++: Declaring an object instance after a class scope

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

In C++, a class instance can be declared right after the class scope.  You might have notice that this method was used in the previous example.

As shown in the example below, music, rock and and rock1 instance variable were declared right after the class scope.

#include <iostream>

using namespace std;

class Music {
public:

    void setTop10(int i) {
        prot_Top10 = i;
    }

    void Display() {
        cout << "No of Top 10: " << prot_Top10 << endl;
    }

protected:

    int prot_Top10;

    void Protband() {

        prot_Top10 = 8;
        cout << "No of Top 10: " << prot_Top10 << endl << endl;
    }

} music;

class Rock : public Music {
public:

    void useProtBand() {
        Protband();
    }

} rock, rock1;

int main() {

    music.setTop10(2); 
    music.Display(); 

    rock.setTop10(5); 
    rock.Display(); 

    rock.useProtBand(); 

    rock1.setTop10(6);
    rock1.Display();

}

classinstance