-
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;
}
-
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
-------------------------------
-
Hi
Thanks, it solved the problem.
I feel that original problem (given in problem statement is not resolved as yet)
-
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
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
-
Replies: 2
Last Post: 04-18-2007, 02:34 AM
-
Replies: 3
Last Post: 02-14-2005, 07:48 AM
-
By Olivier Béghain in forum Java
Replies: 1
Last Post: 07-23-2003, 01:01 AM
-
By Patrick Ireland in forum .NET
Replies: 3
Last Post: 05-07-2001, 03:04 PM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|