1 / 21

Chapter 9 Objects and Classes

Chapter 9 Objects and Classes. OO Programming Concepts

melina
Télécharger la présentation

Chapter 9 Objects and Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Objects and Classes • OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of datafields (also known as instance variables) with their current values. The behavior of an object is defined by a set of methods.

  2. Objects An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

  3. Classes • Classes are constructs that define objects of the same type. A C++ class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of method, known as constructors, which are invoked to construct objects from the class.

  4. Design a class named Circle These instance variables can be used to describe a particular individual Circle: int radius double area

  5. Declare the class named Circle in the Circle.h file class Circle { private: double myRadius; double myArea; public: Circle(); Circle(double); void setRadius(double); void calculateArea(); double getArea(); }; Notice the semicolon!! This class does not have a main() method and therefore cannot run. It is just a definition used to declare and create Circle objects.

  6. Implement methods in the Circle.cpp file class Circle { Circle::Circle() {myRadius = 1;} Circle::Circle(double radius) { myRadius = radius; } void Circle::setRadius(double radius) { myRadius = radius;} void Circle::calculateArea() { myArea = myRadius * myRadius * 3.14159; } double Circle::getArea() { return myArea; } }

  7. Two Instances of the class Circle unit other myRadius : 1 myArea: 3.14159 myRadius: 100 myArea: 31415.9 Each object or instance created from a class type has its own set of instance variables.

  8. Naming Instance Variables unit other myRadius: 1 myArea: 3.14159 myRadius:100 myArea:31415.9 Tip: Use a naming convention that emphasizes the fact that a set of instance variables belong to a particular object. Unit and other each say these are “my” instance variables.

  9. Creating Circle Objects int main() { Circle unit(); Circle other(100); return 0; } After an object is created, its methods can be accessed using the dot operator (.) (also called the object member access operator). For example, unit.getArea()returns the value of the data field myArea in the object named unit.

  10. Objects are variables that are named instances of a class • the class is their type • Objects have both instance variables and methods • Both the data items and methods of a class are members of the object • Data items are also called data fields or instance variables

  11. “Private Parts” are Protected #include “Circle.h” int main() { Circle unit(); unit.calculateArea(); cout << unit.getArea(); return 0; }

  12. Information Hiding • Object-oriented languages enable a class to restrict how its instance variables are accessed. • A class can designate which of its attributes and methods can be directly accessed by statements inside other classes.

  13. Private • The modifier private applied to the declaration of an instance variable or method of a class means that only methods inside that same class can directly access that variable or method. • The private modifier applies solely to the member of a class (e.g., data fields or methods).

  14. Public • The modifier public applied to the declaration of an instance variable or method of a class means that any methods inside or outside of that same class can directly access that variable or method.

  15. Accessor “Getter” Methods • An accessor is a public method that has no pre-conditions and as a post-condition returns the value of a particular instance variable.

  16. getArea Accessor Method /** * Pre-condition: None. * Post-condition: returns double * value for this Circle’s area. */ double Circle::getArea() { return myArea; }

  17. Using Accessors or “Getters” int main() { Circle unit(); unit.setRadius(100); unit.calculateArea(); cout << “Area:” << unit.getArea() << endl; return 0; } What values are printed out? Why do we see this output?

  18. Mutator “Setter” Methods • A mutator is a public method that has pre-condition of receiving a value to be assigned to one of its instance variables. • A mutator method can allow only valid values to be assigned and ignore attempts to assign invalid values.

  19. setRadius Mutator Method • Pre-condition: receives a type double value for the radius. • Post-condition: assign the given value to the myRadius instance variable.

  20. setRadius Mutator Method void Circle::setRadius(double radius) { if (radius >=0) myRadius = radius; else myRadius = 1; }

  21. Using Accessors & Mutators int main() { Circle aCircle(); aCircle.setRadius(100); aCircle.calculateArea(); cout << “the area is ” << aCircle.getArea(); return 0; }

More Related