csci 162 lecture 10 martin van bommel. procedures vs objects procedural programming –centered on...

18
CSci 162 Lecture 10 Martin van Bommel

Upload: jeffry-ryan

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

CSci 162

Lecture 10

Martin van Bommel

Page 2: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Procedures vs Objects• Procedural Programming

– Centered on the procedures or actions that take place in a program

– Send data to the procedures, get results back

– Data and functions separate

• Object-Oriented Programming– Centered around the object (abstract data types that

encapsulate data and procedures together)

– Send messages to objects to work with them

– Only object’s member functions can operate on data

Page 3: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Object-Oriented Terminology

• Object– software entity that contains both data and procedures

(encapsulation)

• Member Attributes– The data items contained in (describe) the object

• Member functions– Procedures that an object performs

Page 4: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Data Hiding

• Object’s internal data is hidden (cannot be accessed directly) from outside code

• Access to the data is restricted to the object’s member functions

• Changes can be made to internal structure as long as interface does not change

• To drive a car, do not need to know internals

Page 5: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Classes and Objects

• Class– Describes the attributes and member functions of a

specific type of object

– e.g. Blueprint of a house

• Object– An instance of a particular class

– e.g. A particular house built using the blueprint

– Many houses can be built from same blueprint

Page 6: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Defining a Class

class ClassName

{

private: // access specifier

// declarations of member variables

// and private member functions

public: // access specifier

// declarations of public member

// functions (prototypes)

};

Page 7: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Initial Class - Rectangleclass Rectangle{ private:

double width; double length; public:

void setWidth (double); void setLength (double); double getWidth () const; double getLength () const; double getArea () const;};

Page 8: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Notes on Rectangle Class

• Public member functions are provided for access to the private data members– get and set are typically used

– Area will be calculated from width and length

• Key word “const” used to specify that function will not change data members– Catches unintentional data update

Page 9: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Implement Member Functionsvoid Rectangle::setWidth(double w){ // mutator

width = w; // (setter)}

double Rectangle::getLength() const{ // accessor

return length; // (getter)}

double Rectangle::getArea() const{

return width * length; }

Note: :: is called scope resolution operator

Page 10: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Object Definition

• A class is a template of an object• To create an object that is an instance of a class,

use:– ClassName objectName;

• Defining an object (declaration of a variable) is called the instantiation of a class– e.g. Rectangle box;

Page 11: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Calling Member Functions

• To call on an objects member functions, use the dot operator– E.g. box.setWidth(5.25);

• Can use the notation as part of an expression if a value is returned– E.g. a = box.getArea();

Page 12: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Inline Member Functions

• When the body of a member function appears inside a class declaration, it is declared inline.

• Inline functions are compiled differently– Compiler replaces call to inline function with the code

of the function itself– Thus it removes the overhead for the call itself– Increases size of the executable file if a large inline

member function is called several times

Page 13: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Constructors

• A constructor is a member function with the same name as the class, but with no specified return type, and is automatically called when an object is created in memory, or instantiated.

Rectangle::Rectangle() { }

• The default constructor takes no arguments• If none declared, one created automatically

Page 14: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Constructor Arguments

• A constructor can have parameterspublic:Rectangle(double w, double l){

width = w;length = l;

}

• Arguments passed when object is createdRectangle box(5.0, 6.0);

Page 15: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Default Arguments

• Functions, including constructors, can have default arguments

• Passed to function if no argument provided• Default value listed in function headerpublic:Rectangle(double w = 0.0, double l = 0.0)

{ width = w; length = l;

}

Page 16: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Default Constructor

• A constructor with no arguments is the default constructor

• A constructor with default arguments for all of its parameters, which can be called with no arguments, is the default constructor

• May have only one default constructor• If constructor defined but it cannot become default

constructor, no default created

Page 17: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Destructor

• A destructor is a member function that is automatically called when an object is destroyed (cannot call a destructor)

• Destructor name is the name of the class preceded by a tilde (~) (with no parameters)

• Can add actions to a destructor

~Rectangle() { cout << ”Destroy!”; }

Page 18: CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program

Private Member Functions

• Private member functions may only be called from member functions of the same class

• Necessary for the internal function of another member function, but not called from outside

• Included in the “private” declarations• Defined either inline or in an implementation file