classes confusion

This is a discussion on classes confusion within the C++ Programming forums, part of the General Programming Boards category; */I believe I did a good job with constructing my program. Please review and feel free to offer any guidelines ...

  1. #1
    Trublu
    Guest

    classes confusion

    */I believe I did a good job with constructing my program.
    Please review and feel free to offer any guidelines or
    tips as I am stuck. I am referring to my text for an
    example program but I cannot seem to find one that fits.
    Am I at least in the ballpark?*/

    //Here's my problem...

    */Part a - Construct a class named Rectangle that has floating-point
    data members named length and width. The class should have
    a constructor that sets each data member to 0, member functions
    named perimeter() and area() to calculate the perimeter and
    area of a rectangle, respectively, a member function named
    setdata() to set a rectangle's length and width, and a member
    function named (showdata) that displays a rectangle's length,
    width, perimeter, and area.
    Part b. - Then include the Rectangle class
    within a working C++ program.*/

    //Part a.

    #include <iostream.h>
    #include <iomanip.h>

    class Rectangle //class declaration

    Private:
    float length;
    float width;

    Public
    Rectangle (float = 0, float = 0);
    float perimeter();
    float area ();
    void setdata (float length, float width);
    void showdata();
    };

    int main()
    //Should I create rectangle objects and call methods here?
    //Please provide an example
    }

    Perimter: (2 * length + 2 * width)
    Area: (length * width)
    setdata: //Not sure what goes here?
    showdata: //I figure that here I should display length,
    width, perimter, and area, then call perimeter and
    area from in main?

    //Part b.
    //brainstorming
    //create two objects and call at least one
    //rectangle r1, r2, r3
    //float temp = (result should be zero)
    //length + width and give some values

    I sent this e-mail for a kick start only. I am reading right now because maybe I missed something.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First, use code tags, it really helps to make the code readable. Next, remember that C++ is case-sensitive.

    Here's a quick example to help get you started:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class square
    {
    public:
        square() : side_(0) {}
    
        void setData(double x) { side_ = x; }
        double getArea() const { return side * side; }
    
        void print() const { cout << "Area: " << getArea() << endl; }
    private:
        double side_;
    };
    
    int main()
    {
        square a;
        a.setData(10);
        a.print();
    
        return 0;
    }
    That should help point you in the right direction. If you have an y more specific problems, let us know.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 02:54 PM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 02:46 AM
  3. Classes - Confusion
    By Kane in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2005, 10:55 AM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. classes and oop confusion
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 11-27-2003, 10:08 AM

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