Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.

Similar presentations


Presentation on theme: "Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions."— Presentation transcript:

1 Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions using the pure-specifier (with the syntax “=0”). An abstract class is a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual functions. A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class.

2 Abstract ClassestMyn2 In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. It is a way of forcing a contact between the class designer and the users of that class. If we wish to create a concrete class from an abstract class we must declare and define a matching member function for each abstract member function of the base class. Otherwise we will create a new abstract class.

3 Abstract ClassestMyn3 Sometimes the phrase ”pure abstract class” is used to mean a class that exclusively has pure virtual functions (and no data members). The concept of interface is mapped to pure abstract classes in C++, as there is no construction “interface” in C++.

4 Abstract ClassestMyn4 Since an abstract class contains one or more functions for which there is no definition, no objects can be created using an abstract class. However, you can create pointers to an abstract class. This allows an abstract class to be used as a base class. A pointer to an abstract class can be used to select the proper virtual function.

5 Abstract ClassestMyn5 #include "stdafx.h" #include using namespace System; using namespace std; //Abstract class: class Shape { public: Shape(int, int); //pure virtual function: virtual void draw()=0; protected: int posX, posY; }; Shape::Shape(int pX, int pY) { posX=pX; posY=pY; }

6 Abstract ClassestMyn6 class Rectangle: public Shape { public: Rectangle(int, int); virtual void draw(); }; Rectangle::Rectangle(int xVal, int yVal): Shape(xVal, yVal) {} void Rectangle::draw() { cout<<"Drawing rectangle at ("<<posX <<", "<<posY<<")."<<endl; } class Pentagon: public Shape { public: Pentagon(int, int); virtual void draw(); };

7 Abstract ClassestMyn7 Pentagon::Pentagon(int xVal, int yVal): Shape(xVal, yVal) {} void Pentagon::draw() { cout<<"Drawing pentagon at ("<<posX <<", "<<posY<<")."<<endl; } class Hexagon: public Shape { public: Hexagon(int, int); virtual void draw(); }; Hexagon::Hexagon(int xVal, int yVal): Shape(xVal, yVal) {}

8 Abstract ClassestMyn8 void Hexagon::draw() { cout<<"Drawing hexagon at ("<<posX <<", "<<posY<<")."<<endl; } int main(array ^args) { Shape* first=new Rectangle(5, 15); first->draw(); Shape* second=new Pentagon(6, 16); second->draw(); Shape* third=new Hexagon(7, 17); third->draw(); delete first; delete second; delete third; return 0; }


Download ppt "Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions."

Similar presentations


Ads by Google