Thread: Polymorphism Problem

  1. #1
    Registered User
    Join Date
    Apr 2007

    Polymorphism Problem

    I was writing this simple polymorphism program:

    #ifndef SHAPE_H
    #define SHAPE_H
    #include <string>
    using namespace std;
    class Shape
    	virtual double getArea() const;
    	virtual double getVolume() const;
    	virtual string getName() const = 0;
    	virtual void print() const = 0;
    #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
    >> double getArea() const

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

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    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!

    "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, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06: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