chapter 10 classes and objects in-depth. chapter 10 a class provides the foundation for creating...

20
Chapter 10 Classes and Objects In-Depth

Upload: vivien-price

Post on 14-Dec-2015

220 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10Classes and Objects In-Depth

Page 2: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10• A class provides the foundation for creating specific objects, each of which shares the general attributes, or

characteristics, and behavior of the class.– At the abstract level, a class can be described as an interface that defines the behavior of its objects.– At the implementation level, a class is a syntactical unit that describes a set of data and related operations that are common to its objects.

Page 3: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

CLASS DECLARATION FORMATclass name{//FUNCTION MEMBERSpublic:

Function prototypes go here.

//DATA MEMBERSprivate:

Variable definitions go here.}; //END CLASS

Page 4: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• The public class functions form the interface to the class and are accessible from outside the class. Public functions are used to provide access to and manipulate the private class members. A private class member is only accessible within the class by functions of the same class.

Page 5: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• Encapsulation means to package data and/or operations into a single well-defined programming unit.

Page 6: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• Information hiding means that there is a binding relationship between the information, or data, and its related operations so that operations outside the encapsulated unit cannot affect the information inside the unit.

Page 7: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• The Unified Modeling Language, or UML, is a technique for analyzing and designing object-oriented software.

Page 8: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

+addDeposit(in amount:double)

+subtractWithdrawal(in amount:double)

+addInterest()

+getBalance():double

-balance: double

-interestRate: double

SavingsAccount

-accountNumber: int

UML diagram for a SavingsAccount class

Page 9: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• An object is an instance, or specimen, of a given class. An object of a given class has the structure and behavior defined by the class that is common to all objects of the same class.

Page 10: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

Object Definition Format

class name object name;

Page 11: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• A function implementation is the definition of the function that includes the function header and the body of the function.

Page 12: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

Format for a Function Implementation

return type class name :: function name (parameter listing)

{

//BODY OF FUNCTION GOES HERE

}//END FUNCTION

Page 13: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• A constructor is a special class function that is used to initialize an object automatically when the object is defined.

Page 14: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

No returntype

Constructor namesame as class name

SavingsAccount :: SavingsAccount(int num, double bal, double rate)

ParamtersetersParameters

Scoping operator

Constructor Format

Page 15: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• All the member functions of a class carry with them an invisible pointer, called this, that points to the object that called the function.

Page 16: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• An access function, sometimes called a get function, is a function that returns only the values of the private members of an object.

Page 17: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

box.getWidth()

Object

Rectangle

Message

width

(box)

• A message is a call to a member function.

Page 18: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• A type cast converts data of one type to a different type, temporarily. You can convert data of one type to another type by preceding the data variable/expression with the type to convert to within parentheses like this: winPercent = (double)wins/(wins + losses) * 100;

Page 19: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

• A project file identifies the files that need to be compiled and linked to create a given executable program.

Page 20: Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,

Chapter 10

#include "rectangl.h"

myProj.prj (PROJECT FILE)

myProg.cpp (APPLICATION SOURCE)

rectangl.cpp (IMPLEMENTATION SOURCE)

...

;

;

;

;

#include "rectangl.h"

rectangl.h (CLASS SOURCE)

class Rectangle{

A multi-file Project