Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CSci 162 Lecture 10 Martin van Bommel

2 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

3 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

4 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

5 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

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

7 Initial Class - Rectangle class Rectangle { private: double width; double length; public: void setWidth (double); void setLength (double); double getWidth ()const; double getLength ()const; double getArea ()const; };

8 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

9 Implement Member Functions void 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

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

11 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();

12 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

13 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

14 Constructor Arguments A constructor can have parameters public: Rectangle(double w, double l) { width = w; length = l; } Arguments passed when object is created Rectangle box(5.0, 6.0);

15 Default Arguments Functions, including constructors, can have default arguments Passed to function if no argument provided Default value listed in function header public: Rectangle(double w = 0.0, double l = 0.0) { width = w; length = l; }

16 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

17 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!”; }

18 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


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

Similar presentations


Ads by Google