Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Design Strategy in C ++: Interface in C ++ (Abstract Class)

Understand Design Strategy in C ++: Interface in C ++ (Abstract Class) with clear explanations, practical examples, and useful tips. This updated guide...

Table of Contents

Design Strategy in C ++: Interface in C ++ (Abstract Class) is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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 help instantiate objects, are called Concrete Class in C ++.

Example of abstract class in C ++

Consider the example below: 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:

Design Strategy in C ++: Interface in C ++ (Abstract Class) example image 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.

FAQ

What should you know about example of abstract class in C ++?

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

What should you know about 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.

What is Design Strategy in C ++: 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.