CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++...
Transcript of CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++...
![Page 1: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/1.jpg)
CSE 230Intermediate Programming
in C and C++C++ as a Better C:
Introduction to OOP
Fall 2017Stony Brook University
Instructor: Shebuti Rayanahttp://www3.cs.stonybrook.edu/~cse230/Ref. Book: C How to Program, 8th edition by Deitel and Deitel
![Page 2: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/2.jpg)
Introduction■ C++, an extension of C, was developed by
Bjarne Stroustrup in 1979 at Bell Laboratories. ■ C++ provides a number of features that
“spruce up” the C language■ C++ was originally called “C with classes”■ The increment operator ++ indicates that
C++ is an enhanced version of C■ The latest version is C++11 standardized
through ANSI and ISO Shebuti Rayana (CS, Stony Brook University) (c) Pearson 2
![Page 3: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/3.jpg)
Programming Paradigms ■ C++ provides Two important programming
paradigms:1. Object Oriented Programming (OOP)– Classes– Encapsulation– Objects– Operator overloading– Inheritance– Polymorphism2. Generic Programming – Function templates– Class templates
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 3
![Page 4: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/4.jpg)
C++ Basics: File name in C vs C++■ In C, file names have the .c (lowercase)
extension is used■ In C++, file names can have one of several
extensions, such as .cpp, .cxx or .C (uppercase)■ We will use .cpp
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 4
![Page 5: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/5.jpg)
How to run your C++ code in gcc?g++ test.c –o test./test■ For C++11:g++ -std=c++11 test.c –o test./test
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 5
![Page 6: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/6.jpg)
Your First Code in C++: Adding Two Integers
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 6
Tells the C++ preprocessor to include the content of input/output stream header
Standard output stream object andStream insertion operator <<
Standard input stream objectStream extraction operator >>
Stream manipulatorOutputs a newline
std:: is required before cout, cin, endl. It specifies that we are using names that belongs to “namespace” std
![Page 7: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/7.jpg)
Your First Code in C++: Adding Two Integers
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 7
Concatenated stream outputs,Cascade outputs of different types
![Page 8: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/8.jpg)
Your First Code in C++: Adding Two Integers
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 8
No return statement in main().If execution reaches the end of main(), it automatically returns 0
![Page 9: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/9.jpg)
Your First Code in C++: Adding Two Integers
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 9
Operator Overloading
![Page 10: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/10.jpg)
C++ Standard Library§ C++ programs consist of pieces called classes
and functions.§ Most C++ programmers take advantage of the
rich collections of classes and functions in the C++ Standard Library.
§ Two parts to learning the C++ “world.” 1. The C++ language itself, and 2. How to use the classes and functions in the
C++ Standard Library. § Many special-purpose class libraries are
supplied by independent software vendors.Shebuti Rayana (CS, Stony Brook University) (c) Pearson 10
![Page 11: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/11.jpg)
C++ Header Files
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 11
![Page 12: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/12.jpg)
C++ Header Files
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 12
And more …
![Page 13: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/13.jpg)
Advantage of C++■ Creating own functions and classes– You will know exactly how they work
■ Using existing collections of functions and classes from C++ Standard Library
– By including suitable header files, containing the prototypes of functions and definitions of various classes
– You can also create your custom header file
■ Inline Function– C++ provides inline function to reduce the overhead
associated with function calls– It is used specifically for small functions
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 13
![Page 14: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/14.jpg)
Def
inin
g an
Inlin
e Fu
nctio
n
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 14
using namespace std;
![Page 15: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/15.jpg)
C++ Keywords
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 15
![Page 16: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/16.jpg)
References and Reference Parameters■ Function arguments are passed in two ways– Call-by-value■ A copy of the arguments value is made– Call-by-reference■ Caller gives called function the ability to access caller’s
data directly■ Pro: No copying overhead like call-by-value for large data■ Con: called function can corrupt the caller’s data– To indicate that a function parameter is passed by
reference, we writetype &var_name;
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 16
![Page 17: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/17.jpg)
Passing by pointer vs Passing by referencevoid swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int a = 10, b = 20;
swap(&a, &b);
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 17
void swap(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
int a = 10, b = 20;
swap(a, b);
![Page 18: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/18.jpg)
Difference in Reference variable and pointer variable■ A reference must refer to an object. Since
references can’t be NULL, they are safer to use.
■ A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
■ A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a reference uses a ‘.'(dot operator)
■ A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 18
![Page 19: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/19.jpg)
References as Alias within a function
int count = 1;
int &cRef = count;
cRef++;// increment count■ Taking address of a reference and comparing references do
not create an error, each operation occurs on the variable for which the reference is an alias.
■ const int &i is a reference to a constant integer type
■ Unless it is reference to a constant a reference is an lvalue(e.g., variable name)
■ It is not a constant or expression that returns an rvalue(e.g., the result of a calculation)
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 19
![Page 20: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/20.jpg)
Returning a reference from a function■ Returning reference from a function is
dangerous ■ When returning reference of a variable from
a function, that variable must be static■ If returned reference is for an automatic
variable then the behavior is undefined■ Such references are called dangling
references
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 20
![Page 21: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/21.jpg)
Unary scope resolution operator ::
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 21
Accessing the global variable
![Page 22: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/22.jpg)
Function Overloading■ Several functions with same name, different parameter list
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 22
![Page 23: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/23.jpg)
Function Overloading■ Several functions with same name, different parameter list
■ Compiler selects the proper function by number, type and order of parameters
■ A common error: Creating overloaded function with identical parameter lists and different return types [gives compilation error].
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 23
![Page 24: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/24.jpg)
Introduction to Object Technology
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 24
■ Objects, or more precisely the classes objects come from, are essentially reusable software components.
– There are date objects, time objects, audio objects, video objects, automobile objects, people objects, etc.
– Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating).
■ Using a modular, object-oriented design-and-implementation approach can make software-development groups much more productive than was possible with earlier techniques
– object-oriented programs are often easier to understand, correct and modify.
![Page 25: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/25.jpg)
Introduction to Object Technology:Example ■ The Automobile as an Object– Let’s begin with a simple analogy. – Suppose you want to drive a car and make it go faster
by pressing its accelerator pedal. – Before you can drive a car, someone has to design it.– A car typically begins as engineering drawings, similar
to the blueprints that describe the design of a house. – Drawings include the design for an accelerator pedal. – Pedal hides from the driver the complex mechanisms
that actually make the car go faster, just as the brake pedal hides the mechanisms that slow the car, and the steering wheel hides the mechanisms that turn the car.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 25
![Page 26: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/26.jpg)
Introduction to Object Technology:Example – Enables people with little or no knowledge of how
engines, braking and steering mechanisms work to drive a car easily.
– Before you can drive a car, it must be built from the engineering drawings that describe it.
– A completed car has an actual accelerator pedal to make the car go faster, but even that’s not enough—the car won’t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 26
![Page 27: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/27.jpg)
Introduction to Object Technology
Member Functions and Classes§ Performing a task in a program requires a
member function§ Houses the program statements that
actually perform its task. § Hides these statements from its user, just
as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 27
![Page 28: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/28.jpg)
Introduction to Object Technology■ In C++, we create a program unit called a
class to house the set of member functions that perform the class’s tasks. ■ A class is similar in concept to a car’s
engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 28
![Page 29: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/29.jpg)
Introduction to Object TechnologyInstantiation § Just as someone has to build a car from its
engineering drawings before you can actually drive a car, you must build an object from a class before a program can perform the tasks that the class’s methods define.
§ An object is then referred to as an instanceof its class.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 29
![Page 30: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/30.jpg)
Introduction to Object TechnologyReuse§ Just as a car’s engineering drawings can be
reused many times to build many cars, you can reuse a class many times to build many objects.
§ Reuse of existing classes when building new classes and programs saves time and effort.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 30
![Page 31: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/31.jpg)
Introduction to Object TechnologyMessages and Member Function Calls§ When you drive a car, pressing its gas pedal
sends a message to the car to perform a task—that is, to go faster.
§ Similarly, you send messages to an object. § Each message is implemented as a
member function call that tells a member function of the object to perform its task.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 31
![Page 32: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/32.jpg)
Introduction to Object TechnologyAttributes and Data Members§ A car has attributes§ Color, its number of doors, the amount of gas
in its tank, its current speed and its record of total miles driven (i.e., its odometer reading).
§ The car’s attributes are represented as part of its design in its engineering diagrams.
§ Every car maintains its own attributes.§ Each car knows how much gas is in its own gas
tank, but not how much is in the tanks of other cars.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 32
![Page 33: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/33.jpg)
Introduction to Object Technology■ An object has attributes that it carries along as
it’s used in a program. ■ Specified as part of the object’s class. ■ A bank account object has a balance attribute
that represents the amount of money in the account.
■ Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank.
■ Attributes are specified by the class’s data members.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 33
![Page 34: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/34.jpg)
Introduction to Object TechnologyEncapsulation§ Classes encapsulate (i.e., wrap) attributes and
member functions into objects—an object’s attributes and member functions are intimately related.
§ Objects may communicate with one another, but they’re normally not allowed to know how other objects are implemented—implementation details are hidden within the objects themselves.
§ Information hiding is crucial to good software engineering.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 34
![Page 35: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/35.jpg)
Introduction to Object TechnologyInheritance§ A new class of objects can be created
quickly and conveniently by inheritance—the new class absorbs the characteristics of an existing class, possibly customizing them and adding unique characteristics of its own.
§ In our car analogy, an object of class “convertible” certainly is an object of the more general class “automobile,” but more specifically, the roof can be raised or lowered.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 35
![Page 36: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/36.jpg)
Introduction to Object TechnologyObject-Oriented Analysis and Design (OOAD)§ How will you create the code (i.e., the program
instructions) for your programs? § Follow a detailed analysis process for
determining your project’s requirements (i.e., defining what the system is supposed to do)
§ Develop a design that satisfies them (i.e., deciding how the system should do it).
§ Carefully review the design (and have your design reviewed by other software professionals) before writing any code.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 36
![Page 37: CSE 230 Intermediate Programming in C and C++cse230/CSE230_L14_IntroToC++.pdfand functions.!Most C++ programmers take advantage of the rich collections of classes and functions in](https://reader033.fdocuments.net/reader033/viewer/2022041816/5e5b110d7c4b0d50f92afd71/html5/thumbnails/37.jpg)
Introduction to Object Technology§ If this process involves analyzing and
designing your system from an object-oriented point of view, it’s called an object-oriented analysis and design (OOAD) process.
§ Languages like C++, Java are object oriented.
§ Object-oriented programming (OOP) allows you to implement an object-oriented design as a working system.
Shebuti Rayana (CS, Stony Brook University) (c) Pearson 37