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

 

Java: A simpler abstract class example

This entry is part 49 of 54 in the series Learn Java

An abstract class provides a contract between a service provider and its clients, but at the same time an abstract class can provide partial implementation.

Methods that must be explicitly overridden can be declared abstract.

package javaapplication41;

public class MainMusic {

    public abstract class Music {

        public abstract int live();            
        
    }

    static class Rock extends Music {

        protected int ls;                              

        public Rock(int l) {
            this.ls = l;
        }          

        public int yearly() {
            return ls * 52;
        }          

        public int live() {
            return ls;
        }          
    }

    public static void main(String[] args) {

        Rock r = new Rock(5);

        System.out.println(r.yearly());
        System.out.println(r.live());
    }
}

abstract1

Java: Abstract class

This entry is part 47 of 54 in the series Learn Java

Java lets us define a method without implementing it by declaring the method with the abstract modifier.

An abstract method has no body; it simply has a signature definition followed by a semicolon.

package javaapplication41;

public class MainShape {

    public abstract class Shape {

        public abstract double area();            // Abstract methods: note

        public abstract double circumference();   // semicolon instead of body.
    }

    static class Circle extends Shape {

        public static final double PI = 3.14159265358979323846;
        protected double r;                              // Instance data

        public Circle(double r) {
            this.r = r;
        }          // Constructor

        public double getRadius() {
            return r;
        }          // Accessor

        public double area() {
            return PI * r * r;
        }          // Implementations of

        public double circumference() {
            return 2 * PI * r;
        } // abstract methods.
    }

    static class Rectangle extends Shape {

        protected double w, h;                               // Instance data

        public Rectangle(double w, double h) {               // Constructor
            this.w = w;
            this.h = h;
        }

        public double getWidth() {
            return w;
        }               // Accessor method

        public double getHeight() {
            return h;
        }              // Another accessor

        public double area() {
            return w * h;
        }                 // Implementation of

        public double circumference() {
            return 2 * (w + h);
        }  // abstract methods
    }

    public static void main(String[] args) {

        Shape[] shapes = new Shape[3];

        shapes[0] = new Circle(2.0);
        shapes[1] = new Rectangle(1.0, 3.0);
        shapes[2] = new Rectangle(4.0, 2.0);

        double totalArea = 0;
        for (int i = 0; i < shapes.length; i++) {
            totalArea += shapes[i].area();
            System.out.println(totalArea);
        }
    }
}

abstract

C#: Abstract class

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

An abstract class in C# is a class without implementation or methods.  As there is no method, you will have to develop the member functions in the derived class.

What is the purpose? You might ask.

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

In a way, creating abstract class is good for organizing code and versionning.  If new parameters in the abstract class declaration are needed, you can always modify the relevant declaration without breaking the code.

using System;

namespace Abstract
{
    public class Music
    {
        public virtual void Display(int i)
        {
            Console.WriteLine("Original implementation {0}", i);
        }
    }

    public abstract class Rock : Music
    {
        public abstract override void Display(int i);
    }

    public class Metal : Rock
    {

        public override void Display(int i)
        {
            Console.WriteLine("New implementation {0}", i);
        }
    }

    class Program
    {
        static void Main()
        {

            Metal metal = new Metal();
            int i = 10;

            metal.Display(i);
            Console.ReadKey();
        }
    }
}

Notice the Display() method in line 15.  As there is no implementation, the declaration of the abstract method ends with a semicolon without a block of code.

public abstract override void Display();

An abstract class is actually a virtual class and it has to be overridden. Also, you cannot create an instance of an abstract class.