Object-Oriented Programming (OOP) and C++

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ fundamentals.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++ Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Introduction to C++ (Extensions to C)
Introduction to C++ Systems Programming.
About the Presentations
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
(5 - 1) Object-Oriented Programming (OOP) and C++
1.13 The Key Software Trend: Object Technology
(5 - 1) Object-Oriented Programming (OOP) and C++
Variables have attributes
Programs written in C and C++ can run on many different computers
Chapter 15 - C++ As A "Better C"
Capitolo 1 – Introduction C++ Programming
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University C. Hundhausen, A. O’Fallon

Key Concepts Object-Oriented Design Object-Oriented Programming (OOP) Class and Objects Data Encapsulation Abstraction/Information Hiding C++ I/O References and Reference Parameters Unary Scope Resolution Operator Function Overloading A. O’Fallon, J. Hagemeister

Object-Oriented Design (OOD) Model software in ways that are similar to how people view/describe real-world objects Descriptions and designs include properties or attributes of the real-world objects The Unified Modeling Language (UML) provides a specification for illustrating properties of objects along with interactions between them A. O’Fallon, J. Hagemeister

Object-Oriented Programming (OOP) (I) Programming language model which institutes mechanisms to support implementing object driven software systems C++, C#, Java Procedural programming, such as instituted by C, is action oriented In C, the unit of programming is a function In C++ the unit is a class A. O’Fallon, J. Hagemeister

Object-Oriented Programming (OOP) (II) We’ll explore OOP with classes, encapsulation, objects, operator overloading, inheritance, and polymorphism We’ll also explore generic programming with function templates and class templates A. O’Fallon, J. Hagemeister

Classes and Objects What is a class? What is an object? A user defined type or data structure Contains data members (attributes) and member functions (operations) A blueprint for an object What is an object? An instantiation of a class The class is the type and the object is the variable with allocated memory for that type A. O’Fallon, J. Hagemeister

Data Encapsulation (I) A way of organizing or wrapping of data/attributes and methods/operations into a structure (or capsule) Demonstrated by objects Objects naturally impose encapsulation – attributes and operations are closely tied together How does making a function or class a friend of another class impact encapsulation? A. O’Fallon, J. Hagemeister

Abstraction/Information Hiding (I) A design principle which states a design decision should be hidden from the rest of the system In other words, objects should communicate with each other through well-defined interfaces, but not know how other objects are implemented A. O’Fallon, J. Hagemeister

Abstraction/Information Hiding (II) Prevents access to data aside from the methods specified by the object Guarantees integrity of data Access specifiers in C++ control the access to information public, protected, and private A. O’Fallon, J. Hagemeister

Programming in C++ When programming in an object-oriented language, we’ll be exposed to encapsulation, abstraction, and information hiding in action We need to start thinking in an object-oriented way so that we can leverage the software design benefits of objects and the richness of C++! Always remember, objects contain data and associated operations! A. O’Fallon, J. Hagemeister

Basics of C++ and I/O (I) In C++, just like in C, every program begins execution with function main () To perform input and output (I/O) we need to include the C++ Standard Library <iostream> Essentially replaces <stdio.h>, but with even more richness and convenience A. O’Fallon, J. Hagemeister

Basics of C++ and I/O (II) In tandem with including <iostream>, we’ll need to use the following: A standard output stream object (std::cout) and stream insertion operator (<<) to display information on the screen Replaces the need for printf () A standard input stream object (std::cin) and the stream extraction operator (>>) to read data from the keyboard Replaces the need for scanf () A. O’Fallon, J. Hagemeister

Basics of C++ and I/O Example #include <iostream> using std::cin; // replaces need for std:: in front of cin using std::cout; // replaces need for std:: in front of cout using std::endl; // replaces need for std:: in front of endl int main (void) { int n1 = 0; cout << “Enter a number: “; cin >> n1; // Notice no address of (&) required! int n2 = 0, sum = 0; // Can declare variables right // before their use in C++! cout << “Enter a second number: “; cin >> n2; sum = n1 + n2; cout << “The sum is: “ << sum << endl; // endl outputs a // newline, then flushes buffer return 0; } A. O’Fallon, J. Hagemeister

