Object-Oriented
Programming (OOP) and
C++
CptS 122 – Fall 2019
Washington State University
• Object-Oriented Design
• Object-Oriented Programming
(OOP)
• Class and Objects
Concepts we • Data Encapsulation
• Abstraction/Information Hiding
will Cover • C++ I/O
• References and Reference
Parameters
• Unary Scope Resolution Operator
• Function Overloading
Nadra Guizani
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
Nadra Guizani
Object-
Oriented
Design (OOD)
Nadra Guizani
Object-Oriented Design (OOD)
• Programming language model which institutes mechanisms to
support implementing object driven software systems
– C++, C#, Java
• In a procedural program:
- Have data stored in a collection of variables and/or
structures,
- Coupled with a set of functions that perform operations on
the data
-C
Nadra Guizani
Object-Oriented Design (OOD)
• In C, the unit of programming is a function
• In C++ the unit of programming is a class
• 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
Nadra Guizani
Basics of C++ and I/O Example
Nadra Guizani
C to C++ Example
Basics of C++ --
References and Reference Parameters
• There are two ways to pass arguments to functions in C++
• Pass-by-value – 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
• Pass-by-reference – 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
Nadra Guizani
Parameter Reference Example
Nadra Guizani
Unary Scope Resolution Operator
• It’s possible to declare local and global variables of the same name
• (::) allows a global variable to be accessed without confusing it with a local
variable
Nadra Guizani
Function Overloading
• The ability to define multiple functions with the same name
• Requires that each function has
• different types of parameters
• different number of parameters
• different order of parameters
• The C++ compiler selects the appropriate function based on
the number, types, and order of arguments in the function
call
Nadra Guizani
Classes and Objects
What is a class?
– 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
Nadra Guizani
Classes and
Objects
Nadra Guizani
Reference:
• Chapter 2-6: C++ Starting from control structures through objects 8th
edition.
• P.J. Deitel & H.M. Deitel, C++: How to Program (9th ed.), Prentice Hall,
2014.
Nadra Guizani