![]() ![]() In the virtual function, the derived class overrides the function of the base class it is the case of the function overriding. A virtual function and a pure virtual function are both declared with the word "virtual" at the beginning of the code declaration, but their syntax is different. The following are the differences between a virtual and a pure virtual function in C++. Virtual Function vs Pure Virtual Function ![]() Std ::cout << "The area of circle is: " <<a3 << std ::endl Īfter going through the above, you can observe that we define a shape class as a base, then we are deriving classes from it and using the member function of the base class to calculate the area, the important thing to note here is that we are efficiently using the pure virtual function cal_area.īut how? See we are not using multiple functions in every derived class to calculate the area rather we are using just the different implementations of it by defining it once in the base class, I hope this will put you in a state where you have found yourself more powerful as you have this craziest tool to use it in your C++ program now. Std ::cout << "The area of rectangle is: " <<a2 << std ::endl Std ::cout << "The area of square is: " <<a1 << std ::endl Return l *b // returns the product of length and breadth Public : virtual int cal_Area() = 0 // cal_Area is a pure virtual function Welcome to FavTutor #include using namespace std Given below is the way to declare the pure virtual functions in C++ : Now we know theoretically, what are important points to keep in mind about the pure virtual functions to define, then let's just quickly have a look at its syntax as well. Note that classes having at least one pure virtual function are called Abstract classes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |