Thread: new C++ programmer in need

  1. #1
    Unregistered
    Guest

    new C++ programmer in need

    im a new C++ programmer(or wanna be programmer i guess). I've started through my C++ tutorial book, but Im having difficulties with classes. Its really starting to frustrate me.

    can someone take a look at this quickly and tell me why it wont compile? it looks straight to me, but then again, this is one i my first "programs"

    #include<iostream>
    using namespace std;
    enum CHOICE { DrawRect=1, GetArea, GetPerim, ChangeDImensions, Quit};

    //Rectangle class declaration
    class Rectangle
    {
    public:
    //constructors
    Rectangle(int width, int height);
    ~Rectangle();

    //Accessors
    int GetHeight() const {return itsHeight;}
    int GetWidth() const {return itsWidth;}
    int GetArea() const {return itsHeight*itsWidth
    int GetPerim() const {return (2*itsHeight)+(2*itsWidth;}
    void SetSize(int newWidth, int newHeight);

    private:
    int itsWidth;
    int itsHeight;
    };

    //class implementation
    void Rectangle::SetSize(int newWidth, int newHeight)
    {
    itsWidth=newWidth;
    itsHeight=newHeight;
    };

    Rectangle::Rectangle(int width, int height)
    {
    itsWidth=width;
    itsHeight=height;
    };

    Rectangle::~Rectangle() {};

    int DoMenu();
    void DoDrawRect(Rectangle);
    void DoGetArea(Rectangle);
    void DoGetPerim(Rectangle);

    int main()
    {
    //initialize a rectangle to 30, 5
    Rectangle theRect(30,5);

    int choice = DrawRect;
    int fQuit = false;

    while (!fQuit)
    {
    choice= DoMenu();
    if (choice < DrawRect || choice > quit)
    {
    cout << "\nInvalid Choice, please try again.\n\n";
    continue;
    }
    switch(choice)
    {
    case DrawRect:
    DoDrawRect(theRect);
    break;
    case GetArea:
    DoGetArea(theRect);
    break;
    case GetPerim:
    DoGetPerim(theRect);
    break;
    case ChangeDimensions:
    int newLength, newWidth;
    cout <<"\nNew Width: ";
    cin >> newWidth
    cout << "\nNew Length ";
    cin >> newLength
    theRect.SetSize(newWidth, NewHeight);
    DoDrawRect(theRect);
    break;
    case Quit:
    fQuit= true;
    cout << "\nExiting...\n\n";
    break;
    default:
    cout >> "Error in choice!\n";
    fQuit = true;
    break;
    }
    }
    return 0;
    }

    int DoMenu()
    {
    int choice;
    cout << "\n\n** M E N U **\n";
    cout << "(1) Draw Rectangle\n";
    cout << "(2) Area\n";
    cout << "(3) Perimeter\n";
    cout << "(4) Resize\n";
    cout << "(5) Quit\n";

    cin >> choice;
    return choice;
    }

    void DoDrawRect(Rectangle theRect)
    {
    int height = theRect.GetHeight();
    int width = theRect.GetWidth();

    for (int i = 0; i<height; i++)
    {
    for (int j = 0; j<width; j++)
    cout << "*";
    cout << "\n";
    }
    }

    void DoGetArea(Rectangle theRect)
    {
    cout << "Area " << theRect.GetArea()<< endl;
    }

    void DoGetPerim(Rectangle theRect)
    {
    cout << "Perimeter: "<< theRect.GetPerim() << endl;
    }



    thanks in advance

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I take it that that smiley means that you have a ) instead of a }. And can you use the const keyword there? I never have.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    GeekSeb
    Guest
    My Comment is to Use the keyword "inline" before all of your functiosn that have brackets, as that has been one of my problems once and that fixed it.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Post your errors when you have problems so that it is easier for us to help.

  5. #5
    Unregistered
    Guest
    Strangely, I cut and pasted your code to file, and I noticed ( but it does not appear in your post ) that every implementation of the accessor functions have a semicolon after the closing brace. This is a real no no. You are saying to the compiler that the definition is just a declaration. You are probably getting a "redeclaration of class member functions" type error, or something to that effect.

    REMOVE ALL OF THE SEMICOLONS FROM THE CLASS ACCESSOR FUNCTION DEFINITIONS.

  6. #6
    Unregistered
    Guest

    ALSO...

    Remove the semicolons after the class Rectangle Constuctor and destructor as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. C++ Programmer (DirectX9) needed
    By HydroNom in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 02-09-2008, 04:31 PM
  3. WANTED: Lead Programmer
    By fluid_hres in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-10-2006, 07:25 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM