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.

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:

Interface in C ++ (Abstract class) Picture 1

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 ++

4 ★ | 1 Vote

May be interested

  • How to Write a Class in PythonHow to Write a Class in Python
    in python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
  • Contructor and Destructor in C ++Contructor and Destructor in C ++
    a contructor class is a special member function of a class that is executed whenever we create new objects of that class.
  • Learn Class and Object in PythonLearn Class and Object in Python
    python is an object-oriented programming language. unlike procedural programming, which emphasizes functions, object-oriented programming focuses on objects. this article quantrimang will explore class and object in depth, inviting readers to follow.
  • Class member functions in C ++Class member functions in C ++
    a member function of a class is a function that has its definition or prototype inside the class definition like any other variable. it works on any object of the class that is a member, and has access to all members of a class for that object.
  • How to use abstract images to create phone wallpapersHow to use abstract images to create phone wallpapers
    you can create your own phone wallpapers from abstract photos. not only are they beautiful, but the process of creating them is quite fun.
  • Class properties in HTMLClass properties in HTML
    in html, class properties are used to identify one or more class names for html elements.
  • 9 Best USB Audio Interface9 Best USB Audio Interface
    if you want to add real-world instruments or voices to your computer's recordings, you'll need an audio interface to record them. the usb audio interface can cost you a lot of money, but depending on your needs, affordable options are also worth considering.
  • Class (class) and Object in C ++Class (class) and Object in C ++
    the main purpose of c ++ is to add object orientation to the c programming language and classes which are central features of c ++ that support object-oriented programming and are often called types user defined (user-defined).
  • Managing Windows networks using Script - Part 13: The script returns all valuesManaging Windows networks using Script - Part 13: The script returns all values
    in the previous article of this series we came up with a script called displayclassproperties.vbs, which shows the names of the properties of the wmi class. this is what the script content is, by using win32_bootconfiguration as a class, we are connecting to the wmi nickname.
  • Static member of class in C ++Static member of class in C ++
    we can define class members as static using the static keyword in c ++. when we declare a member of a class that is static, that is, no matter how many objects of the class are created, there will only be one copy of the static member.