Various C++ Examples (including IPC)  Version: 1.0.0
virtuals.cc
Go to the documentation of this file.
1 
5 #include <iostream>
6 
7 using namespace std;
8 
11 {
12 public:
13  //demanding polymorphic capability
14  virtual void print() = 0;
15 };
16 
18 class BaseClass : public AbstractClass
19 {
20 public:
21  //allowing polymorphic behavior from derived classes
22  //the virtual keyword is superfluious here
23  virtual void print()
24  { std::cout << "base class" << std::endl; }
25 };
26 
28 class ChildClass : public BaseClass
29 {
30 public:
31  //taking advantage of polymorphism by overriding parrent function
32  void print()
33  { std::cout << "child class" << std::endl; }
34 };
35 
38 {
39 public:
40  //do NOT take advantage of polymorphism
41  //void print()
42  //{ std::cout << "child class" << std::endl; }
43 };
44 
47 {
48  //call the appropriat function (based on the actual type of
49  //BaseClass -be it derived or not)
50  p.print();
51 }
52 
54 int main(int argc, char **argv)
55 {
56  BaseClass b;
57  ChildClass c;
59 
60  //concrete behavior
61  callPrint(b);
62 
63  //polymorphic behavior
64  callPrint(c);
65 
66  //concrete (hiarchial) behavior
67  callPrint(a);
68 }
void print()
Definition: virtuals.cc:32
another example of ChildClass (inherits from BaseClass)
Definition: virtuals.cc:37
void callPrint(AbstractClass &p)
print something
Definition: virtuals.cc:46
virtual void print()
Definition: virtuals.cc:23
class that inherits from BaseClass -> AbstractClass
Definition: virtuals.cc:28
class that inherits from AbstractClass
Definition: virtuals.cc:18
virtual void print()=0
int main(int argc, char **argv)
demonstrate virtual functions and abstract classes
Definition: virtuals.cc:54
A basic abstract Class (that is inherited from)
Definition: virtuals.cc:10