|
-
December 5th, 2007, 09:53 AM
#1
Implementing Acessor/Mutator Functions
Hey, I just needed some help on a c++ project I was doing....we are supposed to implement accessor/mutator functions for all the data fields in the Rectangle class....
here is my code before we were told to implement these two kinds of functions....help is appreciated
#include<iostream>
using namespace std;
class Rectangle
{
public:
double height;
double width;
Rectangle()
{
width = 1;
height = 1;
}
Rectangle(double height2, double width2)
{
width = width2;
height = height2;
}
double getArea()
{
return width*height;
}
double getPerimeter()
{
return 2*width*2*height;
}
};
int main()
{
Rectangle Rectangle1(50,5);
Rectangle Rectangle2(35.9,3.5);
cout<<"Rectangle1:"<<endl;
cout<<"Width: "<<Rectangle1.width<<endl;
cout<<"Height: "<<Rectangle1.height<<endl;
cout<<"Area: "<<Rectangle1.getArea()<<endl;
cout<<"Perimeter: "<<Rectangle1.getPerimeter()<<endl;
cout<<endl;
cout<<"Rectangle2:"<<endl;
cout<<"Width: "<<Rectangle2.width<<endl;
cout<<"Height: "<<Rectangle2.height<<endl;
cout<<"Area: "<<Rectangle2.getArea()<<endl;
cout<<"Perimeter: "<<Rectangle2.getPerimeter()<<endl;
return 0;
}
-
December 5th, 2007, 12:30 PM
#2
Re: Implementing Acessor/Mutator Functions
Accessor/mutator functions means that for each data member (height, width) you should have a get function (getHeight, getWidth) that returns the corresponding value and a set function that allows setting the value.
At the same time you should probably change the your data members to private.
Please note that blindly providing mutator functions for all data members is often bad design (and usually done by Java programmers....)
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
December 10th, 2007, 06:07 PM
#3
Re: Implementing Acessor/Mutator Functions
ive tried several things to implement these and non of them are working, could someone give me some kind of example how to add accessor/mutator functions for each of the variables in the class..thanks in advance
-
December 10th, 2007, 06:20 PM
#4
Re: Implementing Acessor/Mutator Functions
What isn't working? What are the specific errors?
-
December 11th, 2007, 12:12 AM
#5
Re: Implementing Acessor/Mutator Functions
actually its more that i cant figure out how to implement both set/get functions together, not that i have specific errors....i understand the get function but dont know how to code a correspoding set function
-
December 11th, 2007, 04:01 AM
#6
Re: Implementing Acessor/Mutator Functions
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
|
Click Here to Expand Forum to Full Width
|