Abstract vs Concrete Classes
• An abstract class is a class for which one or more methods are declared but not defined,
meaning that the compiler knows these methods are part of the class, but not what code to
execute for that method. These are called abstract methods. Here is an example of an
abstract class.
class shape {
public:
virtual void draw() = 0;
};
• This declares an abstract class which specifies that any descendants of the class should
implement the draw method if the class is to be concrete. You cannot instantiate this class
because it is abstract, after all, the compiler wouldn't know what code to execute if you
called member draw. So you can not do the following:
shape my_shape();
my_shape.draw();
To be able to actually use the draw method you would need to derive classes from this abstract class, which do
implement the draw method, making the classes concrete:
class circle : class circle : public shape {
public:
circle(int x, int y, int radius) {
/* set up the circle */
}
virtual draw() {
/* do stuff to draw the circle */
}
};
class rectangle : public shape {
public:
rectangle(int min_x, int min_y, int max_x, int max_y) {
/* set up rectangle */
}
virtual draw() {
/* do stuff to draw the rectangle */
}
};
// concrete vs abstract class
#include <iostream>
using namespace std;
class Shape // abstract class
{
public:
virtual int Area() = 0;
void setWidth(int w) {width = w;}
void setHeight(int h) {height = h;}
protected:
int width;
int height;
};
class Rectangle: public Shape // concrete class
{
public:
int Area() {return (width * height);}
};
int main(void)
{
// Shape shape; // <-- Can't do this
Shape *pShape = new Rectangle(); // <-- Can do this
pShape->setWidth(5);
pShape->setHeight(3);
int area = pShape->Area(); // 15
Rectangle rect; // <-- Can also do this, or assign instance
// to a Rectangle pointer
rect.setWidth(5);
rect.setHeight(3);
area = rect.Area();
return 0;
}

Abstract vs Concrete Classes.pptx

  • 1.
    Abstract vs ConcreteClasses • An abstract class is a class for which one or more methods are declared but not defined, meaning that the compiler knows these methods are part of the class, but not what code to execute for that method. These are called abstract methods. Here is an example of an abstract class. class shape { public: virtual void draw() = 0; }; • This declares an abstract class which specifies that any descendants of the class should implement the draw method if the class is to be concrete. You cannot instantiate this class because it is abstract, after all, the compiler wouldn't know what code to execute if you called member draw. So you can not do the following: shape my_shape(); my_shape.draw();
  • 2.
    To be ableto actually use the draw method you would need to derive classes from this abstract class, which do implement the draw method, making the classes concrete: class circle : class circle : public shape { public: circle(int x, int y, int radius) { /* set up the circle */ } virtual draw() { /* do stuff to draw the circle */ } }; class rectangle : public shape { public: rectangle(int min_x, int min_y, int max_x, int max_y) { /* set up rectangle */ } virtual draw() { /* do stuff to draw the rectangle */ } };
  • 3.
    // concrete vsabstract class #include <iostream> using namespace std; class Shape // abstract class { public: virtual int Area() = 0; void setWidth(int w) {width = w;} void setHeight(int h) {height = h;} protected: int width; int height; }; class Rectangle: public Shape // concrete class { public: int Area() {return (width * height);} };
  • 4.
    int main(void) { // Shapeshape; // <-- Can't do this Shape *pShape = new Rectangle(); // <-- Can do this pShape->setWidth(5); pShape->setHeight(3); int area = pShape->Area(); // 15 Rectangle rect; // <-- Can also do this, or assign instance // to a Rectangle pointer rect.setWidth(5); rect.setHeight(3); area = rect.Area(); return 0; }