Polymorphism Problem

This is a discussion on Polymorphism Problem within the C++ Programming forums, part of the General Programming Boards category; I was writing this simple polymorphism program: Shape.h Code: #ifndef SHAPE_H #define SHAPE_H #include <string> using namespace std; class Shape ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Polymorphism Problem

    I was writing this simple polymorphism program:

    Shape.h
    Code:
    #ifndef SHAPE_H
    #define SHAPE_H
    #include <string>
    
    using namespace std;
    
    class Shape
    {
    public:
    	virtual double getArea() const;
    	virtual double getVolume() const;
    	virtual string getName() const = 0;
    	virtual void print() const = 0;
    };
    
    #endif
    Shape.cpp
    Code:
    #include <iostream>
    #include "Shape.h"
    
    using namespace std;
    
    double getArea() const
    {
    	return 0.0;
    }
    
    double getVolume() const
    {
    	return 0.0;
    }
    When I compile Shape.cpp using Visual C++ 6.0 it come out with errors message:
    Shape.cpp(7) : error C2270: 'getArea' : modifiers not allowed on nonmember functions
    Shape.cpp(12) : error C2270: 'getVolume' : modifiers not allowed on nonmember functions

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> double getArea() const

    In Shape.cpp
    Code:
    double Shape::getArea() const { ... }
    Same with getVolume()

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    twomers already answered your question, but might also consider these pointers:

    1. Fully qualify all names used in header files. For example, use std::string instead of string. The reason is that someone who includes your header file might have say, a class named string (for guitar strings?), and consequently there will be a name collision.

    2. Since Shape is a base class, it should have a virtual destructor. I am not entirely sure if this matters when you do not have any member variables, but it is good practice anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It matters. Functions with virtual member functions always have state. (Typically the vptr, but that's an implementation detail.) Deleting an object through a base pointer is always undefined behaviour if the base doesn't have a virtual destructor. That's just how it is. Just because one implementation might not crash is no reason to omit it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Polymorphism - Can't Articulate Problem
    By Tonto in forum C++ Programming
    Replies: 17
    Last Post: 11-13-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21