When properly used oop can make your programs easier

This preview shows 33 out of 37 pages.

When properly used*, OOP can make  your programs  easier to program  and  debug . Let’s briefly talk about what OOP is…
Image of page 33

Subscribe to view the full document.

Object Oriented Programming  Idea:  Break up your program into simple “objects” that represent different  components of your program. Idea:  An object is a self-contained component that has all of the required  data   and  algorithms  to solve a problem. Idea:  Every object has a “public interface” that enables other parts of your  program to use  your object. Idea:  Every object has “private state” (its data and algorithms) that the object  needs to solve a problem. (What does an object remind you of?) (Right! A C++ object is basically just an ADT!)
Image of page 34
Public Interface & Private State  main() {    FriendFinder x;    x. AddFriends (“Carey Nash”,                          ”Scott Wilhelm”);     x. AddFriends (“Jenny Oh”,                           ”David Sun”);    …    string userA, userB;    cin >> userA >> userB;    if (x. AreTheyFriends (userA,                                     userB) == true)       cout << userA << “ is friends with “               << userB; } Consider our  friend finder  class… It has a very simple  public  interface But it also has a complex set of  internal   data structures and algorithms… The OOP idea : hide these details so  anyone can use our class easily…. If every part of our program is turned into  an easy-to-use class, the whole thing becomes  simpler! We use a similar approach to construct  real-life objects! Consider your car… It has an easy-to-use interface like the  steering wheel, brake and accelerator  pedals. The complicated machinery is hidden  from you – you don’t need to know  how it works to use it!
Image of page 35

Subscribe to view the full document.

Public Interface & Private State  main() {    FriendFinder x;    x. AddFriends (“Carey Nash”,                          ”Scott Wilhelm”);     x. AddFriends (“Jenny Oh”,                           ”David Sun”);    …    string userA, userB;    cin >> userA >> userB;    if (x. AreTheyFriends (userA,                                     userB) == true)       cout << userA << “ is friends with “               << userB; } Now what if I wanted to  improve  my  FriendFinder data structures? Let’s say I made a  radical change  to our  friend  network  data structures…  Would the  user  of my class need to  change  any  part of her program?  No!  Because we’ve hidden the details  from the user.
Image of page 36
Image of page 37
You've reached the end of this preview.

{[ 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