Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Starting Out with C++: Early Objects 5th Edition
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
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.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Introduction to Object Oriented Programming Chapter 10.
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.
1 Class 19 Chapter 13 – Creating a class definition.
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.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
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
Introduction to Classes
Introduction to Classes and Objects
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
SPL – PS3 C++ Classes.
Presentation transcript:

Chapter 13

Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming focuses on objects (“nouns” are important). What is an object? A software entity that contains both data and procedures.

Separation of Data and Code In procedural programming, data and code are two separate entities. This can cause problems such as:  If the underlying data structures change, all code that uses the data must be rewritten.  Users of the data must be familiar with its internal structure.

Encapsulation Encapsulation refers to the combining of data and code into a single object. Only the designer of the class must know its workings. And, if the data structures change, the INTERFACE remains the same.

A Simple Example – Rectangle class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; };

Data Hiding An object can hide its data from the code that is outside of the object (i.e. “private”). In this way:  Users of an object can interface with the object in the same way, regardless of internal changes that may occur.  Users of the object cannot erroneously change the data.

Object Reusability As long as the user understands an object’s interface, it is easy to use an object that has been created by another programmer. Example: consider the string class. You do not know its internal representation, yet it is fairly simple to use.

Defining Member Functions void Rectangle::setWidth(double w) { width = w; } Use the scope resolution operator :: which says that setWidth is a member of the Rectangle class. [see program 13-1 in text.]

Defining an Object Rectangle box; box is called an instance of the Rectangle class. To call a member function of an object, use the DOT operator: box.setWidth(12.7); [see program 13-2]

Accessors and Mutators  Accessors, or “getter functions” are functions that get a value of a member variable. They do not change anything in the object, and therefore should be defined as const functions.  Mutators or “setter functions” change one or more member variables.

Pointers to objects Pointers to class objects work exactly the same way as pointers to struct objects. Example: Rectangle *recptr; Rectangle box; recptr = &box; recptr->setWidth(12.5); recptr->setLength(4.8); recptr = new Rectangle;

Private Members and Data Validation void Rectangle::setWidth(double w) { if (w >=0) width=w; else { cout << “invalid data”; exit(1); } }

Separating Class Specification and Implementation The user of a class does not have to see the actual code of the member functions. The user needs 2 things: 1. compiler must see the class declaration 2. linker must have the actual code to generate the executable.

Recall: Program Translation  Compiler: high level language -> object code gcc –c myhmwk.cpp  Linker: object code -> executable code

Header files  A header file includes the class declaration. For example, for the Rectangle class, we’d create a file called: Rectangle.h All programs that use the Rectangle class will: #include “Rectangle.h”

Class Implementation The actual code (i.e. function definitions) of a class will reside in a.cpp file. For example, Rectangle.cpp will hold definitions of all Rectangle member functions. All programs that use the Rectangle class will link with the object code of Rectangle.cpp

Linking Several Files Example: suppose you wrote a program called shapes.cpp that used objects of type Rectangle and Circle. To compile your program, you type: g++ -c shapes.cpp To link, you type: g++ shapes.o Rectangle.o Circle.o

More on Header Files To avoid multiple declarations of the same class, always “wrap” your header file with: #ifndef RECTANGLE_H #define RECTANGLE_H ….. (contents of header file) #endif

Rectangle.hCircle.h #include “shapes.h” #include “Rectangle.h” #include “Circle.h” mainprogram.cpp

Advantages of Separate Compilation  Great for Reusability  Protects code  Maintenance made easy – only need to recompile those files that were changed, and then link object code.

Makefiles and Project Files  In UNIX, you can automate the process of separate compilation using a “makefile” and the “make” utility.  In Windows, you create a project file.  The next step of your homework assignment will be to move to classes and to separate compilation.

13.6 Inline member functions Short functions can be written in the declaration of the class (.h file). class Rectangle { private: double width; double length; public: double getWidth() const { return width; } };

Inline member functions (cont.)  Do not need a scope resolution operator :: since this is inside the class.  This is actually a directive to the compiler to replace every call with the actual code (similar to a macro).  Improves efficiency since function calls are eliminated.  Long functions should never be placed inline, since this will blow up the size of the program.

Constructors A constructor is a function that is automatically called when an object is created (i.e. defined). The name of the constructor is the same as the class name. For example: class Rectangle { ….. public: Rectangle(); } [see program 13-5 in text.]

Code for Constructor Rectangle::Rectangle() { width=0.0; length=0.0; }  No return type  Yes, may accept arguments.  Use the constructor to initialize the object’s member variables.

“Calling” the constructor When you instantiate an object, the constructor automatically gets called. For example: Rectangle box; box’s width and length member variables have been set to 0.0 by the constructor. The purpose of the constructor is to initialize data members of an object before the members are used.

Default Constructor  A constructor without arguments is called the default constructor. It gets called automatically, when an object is instantiated.  NOTE: you do not have to put () for a default constructor to be called.  If the class does not include a constructor, the compiler creates a default constructor that does nothing.

Constructors and “New” The constructor IS called when dynamically allocated objects are created. Example: Rectangle *recptr; // define a pointer recptr = new Rectangle; // default constructor is called here.

Constructors with Arguments Example: Rectangle::Rectangle(double w, double h) { width=w; length=h; } In main: Rectangle box(10.0, 20.0); int w=9, len=9; Rectangle box2(w, len);

Overloading Constructors  You may include as many constructors as you wish, as long as the parameter list is different.  Recall: this is the same as overloading any function.  Recall: with strings, we are familiar with several different constructors. string str; string str(“Hello”); string str(str2); ….

Default Arguments A default argument to a function is an argument that does not have to be sent when the function is called. If no argument is passed to the function, a default value is assigned to the parameter. Example: int displayChar(char c=‘?’) { cout << c << endl; }

Default args in a constructor  Instead of defining a default constructor, and a constructor with arguments, you can use default arguments in the constructor.  NOTE: this is considered a default constructor since it can be called without arguments. Therefore, you cannot define another default constructor.

Example of default arguments Rectangle::Rectangle(double w=0.0, double len=0.0) { width=w; length=len; } Note: If there is no default constructor, then you cannot create an object without sending the necessary arguments.

Destructors  Similar to constructors, in that a destructor is automatically called when an object goes out of scope, or it is deleted.  Same name as the class, preceded by ~  Usually used to free up memory that was dynamically allocated.

Destructor Example Rectangle::~Rectangle() { cout << “this object is about to be destroyed.”; }  No return type  No arguments (hence there can be only 1 destructor.)

Inventory Example See programs and in the text.

Private Member Functions Note the function createDescription in the InventoryItem class is private. Why create a private member function? Used for functions that help the class internally accomplish tasks that should never be done by a user.

Arrays of Objects Example: InventoryItem inventory[100]; Use the DOT operator as we did with structures: inventory[i].setUnits(30); Question: When does the constructor get called? When you define the array, the constructor is called 100 times, once per object.

Arrays cont.  Which constructor is called? The default constructor.  You can add an initializer, and then other constructors will be invoked. Example: InventoryItem inventory[1]={“Hammer”, “Wrench”, “Pliers”}; If values are missing, the default constructor will be called (similar to all zeros for remaining array).

Arrays cont.  What about >1 argument? Use form of a function call. Example: InventoryItem inventory[1]={“Hammer”, InventoryItem(“Wrench”, 8.75, 20)}; Here we are explicitly invoking the constructor.

Lab 4 Posted on course website.