- Examples

In this lesson, we'll discuss the examples of final and override.

Example 1: Override final #

Press + to interact
#include <iostream>
class Sort{
public:
virtual void processData(){
readData();
sortData();
writeData();
}
private:
virtual void readData(){}
virtual void sortData() = 0;
virtual void writeData(){}
};
class QuickSort: public Sort{
private:
void readData(){
std::cout << "readData" << std::endl;
}
void sortData(){
std::cout << "sortData" << std::endl;
}
void writeData(){
std::cout << "writeData" << std::endl;
}
};
int main(){
std::cout << std::endl;
Sort* sort = new QuickSort;
sort->processData();
std::cout << std::endl;
}

Explanation #

  • We have implemented two classes named Sort and QuickSort.

  • We have created three private virtual methods and a public virtual method processData in the Sort ...