Solved

c++ polymorphism function

Posted on 2006-11-30
3
196 Views
Last Modified: 2010-04-01
Hello, I found the following code from one of the experts here, but I don't understand a lot of it.  Could someone please help me understand what the program is doing?  I understand the basics, but need help with understanding these lines.  I am not sure what teh ~ means.  Please help me understand.  

class Shape
{
public:
     Shape() {};
     virtual ~Shape() {};
     virtual void DrawShape() = 0;
};

class Circle : public Shape
{
public:
     Circle() {};
     virtual ~Circle() {};
     virtual void DrawShape() { cout << "This is a circle" << endl; }
};




-----------------------------------------------
Here's the code someone wrote:
-----------------------------------------------
#include <iostream>

using namespace std;

class Shape
{
public:
     Shape() {};
     virtual ~Shape() {};
     virtual void DrawShape() = 0;
};

class Circle : public Shape
{
public:
     Circle() {};
     virtual ~Circle() {};
     virtual void DrawShape() { cout << "This is a circle" << endl; }
};

class Triangle : public Shape
{
public:
     Triangle() {};
     virtual ~Triangle() {};
     virtual void DrawShape() { cout << "This is a triangle" << endl; }
};

class Rectangle : public Shape
{
public:
     Rectangle() {};
     virtual ~Rectangle() {};
     virtual void DrawShape() { cout << "This is a rectangle" << endl; }
};

int main()
{
     Shape* pShape = 0;
     int ask = 0;
     cout <<"=============================================="<<endl;
     cout << "Do you want to display (type 1,2,or 3):" <<endl;
     cout << "1. Circle"                                             <<endl;
     cout << "2. Triangle"                                   <<endl;
     cout << "3. Rectangle"                                             <<endl;
     cout <<"----------------------------------------------"<<endl;
     cin >> ask;
       cin.get();
     
     switch (ask)
     {    
     case 1:
          pShape = new Circle;
          break;
              cin.get();
     case 2:
          pShape = new Triangle;
          break;
              cin.get();
     case 3:
          pShape = new Rectangle;
          break;
              cin.get();
     default:
          cout << "Invalid choice.  Good-bye!" << endl;
              cin.get();
          return -1;
             
     }
     pShape->DrawShape();
     delete pShape;
       cin.get();

     return 0;
}
0
Comment
Question by:meaar
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 18051859
Hi meaar,

~<functionName>
Where functionName is same as class name signifies destructor ...

Class Shape has 3 members ... a constructor which is empty and does nothing ...
A virtual destructor .... why we need a virtual destructor
http://blogs.msdn.com/oldnewthing/archive/2004/05/07/127826.aspx
http://www.codersource.net/cpp_virtual_destructors.html

And third member is a pure virtual function DrawShape() .. Note the =0. Pure virtual function is one whose implementation is mandatory for every class that derives from Shape class ...

Class Circle derives from Shape ... again an empty constructor and a virtual destructor ...
Since it derives from Shape, it provides an implementation for DrawShape - the pure virtual function in parent class ..

Now if you have
Shape * ptr;
Circle obj1;

ptr = &obj1;
ptr->DrawShape(); //this call will print "this is a circle"

Cheers!
sunnycoder
0
 

Author Comment

by:meaar
ID: 18051874
sunnycoder, your answer was nice you had to send it twice. :)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 18051974
:) ...

It reported failure on first attempt which as I discovered later was successful.

Deleted the duplicate post.

sunnycoder
Page Editor
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ finding a sting in a char* string from a text file 3 122
Grammars for C C++ and java 1 131
VS2015 compilation and missing DLLs 9 164
learn programming 8 42
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question