References and Reference Parameters (I) There are two ways to pass arguments to functions in C++ Pass-by-value (PBV) – a copy of the contents/value of each argument is made and passed (on the function call stack) to the called function One disadvantage of pass-by-value is copying the contents of a large data item or object introduces longer execution times and memory space In general, should only be used with simple types Passing-by-pointer falls under this category Pass-by-reference (PBR) – NO copy of the contents/value of each argument is made The called function can access the caller’s data directly, and modify the data A. O’Fallon, J. Hagemeister

References and Reference Parameters (II) Thoughts: we don’t use pass-by-reference strictly so that we can modify the data in an object directly, in many cases we use it so that the overhead of copying data is circumvented We use the ampersand (&) to represent pass-by-reference i.e. void cube (int &n); // this is a prototype Don’t confuse with the address of (&) operator! Context determines which one’s in play! Check out: http://www.cplusplus.com/articles/z6vU7k9E/ A. O’Fallon, J. Hagemeister

References and Reference Parameters (III) We can return a reference to a variable as well – however we have to be very careful! i.e. int & someFunction (int &n); If we return a reference to an automatic local variable, the variable becomes “undefined” when the function exits; unless the variable is declared as “static” (keyword) References to undefined variables are called dangling references Note: dangling references and dangling pointers are NOT the same! A. O’Fallon, J. Hagemeister

References and Reference Parameters Example … void cubeByRef (int &n); void cubeByPtr (int *pN); int main (void) { int n = 5; cubeByRef (n); // Don’t need &, the formal parameter list indicates PBR cubeByPtr (&n); // Need address of (&) operator to satisfy pointer; applying PBV } void cubeByRef (int &n) n = n * n * n; // We have direct access to n, don’t need to dereference; // changes are retained void cubeByPtr (int *pN) *pN = (*pN) * (*pN) * (*pN); // Need to dereference to indirectly change value A. O’Fallon, J. Hagemeister

Unary Scope Resolution Operator It’s possible to declare local and global variables of the same name Unary Scope Resolution Operator (::) allows a global variable to be accessed without confusing it with a local variable … int num = 42; // global variable int main (void) { double num = 100.25; // local variable cout << num << endl; // displays 100.25 cout << ::num << endl; // displays 42 } A. O’Fallon, J. Hagemeister

Function Overloading (I) The ability to define multiple functions with the same name Requires that each function has different types of parameters and/or different number of parameters and/or different order of parameters i.e. int cube (int n); double cube (double n); The C++ compiler selects the appropriate function based on the number, types, and order of arguments in the function call A. O’Fallon, J. Hagemeister

Function Overloading (II) We use function overloading to increase readability and understandability Of course, we only want to overload functions that perform similar tasks A. O’Fallon, J. Hagemeister

C++ Standard Template Library (STL) Class Vector STL class vector represents a more robust array with many more capabilities May operate with different types of data because they’re templated! i.e. vector<int> v1(10); // declares a 10 element // vector of integers vector<double> v2(5); // declares a 5 element vector // of doubles A. O’Fallon, J. Hagemeister

Closing Thoughts OOP and C++ opens us up to an entirely different world! We need to start thinking more in terms of data and “capsules” instead of just actions and logic Learning C++ is a challenge, but provides features that will increase levels of production! A. O’Fallon, J. Hagemeister C. Hundhausen, A. O’Fallon

References P.J. Deitel & H.M. Deitel, C++: How to Program (9th ed.), Prentice Hall, 2014. A. O’Fallon, J. Hagemeister C. Hundhausen, A. O’Fallon

Collaborators Jack Hagemeister A. O’Fallon, J. Hagemeister C. Hundhausen, A. O’Fallon