Need help on array

This is a discussion on Need help on array within the C++ Programming forums, part of the General Programming Boards category; My project is to create shapes, and place Shape * pointers to each new Shape object into an array. This ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    Question Need help on array

    My project is to create shapes, and place Shape * pointers to each new Shape object into an array.

    This is something that I came up with, and it doesn't work at all.

    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    #include "shape.h"
    #include "circle.h"
    #include "rectangle.h"
    #include "square.h"
    #include "triangle.h"

    void virtualViaPointer( const Shape * );

    int main()
    {
    bool exit = false;
    for (;
    {
    int choice;
    Shape *arrayOfShapes[ 4 ];

    cout << "( 1 ) Circle" << endl;
    cout << "( 2 ) Rectangle\n";
    cout << "( 3 ) Square\n";
    cout << "( 4 ) Triangle\n";
    cout << "( 5 ) Quit\n";
    cout << "Choose shape to create: ";
    cin >> choice;

    for ( int i = 0; i < 4; i++ )
    {

    switch( choice )
    {
    case ( 1 ):
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    case ( 2 ):
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    case ( 3 ):
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    case ( 4 ):
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    case ( 5 ):
    exit = true;
    break;
    default:
    cout << "Please select again!\n";
    break;
    }
    if ( exit )
    break;
    }
    }
    return 0;
    }

    void virtualViaPointer( const Shape *baseClassPtr )
    {
    baseClassPtr->printShapeName();
    baseClassPtr->print();
    cout <<"\nArea = " << baseClassPtr->area() << "\n\n";
    }

    Please help!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You need to create the shapes as well...

    Code:
    switch( choice )
    {
    case ( 1 ):
    arrayOfShapes[i] = new Circle;
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    case ( 2 ):
    arrayOfShapes[i] = new Rectangle;
    virtualViaPointer( arrayOfShapes[ i ] );
    break;
    etc.

    Also, you need to free the memory before the program exits.

    Code:
    for (int i = 0; i < 4; i++) delete arrayOfShapes[i];
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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