Implementing Pure Virtual Functions in C++
By: Reema sen in C++ Tutorials on 2007-09-17
Typically, the pure virtual functions in an abstract base class are never implemented. Because no objects of that type are ever created, there is no reason to provide implementations, and the ADT works purely as the definition of an interface to objects which derive from it.
It is possible, however, to provide an implementation to a pure virtual function. The function can then be called by objects derived from the ADT, perhaps to provide common functionality to all the overridden functions. Listing below, this time with Shape as an ADT and with an implementation for the pure virtual function Draw(). The Circle class overrides Draw(), as it must, but it then chains up to the base class function for additional functionality.
In this example, the additional functionality is simply an additional message printed, but one can imagine that the base class provides a shared drawing mechanism, perhaps setting up a window that all derived classes will use.
Implementing pure virtual functions.
1: //Implementing pure virtual functions 2: 3: #include <iostream.h> 4: 5: enum BOOL { FALSE, TRUE }; 6: 7: class Shape 8: { 9: public: 10: Shape(){} 11: ~Shape(){} 12: virtual long GetArea() = 0; // error 13: virtual long GetPerim()= 0; 14: virtual void Draw() = 0; 15: private: 16: }; 17: 18: void Shape::Draw() 19: { 20: cout << "Abstract drawing mechanism!\n"; 21: } 22: 23: class Circle : public Shape 24: { 25: public: 26: Circle(int radius):itsRadius(radius){} 27: ~Circle(){} 28: long GetArea() { return 3 * itsRadius * itsRadius; } 29: long GetPerim() { return 9 * itsRadius; } 30: void Draw(); 31: private: 32: int itsRadius; 33: int itsCircumference; 34: }; 35: 36: void Circle::Draw() 37: { 38: cout << "Circle drawing routine here!\n"; 39: Shape::Draw(); 40: } 41: 42: 43: class Rectangle : public Shape 44: { 45: public: 46: Rectangle(int len, int width): 47: itsLength(len), itsWidth(width){} 48: ~Rectangle(){} 49: long GetArea() { return itsLength * itsWidth; } 50: long GetPerim() {return 2*itsLength + 2*itsWidth; } 51: virtual int GetLength() { return itsLength; } 52: virtual int GetWidth() { return itsWidth; } 53: void Draw(); 54: private: 55: int itsWidth; 56: int itsLength; 57: }; 58: 59: void Rectangle::Draw() 60: { 61: for (int i = 0; i<itsLength; i++) 62: { 63: for (int j = 0; j<itsWidth; j++) 64: cout << "x "; 65: 66: cout << "\n"; 67: } 68: Shape::Draw(); 69: } 70: 71: 72: class Square : public Rectangle 73: { 74: public: 75: Square(int len); 76: Square(int len, int width); 77: ~Square(){} 78: long GetPerim() {return 4 * GetLength();} 79: }; 80: 81: Square::Square(int len): 82: Rectangle(len,len) 83: {} 84: 85: Square::Square(int len, int width): 86: Rectangle(len,width) 87: 88: { 89: if (GetLength() != GetWidth()) 90: cout << "Error, not a square... a Rectangle??\n"; 91: } 92: 93: int main() 94: { 95: int choice; 96: BOOL fQuit = FALSE; 97: Shape * sp; 98: 99: while (1) 100: { 101: cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: "; 102: cin >> choice; 103: 104: switch (choice) 105: { 106: case 1: sp = new Circle(5); 107: break; 108: case 2: sp = new Rectangle(4,6); 109: break; 110: case 3: sp = new Square (5); 111: break; 112: default: fQuit = TRUE; 113: break; 114: } 115: if (fQuit) 116: break; 117: 118: sp->Draw(); 119: cout << "\n"; 120: } 121: return 0; 122: } Output: (1)Circle (2)Rectangle (3)Square (0)Quit: 2 x x x x x x x x x x x x x x x x x x x x x x x x Abstract drawing mechanism! (1)Circle (2)Rectangle (3)Square (0)Quit: 3 x x x x x x x x x x x x x x x x x x x x x x x x x Abstract drawing mechanism! (1)Circle (2)Rectangle (3)Square (0)Quit: 0
Analysis: On lines 7-16, the abstract data type Shape is declared, with all three of its accessor methods declared to be pure virtual. Note that this is not necessary. If any one were declared pure virtual, the class would have been an ADT.
The GetArea() and GetPerim() methods are not implemented, but Draw() is. Circle and Rectangle both override Draw(), and both chain up to the base method, taking advantage of shared functionality in the base class.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments