Escolar Documentos
Profissional Documentos
Cultura Documentos
Slide 13- 1
Chapter
13
Introduction to Classes
13.1
Procedural programming focuses on the process/actions that occur in a program Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of Abstract Data Types (ADTs) that represent the data and its functions
Slide 13- 4
If the data structures change, many functions must also be changed Programs that are based on complex function hierarchies are: difficult to understand and maintain difficult to modify and extend easy to break
Slide 13- 5
Procedural programming worked well for many years. As Programs became larger and more complex, the data and the code that operates on the data tend to separate. This causes problems. In Object Oriented programming, we encapsulate the data with the functions that operate on that data into a class or object. The data format is hidden from the rest of the program.
Slide 13- 6
Classes
Classes include data and the functions (called methods) that operate on the data.
Slide 13- 7
class: like a struct (allows bundling of related variables), but variables and functions in the class can have different properties than in a struct.
object: an instance of a class, in the same way that a variable can be an instance of a struct
Slide 13- 8
A Class is like a blueprint and objects are like houses built from the blueprint
Slide 13- 9
Slide 13- 10
More on Objects
data hiding: restricting access to certain members of an object public interface: members of an object that are available outside of the object. This allows the object to provide access to some data and functions without sharing its internal details and design, and provides some protection from data corruption
Slide 13- 11
Object Reusability
An object includes data and the methods that operate on it. An object is not a stand-alone program. Objects are used by programs that need their services. A house designer might need a rectangle object, or a room object, which needs a rectangle object. Programs are modular, including required objects.
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 12
The format of the data is hidden. The methods perform required actions. string firstName = Paula; string lastName = Kurtenbach; string name = firstName + lastName;
Slide 13- 13
13.2
Introduction to Classes
Introduction to Classes
Slide 13- 15
Class Example
Slide 13- 16
Access Specifiers
Used to control access to members of the class public: can be accessed by functions outside of the class private: can only be called by or accessed by functions that are members of the class
Slide 13- 17
Class Example
Private Members
Public Members
Slide 13- 18
Can be listed in any order in a class Can appear multiple times in a class
Slide 13- 19
const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object.
Slide 13- 20
When defining a member function: Put prototype in class declaration Define function using class name and scope resolution operator (::)
int Rectangle::setWidth(double w) { width = w; }
Slide 13- 21
Mutator: a member function that stores a value in a private member variable, or changes its value in some way
Accessor: function that retrieves a value from a private member variable. Accessors do not change an object's data, so they should be marked const.
Slide 13- 22
13.3
An object is an instance of a class Defined like structure variables: Rectangle r; Access members using dot operator: r.setWidth(5.2); cout << r.getWidth(); Compiler error if attempt to access private member using dot operator
Slide 13- 24
Slide 13- 25
Slide 13- 26
Slide 13- 27
Slide 13- 28
Some data is the result of a calculation. In the Rectangle class the area of a rectangle is calculated. length x width If we were to use an area variable here in the Rectangle class, its value would be dependent on the length and the width. If we change length or width without updating area, then area would become stale. To avoid stale data, it is best to calculate the value of that data within a member function rather than store it in a variable.
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 29
Pointer to an Object
Can define a pointer to an object: Rectangle *rPtr; Can access public members via pointer: rPtr = &otherRectangle; rPtr->setLength(12.5); cout << rPtr->getLenght() << endl;
Slide 13- 30
Slide 13- 31
13.4
Making data members private provides data protection Data can be accessed only through public functions Public functions define the classs public interface
Slide 13- 33
Code outside the class must use the class's public member functions to interact with the object.
Slide 13- 34
13.5
Place class declaration in a header file that serves as the class specification file. Name the file ClassName.h, for example, Rectangle.h Place member function definitions in ClassName.cpp, for example, Rectangle.cpp File should #include the class specification file Programs that use the class must #include the class specification file, and be compiled and linked with the member function definitions
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 36
Rectangle Version 1 rectangle.h, rectangle.cpp, Pr13-4.cpp rectangle.cpp and Pr13-4.cpp include rectangle.h rectangle.h uses #ifndef RECTANGLE_H to ensure that it is only included once.
Slide 13- 37
Compiling Separate Files in Linux Make sure all .cpp and .h files are in the same folder. Type the g++ comand, including all .cpp files in the compile & link command. Type a.out to run the compiled program. Ex:
g++ Pr13-4.cpp rectangle.cpp a.out
Slide 13- 38
Choose Console Application Choose a project name similar to the name of the file that contains main (). Right click on main.cpp & choose remove Right click on the project name & choose Add to Project Be sure to add all .cpp and .h files 3rd button from left
Slide 13- 39
13.6
Member functions can be defined inline: in class declaration after the class declaration
Inline appropriate for short function bodies: int getWidth() const { return width; }
Slide 13- 41
rectangle.cpp
Slide 13- 42
Regular functions when called, compiler stores return address of call, allocates memory for local variables, etc.
Code for an inline function is copied into program in place of call larger executable program, but no function call overhead, hence faster execution
Slide 13- 43
13.7
Constructors
Constructors
Member function that is automatically called when an object is created Purpose is to construct an object Constructor function name is class name Has no return type
Program 13-5
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 45
Rectangle Version 3
Program 13-6
rectangle.h rectangle.cpp
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 46
Continues...
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 13- 47
Slide 13- 48
Slide 13- 49
Default Constructors
A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing. A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;
Slide 13- 50
When the Rectangle object is created by the new operator, its default constructor is automatically executed.
Slide 13- 51
13.8
Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }
Slide 13- 53
You can pass arguments to the constructor when you create an object:
Slide 13- 54
If all of a constructor's parameters have default arguments, then it is a default constructor. For example: Rectangle(double = 0, double = 0);
Creating an object and passing no arguments will cause this constructor to execute:
Rectangle r;
Slide 13- 55
When all of a class's constructors require arguments, then the class has NO default constructor.
When this is the case, you must pass the required arguments to the constructor when creating an object.
Slide 13- 56
Rectangle Version 4
Slide 13- 57
Sale Class
Sale Version 1 Constructors may have default arguments. The default is passes automatically if no argument is given. Sale Version 2
Slide 13- 58
13.9
Destructors
Destructors
Member function automatically called when an object is destroyed Destructor name is ~classname, e.g., ~Rectangle Has no return type; takes no arguments Only one destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it Program 13-10
Slide 13- 60
Slide 13- 61
Slide 13- 62
Slide 13- 63
Slide 13- 64
When an object is dynamically allocated with the new operator, its constructor executes:
Rectangle *r = new Rectangle(10, 20);
Slide 13- 65
13.10
Overloading Constructors
Overloading Constructors
A class can have more than one constructor Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double);
Slide 13- 67
Slide 13- 68
Slide 13- 69
Slide 13- 70
Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters Square(); Square(int = 0); // will not compile
Since a destructor takes no arguments, there can only be one destructor for a class
Slide 13- 71
Non-constructor member functions can also be overloaded: void setCost(double); void setCost(char *); Must have unique parameter lists as for constructors
Slide 13- 72
13.11
A private member function can only be called by another member function It is used for internal processing by the class, not for use outside of the class See the createDescription function in InventoryItem.h (Version 3)
Slide 13- 74
13.12
Arrays of Objects
Arrays of Objects
InventoryItem inventory[40];
Slide 13- 76
Arrays of Objects
Slide 13- 77
Arrays of Objects
If the constructor requires more than one argument, the initializer must take the form of a function call:
Slide 13- 78
Arrays of Objects
It isn't necessary to call the same constructor for each object in an array:
Slide 13- 79
Slide 13- 80
Slide 13- 81
Slide 13- 82
13.15
Slide 13- 84
Slide 13- 85
Slide 13- 86
In UML you indicate a private member with a minus (-) and a public member with a plus(+).
Slide 13- 87
To indicate the data type of a member variable, place a colon followed by the name of the data type after the name of the variable.
- width : double - length : double
Slide 13- 88
To indicate the data type of a functions parameter variable, place a colon followed by the name of the data type after the name of the variable.
+ setWidth(w : double)
Slide 13- 89
To indicate the data type of a functions return value, place a colon followed by the name of the data type after the functions parameter list.
+ setWidth(w : double) : void
Slide 13- 90
Slide 13- 91
Constructors Destructor
Slide 13- 92
13.16
Get a written description of the problem domain. Identify all the nouns in the description. Include pronouns and noun phrases. Each of these is a potential class. Refine the list to include only classes that are relevant to the problem.
Slide 13- 94
Include: Physical objects such as vehicles, machines, or products. Any role played by a person.
Recordkeeping items
Slide 13- 95
Eliminate nouns that represent the same thing. Eliminate nouns that represent objects not needed to solve the problem. Eliminate nouns that represent objects, not classes. Eliminate nouns that might represent simple values. These can be stored in a variable and do not require a class.
Does the noun require any actions? Would you use a group of related values to represent the items state?
Slide 13- 96
Slide 13- 97