DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: rectangle class [was:please give suggestions]

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Posts
    3

    rectangle class [was:please give suggestions]

    I am writing a code that creates a class rectangle. The class rectangle consists of two member data height and width both of type float. The class rectangle has two member functions getdata() and areacal(). The getdata() function is used to read data in height and width. The areacal() function is used to calculate and display the area of a triangle on the screen. The program creates an array on five (5) objects of the class rectangle using new operator and place the address of the array in a pointer ptr. The type of ptr is rectangle. Now the program reads data into each object and calculates the area using the member functions.


    #include <iostream>
    class Rectangle {
    float width, height, area;
    public:
    void getdata (float, float);
    //float area (void) {return (width * height);
    void areacal (float);
    };

    void Rectangle::getdata (float a, float b) {
    width = a;
    height = b;
    }
    void Rectangle::areacal (float area) {
    area = width * height;
    }
    int main () {
    char temp;
    Rectangle a, *b, *c;
    Rectangle * ptr = new Rectangle[2];
    b= new Rectangle;
    c= &a;
    a.getdata (1,2);
    b->getdata (3,4);
    ptr->getdata (5,6);
    ptr[1].getdata (7,8);
    cout << "a area: " << a.areacal(a,b) << endl;
    cout << "*b area: " << b->areacal() << endl;
    cout << "*c area: " << c->areacal() << endl;
    cout << "d[0] area: " << ptr[0].areacal() << endl;
    cout << "d[1] area: " << ptr[1].areacal() << endl;
    cin >> temp;
    return 0;
    }

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,

    you got a few errors here:
    highlighted in bold

    Code:
    #include <iostream>
    using namespace std;
    
    class Rectangle {
        float width, height;// unneccessary ,area;
    public:
        void getdata (float, float);
        //float area (void) {return (width * height);
        float  areacal (); // returntype float because of the way you want 
                                        // to call the method later in main
    };
    
    void Rectangle::getdata (float a, float b) {
        width = a;
        height = b;
    }
    float Rectangle::areacal ()  { // no parameters needed
        return  width * height; // return a value, rather than assigning
                                                 // a value to a local
    }
    int main () {
        char temp;
        Rectangle a, *b, *c;
        Rectangle * ptr = new Rectangle[2];
        b= new Rectangle;
        c= &a;
        a.getdata (1,2);
        b->getdata (3,4);
        ptr->getdata (5,6);
        ptr[1].getdata (7,8);
        cout << "a area: " << a.areacal() << endl; // areacal does now
                                                                       //not have any parameters
        cout << "*b area: " << b->areacal() << endl;
        cout << "*c area: " << c->areacal() << endl;
        cout << "d[0] area: " << ptr[0].areacal() << endl;
        cout << "d[1] area: " << ptr[1].areacal() << endl;
        cin >> temp;
        return 0;
    }
    Hope that helps?

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Jul 2006
    Posts
    3
    Hi
    Thanks, it solved the problem.
    I feel that original problem (given in problem statement is not resolved as yet)

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,

    To be honest, I do not understand the question. In fact your variable ptr is a pointer to two objects of type rectangle. What you want to do now is to use a loop to read in the data (for/while).
    But don't forget to delete the objects to avoid memory leaks (I forgat that in the above solution).
    Add the line
    Code:
    delete[] ptr;
    before the return statement of the main() routine.

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

Similar Threads

  1. Replies: 2
    Last Post: 04-18-2007, 02:34 AM
  2. class vector<?>
    By pseudo in forum C++
    Replies: 3
    Last Post: 02-14-2005, 07:48 AM
  3. Class.forName() -- class not found, why?
    By Olivier Béghain in forum Java
    Replies: 1
    Last Post: 07-23-2003, 01:01 AM
  4. How To Do It - Shared Class Variables Part IV
    By Patrick Ireland in forum .NET
    Replies: 3
    Last Post: 05-07-2001, 03:04 PM
  5. Help needed before 25/1/01!!!!
    By Chris reid in forum Java
    Replies: 3
    Last Post: 02-11-2001, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center