I am learning C++, but I am confused about abstract class and concrete class. Some real world examples would be appreciated.
An abstract class is one that has one or more pure virtual functions. A concrete class has no pure virtuals. An abstract class can be either a base class or a derived class (it is derived from another abstract class). A concrete class can also be either base or derived.
An abstract class can be a child of a higher abstract class. A selection on the Class form determines whether a class is concrete or abstract. Concrete classes usually appear near or at the bottom (leaf nodes) of the class hierarchy, but may appear anywhere below the top level. Hope this helps!
A concrete class is a class that has an implementation for all of its methods. They cannot have any unimplemented methods. It can also extend an abstract class or implement an interface as long as it implements all their methods. It is a complete class and can be instantiated.
abstract class a type of class that object can not be create it contain abstract or not abstract method while abstraction is mechanism of data hiding......... simply , abstract class implements abstraction for hiding complexity . Abstract class is a class with abstract methods & non abstract methods .
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 : 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 */ } };
Now you can instantiate the concrete objects circle and rectangle and use their draw methods:
circle my_circle(40, 30, 10); rectangle my_rectangle(20, 10, 50, 15); my_circle.draw(); my_rectangle.draw();
Now of course the question is, why would you want to do this? Couldn't you just as well have defined the circle and rectangle classes and have done away with the whole shape class? You could, but then you wouldn't be able to take advantage of their inheritance:
std::vector<shape*> my_scene; my_scene.push_back(new circle(40, 30, 10)); my_scene.push_back(new rectangle(20, 10, 50, 15)); std::for_each(my_scene.begin(), my_scene.end(), std::mem_fun_ref(&shape::draw)
This code let's you collect all your shapes into one container. This makes it a lot easier if you have a lot of shapes and many different shapes in your scene. For example we can now draw all the shapes in one go, and the code that does so doesn't even need to know about the different types of shapes we have.
Now finally we need to know why the draw function of shape is abstract, and not just an empty function, i.e. why didn't we just define:
class shape { public: virtual void draw() { /* do nothing */ } };
The reason for this is that we don't really want objects of type shape, they wouldn't be real things anyway, they would be abstract. So it doesn't make any sense to define an implementation for the draw method, even an empty one. Making the shape class abstract prevents us from mistakenly instantiating the shape class, or mistakenly calling the empty draw function of the base class instead of the draw function of the derived classes. In effect we define an interface for any class that would like to behave like a shape, we say that any such class should have a draw method that looks like we have specified it should.
To answer you last question, there isn't any such thing as a 'normal derived class' every class is either abstract or concrete. A class that has any abstract methods is abstract, any class that doesn't is concrete. It's just a way to differentiate the two types of classes. A base class can be either abstract or concrete and a derived class can be either abstract or concrete:
class abstract_base { public: virtual void abstract_method1() = 0; virtual void abstract_method2() = 0; }; class concrete_base { public: void concrete_method1() { /* do something */ } }; class abstract_derived1 : public abstract_base { public: virtual void abstract_method3() = 0; }; class abstract_derived2 : public concrete_base { public: virtual void abstract_method3() = 0; }; class abstract_derived3 : public abstract_base { public: virtual abstract_method1() { /* do something */ } /* note that we do not provide an implementation for abstract_method2 so the class is still abstract */ }; class concrete_derived1 : public concrete_base { public: void concrete_method2() { /* do something */ } }; class concrete_derived2 : public abstract_base { public: virtual void abstract_method1() { /* do something */ } virtual void abstract_method2() { /* do something */ } /* This class is now concrete because no abstract methods remain */ };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With