lecture33-apr21 - Lecture 33 Announcements Assignment 6 due...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
4/21/10 1 Lecture 33 Announcements Assignment 6 – due tomorrow, Q&A Assignment 7 – out Friday, due the last day of classes Topics of the day – More OO – Classes and objects – OO modeling/examples Classes: Dog Example Suppose you are writing a dog tracking system for owner subscribers: Dog is a real-world class that can be modeled in your program as an ADT – Every class has common associated data, or attributes . Attributes of interest for the Dog class are: breed, color, height, weight, gender, name, tag# – Every class has common associated actions or behaviors. Actions of interest for the Dog class are: bark, sleep, eat, locate, . . . Instances of the Dog class, or objects from the Dog class are specific dog entities: – my dog Bulldozer, your dog X, president Obama's dog Bo Dog as a C++ Class class Dog { public: string breed; string name; double height; double weight; char gender; int tagNumber; /* In order to be able to initialize specific Dog instances, or objects of type Dog, we need a constructor function that initially sets the attribute values on creation */ Dog (string theName, char theGender) { name = theName; gender = theGender; } ~Dog ( ) { } /* destructor */ void bark(int n) { cout << name <<‘:’; for (int i =1; i <=n; i++) cout << "rff ” << endl; } void sleep() { cout << name <<‘:’<< "zzzz, zzzz” << endl; } void eat() { cout << name <<‘:’<< "slurp” << endl; } } /* end of Dog class definition */ These are the attributes: called member variables or data members (the struct part of the data type) These are the actions: called member functions Here is a constructor Here is the destructor Creating and Using Dog Objects Now if I'm writing a program, I can create Dog objects: Dog myDog (“Bulldozer”, ‘m’); Dog beckysDog (“Lady Bird”, ‘f’); Dog kelliesDog (“Izzy”, ‘f’); I can call the public functions that were defined in the Dog class to operate on a specific Dog object: myDog.bark(2); // output - Bulldozer: rff rff beckysDog.eat ( ); // output - Lady Bird:slurp kelliesDog.sleep( );
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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