Interface in C ++ (Abstract class)

An Interface describes the behavior or capabilities of a class in C ++ without signing to a specific implementation of that class.

An Interface describes the behavior or capabilities of a class in C ++ without signing to a specific implementation of that class.

Interface in C ++ is implemented using abstract classes (Abstract class) and these abstract classes should not be confused with Data Abstraction, but a concept of Implementation Detail segregation with data associated.

A class is created as abstract by declaring at least once its functions to be a pure virtual function. A pure virtual function is defined by setting "= 0" in its declaration, as follows:

 class Box { public : // khai bao pure virtual function virtual double tinhTheTich () = 0 ; private : double chieudai ; // Chieu dai cua mot box double chieurong ; // Chieu rong cua mot box double chieucao ; // Chieu cao cua mot box }; 

The purpose of an abstract class (commonly referred to as ABC) is to provide an appropriate base class from which other classes can inherit. The abstract class cannot be used to initialize objects and only serve as an Interface . The attempt to create an object of an abstract class in C ++ will create a compilation error.

Therefore, if a subclass of an ABC needs to be initialized, it must deploy virtual functions, meaning that it supports Interface declared by ABC. Failure to override a pure virtual function in an inheritance class, the effort to initialize objects of that class, is a compile error.

Classes, which can be used to instantiate objects, are called Concrete Class in C ++.

Example of abstract class in C ++

Consider the following example: the superclass provides an Interface to the base class to deploy a pureDichTich () function in C ++:

 #include using namespace std ; // day la lop co so (base class) class Hinh { public : // khai bao pure virtual function virtual int tinhDienTich () = 0 ; void setChieuRong ( int rong ) { chieurong = rong ; } void setChieuCao ( int cao ) { chieucao = cao ; } protected : int chieurong ; int chieucao ; }; // cac lop ke thua class HinhChuNhat : public Hinh { public : int tinhDienTich () { return ( chieurong * chieucao ); } }; class TamGiac : public Hinh { public : int tinhDienTich () { return ( chieurong * chieucao )/ 2 ; } }; int main ( void ) { HinhChuNhat hcn ; TamGiac tag ; hcn . setChieuRong ( 25 ); hcn . setChieuCao ( 4 ); // in dien tich cua doi tuong cout << "Tong dien tich HinhChuNhat la: " << hcn . tinhDienTich () << endl ; tag . setChieuRong ( 20 ); tag . setChieuCao ( 15 ); // in dien tich cua doi tuong. cout << "Tong dien tich TamGiac la: " << tag . tinhDienTich () << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Picture 1 of Interface in C ++ (Abstract class)

You can see how an abstract class defines an Interface with this planetDienTich () function and the other two classes implement the same function but with different algorithms to calculate the specific area for the image.

Design strategy in C ++

An object-oriented system can use a base class to provide a common and standard Interface suitable for all peripheral applications. Therefore, through inheriting from that abstract base class, inheritance classes are set up in the same way.

Capabilities (for example, public functions), provided by peripheral applications, are provided in the form of pure virtual functions in abstract base classes. The implementation of these pure virtual functions is provided in inheritance classes corresponding to specific application types.

This structure also allows new applications to be added to the system easily, even after the system has been defined.

According to Tutorialspoint

Previous article: Calculating closure in C ++

Next article: Read / write File in C ++ | fstream in c ++

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile