CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    3

    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;
    }

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  3. #3
    Join Date
    Dec 2007
    Posts
    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Implementing Acessor/Mutator Functions

    What isn't working? What are the specific errors?

  5. #5
    Join Date
    Dec 2007
    Posts
    3

    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

  6. #6
    Join Date
    Dec 2007
    Posts
    13

    Re: Implementing Acessor/Mutator Functions

    just look carefully.




    http://www.codeuu.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured