Im a member function so i can access width and height

Info icon This preview shows pages 52–55. Sign up to view the full content.

View Full Document Right Arrow Icon
I’m a member function, so I can  access  width  and  height . width * height width height I’m a member function too, so I  can access  width  and  height . r.width = 10; // ERROR! I’m not a member function of  Rect   so I can’t access its private  variables!
Image of page 52

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Public vs Private class  Rect {     int GetArea()     {        int area = width * height;       return(area);     }   }; void Initialize(int startW, int startH)    width = startW;    height = startH; } public: private: You should make most of your class  member  variables  private to  hide them  from them from  the rest of your program… After all, only your class should know  what variables it has and how they’re  used… This is called  encapsulation .” To use your class, the other parts of your  program should call its  public member  functions int main(void) {    Rect r;        r. Initialize (10,7);    cout << r. GetArea ();     r int GetArea()    int area = width * height;    return(area); } void Initialize(int startW, int startH)    width = startW;    height = startH; } width height 10        7 10 7
Image of page 53
Why Have Private Data? When do you drive your  car, do you really care how  the engine works? No. You just use the car’s  “public  interface”:  the  steering wheel , the  accelerator  and the  brake  and  expect the engine to work. Similarly, once we define a  class, we just want to use  its public functions and  forget how it actually  works inside.
Image of page 54

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Each class is like a  self-contained,  problem-solving unit …  AN ADT! Self-contained Problem Solvers It contains all of the necessary  data   and  functions  to solve a certain  problem. We can write a class once, test it,  and then use it over and over. Other parts of our program don’t  need to know  how   it works , only  how to use it ! Since we hide how our class works  and what variables it uses… We can improve its internal logic and  variables later and the rest of our  program will still work as-is!
Image of page 55
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern