Lecture 8 Object Oriented Programming (OOP)

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Starting Out with C++: Early Objects 5th Edition
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: A First Look at Classes
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
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.
Review: Two Programming Paradigms
classes and objects review
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
More about OOP and ADTs Classes
Introduction to Classes
Corresponds with Chapter 7
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Introduction to Classes and Objects
Today’s topics UML Diagramming review of terms
COP 3330 Object-oriented Programming in C++
Lecture 5- Classes, Objects and Methods
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Classes C++ representation of an object
CPS120: Introduction to Computer Science
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Lecture 8 Object Oriented Programming (OOP) CS 128 Introduction to C++ Lecture 8 Object Oriented Programming (OOP) Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis

Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program C, Fortran, Pascal, Ada, Basic, Go Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of Abstract Data Types (ADT) that represent the data and its functions Java, C++, C#, Python, Objective-C, Swift, Ruby, Perl

Limitations of Procedural Programming 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

Object-Oriented Programming Terminology The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. class: (allows bundling of related variables). When you define a class, you define a blueprint for a data type.  object: an instance of a class

Classes and Objects A Class is like a blueprint and objects are like houses built from the blueprint

Object-Oriented Programming Terminology attributes: members of a class methods or behaviors: member functions of a class

Introduction to Classes Objects are created from a class Format: class ClassName { declaration; // functon prototypes declaration; }; returnType className:: methodName() // definition }

Class Example

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

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

Class Example Private Members Public Members

More on Access Specifiers Can be listed in any order in a class Can appear multiple times in a class If not specified, the default is private

Using const With Member Functions const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object.

Defining a Member Function When defining a member function: Define function using class name and scope resolution operator (::) in the relevant .cpp file Example: Rectangle.cpp void Rectangle::setWidth(double w) { width = w; }

Accessors and Mutators 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.

Defining an Instance of a Class 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

Program 13-1 (Continued)

Program 13-1 (Continued)

Program 13-1 (Continued)

Why Have Private Members? Making data members private provides data protection Data can be accessed only through public functions Public functions define the class’s public interface

Activity 21 Create a class called Shape which has the double width and height. Create the accessors and mutator’s for the data fileds width and height Create a member function calculateArea() Create 2 Shapes, a square and rectangle and use the calcualteArea() method to calculate the area and display the results

Code outside the class must use the class's public member functions to interact with the object.

Code Organization As programs grow larger, it becomes inconvenient and frequently inefficient for all code to reside in a single file. Frequently, functions are moved into one or more "library" files. It can also be inconvenient to have all functions in a single library. So logically separate functions into groups and put each group in its own "library." Note that each library will consist of a header file (.h or .hpp) and a source file (.cpp).

Separating Specification from Implementation 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

Code Organization : Header Guards The result of preprocessing one implementation (".cpp") file is a translation unit (TU). Headers can include other headers, so a header may be indirectly included multiple times within the same TU. Definitions can only occur at most once per TU. (Some definitions must also not be in multiple TUs By including guards solve this by preventing multiple definition errors when a given header is included more than once within one TU. Include guards work by "wrapping" the contents of the header in such a way that the second and subsequent includes are no-ops. The #ifndef and #define directives should be the first two lines of the file, and #endif should be the last. Include guards are only used in headers.

Creating Header file for Declarations : Rectangle.h #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth()const; double getLength()const; double getArea()const; }; #endif

Inline Member Functions Member functions can be defined inline: in class declaration after the class declaration Inline appropriate for short function bodies: int getWidth() const { return width; }

Rectangle Class with Inline Member Functions 1 // Specification file for the Rectangle class 2 // This version uses some inline member functions. 3 #ifndef RECTANGLE_H 4 #define RECTANGLE_H 5 6 class Rectangle 7 { 8 private: 9 double width; 10 double length; 11 public: 12 void setWidth(double); 13 void setLength(double); 14 15 double getWidth() const 16 { return width; } 17 18 double getLength() const 19 { return length; } 20 21 double getArea() const 22 { return width * length; } 23 }; 24 #endif

Tradeoffs – Inline vs. Regular Member Functions 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

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

Continues...

Contents of Rectangle.cpp Version3 (continued)

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;

Passing Arguments to Constructors To create a constructor that takes arguments: indicate parameters in prototype: Rectangle(double, double); Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }

Passing Arguments to Constructors You can pass arguments to the constructor when you create an object: Rectangle r(10, 5);

More About Default Constructors 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;

Classes with No Default Constructor 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.

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

Continues...

Using Private Member Functions 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

Activity 22 Write a complete program to demonstrate a functionality for a Circle. Should have methods for, Getters and setters Default constructor that initialize radius to 0. Constructor with 1 parameter to assign radius CalculateArea() CalculateCircumference() Your program should have files for Circle.h Circle.cpp Driver.cpp Display the functionality by creating 2 circles with different radius values