Chapter 9 Objects and Classes

21
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 data fields (also known as instance variables) with their current values. The behavior of an object is defined by a set of methods.

description

Chapter 9 Objects and Classes. OO Programming Concepts - PowerPoint PPT Presentation

Transcript of Chapter 9 Objects and Classes

Page 1: Chapter 9 Objects and Classes

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 data fields (also known as instance variables) with their current values. The behavior of an object is defined by a set of methods.

Page 2: Chapter 9 Objects and Classes

Objects

data field 1

method n

data field m

method 1

(A) A generic object

...

...

State (Properties)

Behavior

radius = 5

findArea()

Data field, State Properties

Method, Behavior

(B) An example of circle object

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

Page 3: Chapter 9 Objects and Classes

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.

Page 4: Chapter 9 Objects and Classes

Design a class named Circle

These instance variables can be used to describe a particular individual Circle:int radius

double area

Page 5: Chapter 9 Objects and Classes

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.

Page 6: Chapter 9 Objects and Classes

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

}

Page 7: Chapter 9 Objects and Classes

Two Instances of the class Circle

Each object or instance created from a class type has its own set of instance variables.

myRadius : 1

myArea: 3.14159

unit

myRadius: 100

myArea: 31415.9

other

Page 8: Chapter 9 Objects and Classes

Naming Instance Variables

myRadius: 1

myArea: 3.14159

unit

myRadius:100

myArea:31415.9

other

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.

Page 9: Chapter 9 Objects and Classes

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.

Page 10: Chapter 9 Objects and Classes

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

Page 11: Chapter 9 Objects and Classes

“Private Parts” are Protected

#include “Circle.h”

int main()

{

Circle unit();

unit.calculateArea();

cout << unit.getArea();

return 0;

}

Page 12: Chapter 9 Objects and Classes

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.

Page 13: Chapter 9 Objects and Classes

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).

Page 14: Chapter 9 Objects and Classes

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.

Page 15: Chapter 9 Objects and Classes

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.

Page 16: Chapter 9 Objects and Classes

getArea Accessor Method

/**

* Pre-condition: None.

* Post-condition: returns double

* value for this Circle’s area.

*/

double Circle::getArea()

{

return myArea;

}

Page 17: Chapter 9 Objects and Classes

int main() { Circle unit(); unit.setRadius(100); unit.calculateArea(); cout << “Area:” << unit.getArea() << endl; return 0;}

Using Accessors or “Getters”

What values are printed out?Why do we see this output?

Page 18: Chapter 9 Objects and Classes

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.

Page 19: Chapter 9 Objects and Classes

setRadius Mutator Method

• Pre-condition: receives a type double value for the radius.

• Post-condition: assign the given value to the myRadius instance variable.

Page 20: Chapter 9 Objects and Classes

setRadius Mutator Method

void Circle::setRadius(double radius)

{ if (radius >=0) myRadius = radius; else myRadius = 1; }

Page 21: Chapter 9 Objects and Classes

int main()

{

Circle aCircle();

aCircle.setRadius(100);

aCircle.calculateArea();

cout << “the area is ”

<< aCircle.getArea();

return 0;

}

Using Accessors & Mutators