ABSTRACT CLASS C# LÀ GÌ

C++ abstract class is conceptually a class that cannot be instantiated, và it should be implemented as a class with one or more pure virtual (abstract) functions. A pure virtual function must be overridden by any concrete (i.e., non-abstract) derived class.

Bạn đang xem: Abstract class c# là gì

Before understanding abstract class in C++, it is essential lớn know the concepts of virtual functions as it is an integral part of learning the concept of abstract classes in C++.

We cannot create the instance of an abstract class. However, we can create a pointer và reference base abstract class points khổng lồ the instance of the child class.


Abstract Class in C++

A class is abstract if it has at least one pure virtual function. A pure virtual function in C++ can be stated as the function which is only declared in the base class, and it is defined in the derived classes. In simpler words, it can be stated as the functions which are only declared, và it’s not implemented in the base class. Those functions are known as virtual functions.


Syntax for declaring a virtual function

See the following syntax.

class class_namepublic: virtual return_type function_name() = 0;;See the following example.


class Apublic: virtual void fun_1() = 0;;Now coming to lớn an abstract class, it is a kind of class that has at least one pure virtual function is known as abstract class. In other words, if we only declare the function in the base class và don’t write its implementation in the base class và use derived classes for writing an implementation of the declared function, then that class is known as abstract class.

Properties of abstract classes

To be an abstract class, it must have a presence of at least one virtual class.We can use pointers & references to abstract class types.If we don’t override the virtual function in the derived class, then the derived class also becomes an abstract class.We can create constructors of an abstract class.

Xem thêm: Quy Đổi 1 Vạn Bằng Bao Nhiêu Km, Tiền, 1 Vạn Là Bao Nhiêu

Abstract class in java Vs. Abstract class in C++

If we want to make a class abstract in java, we use the abstract keywords. Similarly, we can make pure virtual functions lớn make the class the class abstract.

In C++, we don’t need to add an abstract keywords as the base class, which has at least one pure virtual function is understood to lớn be an abstract class.

Examples of Abstract Class in C++

Write one program to lớn show the mechanism of the abstract class.

#include using namespace std;class Apublic: virtual void test() = 0; //declaring a virtual function;class B : public A{public: void test() { cout See the following output.

*

Write a program khổng lồ use constructors of the abstract class to lớn find the sum of two numbers and display the results.

#include using namespace std;class A{public: int a, b, c; virtual void test() = 0; A(int a, int b) { cout See the following output.

*

Now let’s write the last program. But, first, it shows that we cannot create the objects of abstract class in C++.

#include using namespace std;class Apublic: virtual void test() = 0;;class B : public A{public: void test() { cout See the following output.