Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Object Oriented Programming 15/09/2019

Object oriented Programming
Lecture 1: Introduction and revision
Natalia Chaudhry

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 1

Course Title Object Oriented Programming
Credit Hours 3+1
Semester Fall 2019
Prerequisites by  Programming fundamentals
Course(s) and Topics

Course Objectives
• Be able to learn object oriented programming in C++ and decompose a problem into 
objects.
• Be able to understand the difference between object oriented and procedural 
programming 
• Be able to learn constructs used in implementing object oriented programming concepts
• Be able to improve the problem solving skills and apply object oriented techniques to 
solve computing problems.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 2

1
Object Oriented Programming 15/09/2019

Recommended Books
• Object Oiented Programming in C++ by Robert Lafore, 4th Edition, SAMS 
Publishing, 2001.
• C++ Programming, D.S. Malik, Fourth Edition
• C++ How to Program, 9th Edition, by Paul J. Dietel and Harvey Deitel.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 3

Implications

• To make you a good programmer
• Programming takes practice to develop the skill. 
• To become a good programmer, you have to write a certain amount of 
code, make the code work, and fix a certain number of bugs. 
• Watching other people making your code work is not going to help your 
program skills and is a missed opportunity. 
• Running for help at the first sight of a problem is the biggest obstacle for 
one to become a good programmer. 
• No pain, no gain!!!

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 4

2
Object Oriented Programming 15/09/2019

Do’s and Don’ts
• Don’t go for help at the first sight of a problem. Get help in coding as the last 
resort. 
• Don’t work on other people’s code. 
• Don’t cheat and copy code from others 
• Don’t paraphrase code from others either E.g., changing variable names & 
indentations 
• Don’t leak your code to any place There is no difference in terms of penalty 
between copying and being copied. 

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 5

Course Policies
• Mandatory attendance
• No make‐up for missed exams except
(1) Valid reason
(2) notices the instructor before the exam

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 6

3
Object Oriented Programming 15/09/2019

Why OOP?
• Reducing the effort, complexity, and cost of development and maintenance of 
software systems.
• Reducing the time to adapt an existing system (quicker reaction to changes in the 
business environment).
• Flexibility, reusability.
• Increasing the reliability of the system.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 7

If successful, this medium of expression (the object‐oriented way) will be 
significantly easier, more flexible, and efficient than the alternatives as problems 
grow larger and more complex.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 8

4
Object Oriented Programming 15/09/2019

• The most important thing to do when learning C++ is to focus on concepts and 
not get lost in language‐technical details.
• Design techniques is far more important than an understanding of details; that 
understanding comes with time and practice.
• Your purpose in learning C++ must not be simply to learn a new syntax for doing 
things the way you used to, but to learn new and better ways of building systems
• Object‐oriented programming technique enables programmers to build high 
quality programs. While designing and coding a program, the software quality 
metrics must be kept always in mind.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 9

Problems with procedural programming
• Procedural programs (functions and data structures) don’t model the real world very 
well. The real world does not consist of functions.
• Global data can be corrupted by functions that have no business changing it.
• To add new data items, all the functions that access the data must be modified so that 
they can also access these new items.
• The real world consists of objects. Computer programs may contain computer world 
representations of the things (objects) that constitute the solutions of real world 
problems.
• Real world objects have two parts:
• Properties (or state :characteristics that can change),
• Behavior (or abilities :things they can do).
• To solve a programming problem in an object‐oriented language, the programmer no 
longer asks how the problem will be divided into functions, but how it will be divided 
into objects.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 10

10

5
Object Oriented Programming 15/09/2019

What are objects?
• Data abstraction
• Two things captured by this abstraction
• Data representation (What data represents a car object?)
• Interface for interacting with objects, using methods (What are the ways through which we 
can interact with that car?)

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 11

11

What kinds of things become objects in object‐oriented programs?
– Human entities: Employees, customers, salespeople, worker, manager
– Graphics program: Point, line, square, circle, ...
– Mathematics: Complex numbers, matrix
– Computer user environment: Windows, menus, buttons
– Data‐storage constructs: Customized arrays, stacks, linked lists

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 12

12

6
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 13

13

PF Basics

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 14

14

7
Object Oriented Programming 15/09/2019

Basic concepts
• A program is a set of instructions that a computer follows to perform a task. 
• Programming languages, which use words instead of numbers, were invented to ease the task of 
programming. 
• Source Code, Object Code, and Executable Code
• The statements written by the programmer are called source code , and the file they are saved in 
is called the source file .
• After the source code is saved to a file, the process of translating it to machine language can 
begin. During the next phase the compiler steps through the preprocessed source code, 
translating each source code instruction into the appropriate machine language instruction. This 
process will uncover any syntax errors that may be in the program
• If the program is free of syntax errors, the compiler stores the translated machine language 
instructions, which are called object code , in an object file .
• Linker‐> executable code

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 15

15

The #include directive causes the preprocessor to include the contents of another file in
the program.
#include <iostream> preprocessor directive 
using namespace std;
int main()
{
cout<<"Hello World\n";
return 0;
}

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 16

16

8
Object Oriented Programming 15/09/2019

• std is the namespace for the C++ standard library 
• for example, string actually belongs to this namespace, so the full name for string is 
std::string. 
• using namespace std tells the compiler that we want to access the resources in this 
namespace ‐ giving us global, or direct access to the string it holds. 
• The C++ standard library contains many different packages that can be included by their 
headers, one of which is <iostream>

// without using namespace std Now with using namespace std:
#include <iostream> #include <iostream>
using namespace std;
int main() {
cout << "Hello World"; // error int main() {
std::cout << "Hello World"; // outputs Hello World cout << "Hello World"; // outputs Hello World
return 0; return 0;
} }

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 17

17

Variables, named constants, literals
• Named constants
• const double INTEREST_RATE = 0.069;

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 18

18

9
Object Oriented Programming 15/09/2019

If/else , switch statements
#include <iostream> switch (o)
using namespace std; {
int main() case '+':
{ cout << num1 << " + " << num2 << " = " << num1 + num2;
char o; break;
float num1, num2; case '‐':
cout << "Enter an operator (+, ‐, *, /): "; cout << num1 << " ‐ " << num2 << " = " << num1 ‐ num2;
cin >> o; break;
cout << "Enter two operands: "; case '*':
cin >> num1 >> num2; cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, ‐, *, 
/)
cout << "Error! operator is not correct";
break;
}
system("pause");
return 0;
}

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 19

19

Filenames and File Stream Objects
• #include <fstream>
• The fstream header file defines the data types ofstream , ifstream , and 
fstream .

• Ifstream inputFile;
• inputFile.open(“Customers.txt”);

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 20

20

10
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 21

21

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 22

22

11
Object Oriented Programming 15/09/2019

…..
• What are functions?
• Functions prototype
• Function declaration and definition
• Function header and function call 
• Local vs global variables
• Static global variables
• Pass by value
• Pass by reference
• Function overloading

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 23

23

• Functions are commonly used to break a problem down into small manageable 
pieces
• divide and conquer because a large problem is divided into several smaller problems 
that are easily solved
• Why functions?
• Simplify
• Code reuse
• Four parts in definition: Return type, Name, Parameter list, Body

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 24

24

12
Object Oriented Programming 15/09/2019

• func header  and func call


• Jumping around memory (no need to use function unnecessarily)

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 25

25

Function prototype 

• A function prototype (or declaration) eliminates the need to place a function 
definition before all calls to the function.
• The prototype looks similar to the function header, except there is a semicolon at 
the end.

Declaration
Definition

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 26

26

13
Object Oriented Programming 15/09/2019

Sending data to function
• Values that are sent into a function are called arguments
• result = pow(2.0, 4.0);
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
• Pass by value vs pass by reference

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 27

27

Local vs global variables
• A local variable is defined inside a function and is not accessible outside the function. 
• Lifetime of a variable: A function’s local variables exist only while the function is 
executing.
• A global variable is defined outside all functions and is accessible to all functions in its 
scope.
• The scope of a global variable is the portion of the program from the variable 
definition to the end.
• You cannot have two local variables with the same name in the same function
• you can have a local variable or a parameter variable with the same name as a global 
variable, or a global constant. When you do, the name of the local or parameter 
variable shadows the name of the global variable or global constant.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 28

28

14
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 29

29

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 30

30

15
Object Oriented Programming 15/09/2019

Points to consider
• Global variables make debugging difficult. Any statement in a program can change 
the value of a global variable.
• Portability issue: Functions that use global variables are usually dependent on those 
variables
• It is generally permissible to use global constants in a program. (e.g. interest rate)

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 31

31

Static Local Variables

• If a function is called more than 
once in a program, the values 
stored in the function’s local 
variables do not persist between 
function calls.
• Sometimes it’s desirable for a 
program to “remember” what value 
is stored in a local variable between 
function calls.
• Static local variables are not 
destroyed when a function returns. 
They exist for the lifetime of the 
program

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 32

32

16
Object Oriented Programming 15/09/2019

Like global variables, all static local variables are initialized to zero by default.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 33

33

Const vs static
• A const is a promise that you will not try to modify the value once set.
• A static variable means that the object's lifetime is the entire execution of the 
program and it's value is initialized only once before the program startup. 
• Static can change .. constant cannot

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 34

34

17
Object Oriented Programming 15/09/2019

Pass by Value
• When an argument is passed into a parameter, only a copy of the argument’s value is passed. 
• Changes to the parameter do not affect the original argument

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 35

35

Pass by Reference
• When used as parameters, reference variables allow a function to access the 
parameter’s original argument. 
• Changes to the parameter are also made to the argument.
• Reference variables are defined like regular variables, except you place an 
ampersand (&) in front of the name. For example, the following function 
definition makes the parameter refVar a reference variable:
void doubleNum(int &refVar)
{
refVar *= 2;
}
• Here is the prototype for the doubleNum function: void doubleNum(int &);

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 36

36

18
Object Oriented Programming 15/09/2019

 Only variables may be passed by 
reference.
 If you attempt to pass a non‐variable
 argument, such as a literal, a constant, or 
an expression, into a reference 
parameter, an error will result. 
 Using the doubleNum function as an 
example, the following statements will 
generate an error.

 doubleNum(5); // Error
 doubleNum(userNum + 10); // Error

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 37

37

void swap(int& x, int& y)
{  
int tmp = x;
x = y;
y = tmp;
}
int main() 
{
int m = 6;
int n = 10;
cout<<m<<n<<endl;
swap(m,n); // we now invoke the function on the two integer types
// but in fact their corresponding reference types will be passed to the swap function
cout<<m<<n<<endl;
}

https://gabrieletolomei.wordpress.com/miscellanea/programming‐languages/c‐cpp/pass‐by‐value‐vs‐pass‐by‐reference/ 38

38

19
Object Oriented Programming 15/09/2019

Overloading functions
• The function signature is the name of the function and the data types of the function’s parameters in 
the proper order
• Two or more functions may have the same name, as long as their parameter lists are different
• E.g. squareInt and squareDouble
• Note that the function’s return value is not part of the signature.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 39

39

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 40

40

20
Object Oriented Programming 15/09/2019

Arrays

• An array works like a variable that can store a group of values, all of the same type. 
• The values are stored together in consecutive memory locations. Here is a definition of an array of 
integers:
• int days[6];
• An array’s size declarator must be a constant integer expression with a value greater than zero.
• It can be either a literal, as in the previous example, or a named constant, as shown in the following:
const int NUM_DAYS = 6;
int days[NUM_DAYS];

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 41

41

Inputting and Outputting Array Contents

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 42

42

21
Object Oriented Programming 15/09/2019

const int ARRAY_SIZE = 5;


int numbers[ARRAY_SIZE];
for (int count = 0; count < ARRAY_SIZE; count++)
numbers[count] = 99;

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 43

43

Array Initialization
• const int MONTHS = 12;
• int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 44

44

22
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 45

45

• Partial Array Initialization
• When an array is being initialized, C++ does not require a value for every element. It’s 
possible to only initialize part of an array, such as:
• int numbers[7] = {1, 2, 4, 8};
• Implicit Array Sizing
• It’s possible to define an array without specifying its size, as long as you provide an 
initialization
• For example, the following definition creates an array with five elements:
• double ratings[] = {1.0, 1.5, 2.0, 2.5, 3.0};

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 46

46

23
Object Oriented Programming 15/09/2019

• Printing the Contents of an Array
Suppose we have the following array definition:
const int SIZE = 5;
int numbers [SIZE] = {10, 20, 30, 40, 50};

cout << numbers << endl; //Wrong!

for (int count = 0; count < SIZE; count++)
cout << numbers[count] << endl;
• Summing the Values in a Numeric Array
int total = 0; // Initialize accumulator
for (int count = 0; count < NUM_UNITS; count++)
total += units[count];

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 47

47

Arrays as Function Arguments
• To pass an array as an argument to a function, pass the name 
of the array.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 48

48

24
Object Oriented Programming 15/09/2019

Two‐Dimensional Arrays

void showArray(const int numbers[][COLS], int rows)


Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 49

49

Passing Two‐Dimensional Arrays to Functions

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 50

50

25
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 51

51

Example: arrays

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 52

52

26
Object Oriented Programming 15/09/2019

Example: Sum elements of an array

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 53

53

Arrays as Function Arguments

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 54

54

27
Object Oriented Programming 15/09/2019

Array parameters work very much like reference variables.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 55

55

Initialization

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 56

56

28
Object Oriented Programming 15/09/2019

POINTERS
The address operator (&) returns the memory address of a variable.
• displays the variable’s address on the screen:
cout << &amount;
• Pointer variables hold memory addresses. 
• With pointer variables you can indirectly manipulate data stored in other variables
yDiff from reference variable:
• A pointer can be assigned any number of times while a reference cannot be re‐assigned after binding…
• Int x=5; int y=6;  int *p; p=&x;   p=&y; *p=10;
• Int x=5; int y=6; int &r = x;  &r = y; WRONG .. Can’t re‐assign
• Reference is like t[he another name for the same variable…Pointer has its own memory address 
whereas reference shares memory address
• It is not possible to get address of a reference .. the address operator will return the address of the 
referenced value instead
• Pointers are useful for dynamic memory allocation

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 57

57

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 58

58

29
Object Oriented Programming 15/09/2019

ptr could be used to change the contents of the variable x . This is done with the indirection 


operator , which is an asterisk (*).

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 59

59

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 60

60

30
Object Oriented Programming 15/09/2019

Array names can be used as constant pointers, and pointers can be used as array names.
an array name, without brackets and a subscript, actually represents the starting address of the array

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 61

61

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 62

62

31
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 63

63

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 64

64

32
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 65

65

 By REFERENCE 

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 66

66

33
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 67

67

Const pointer to int (address cannot be changed)


Pointer to const int (value cannot be changed)
Wrong
Correct
Correct
Wrong

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 68

68

34
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 69

69

Dynamic Memory Allocation
CONCEPT: Variables may be created and destroyed while a program is running.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 70

70

35
Object Oriented Programming 15/09/2019

There is a substantial difference between declaring a normal array and allocating dynamic memory for a 
block of memory using new. 
The most important difference is that the size of a regular array needs to be a constant expression, and thus 
its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic 
memory allocation performed by new allows to assign memory during runtime using any variable value as 
size.
Int var=4;
int *foo=new int [var]

Static ‐> memory allocated at compile time

What if you don’t know before hand how much memory u need? E.g ask user….
Sol: use dynamic allocation at runtime

Int* iptr = new int;
Int* aptr = new int[10];

Deallocate:
Delete iptr;
Delete [] aptr;

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 71

71

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 72

72

36
Object Oriented Programming 15/09/2019

Return int pointer array 

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 73

73

Abstraction
• Data abstraction refers to providing only essential information to the outside world without presenting
the details.
• Data abstraction is a programming (and design) technique that relies on the separation of interface
and implementation.
• Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the
volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know
its internal details, that is, you do not know how it receives signals over the air or through a cable, how
it translates them, and finally displays them on the screen.
• In C++, we use classes to define our own abstract data types (ADT).

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 74

74

37
Object Oriented Programming 15/09/2019

Data Types
C++ has several primitive data types , or data types that are defined as a basic part of the
Language..e.g bool int char…..
data type defines what values an object may hold and the operations that may be
performed on the object

int x = 1, y = 2, z = 3; data type int is the abstraction, and the variables x , y , and z are concrete occurrences.

Abstract Data Types
An abstract data type (ADT) is a data type created by the programmer and is composed
of one or more primitive data types. The programmer decides what values are acceptable
for the data type, as well as what operations may be performed on the data type

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 75

75

Combining Data into Structures
CONCEPT: C++ allows you to group several variables together into a single item
known as a structure.

The limitation of arrays, however, is that all the elements must be of the same data type. Sometimes a relationship 
exists between items of different types.

C++ gives you the ability to package them together into a structure .

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 76

76

38
Object Oriented Programming 15/09/2019

The tag is the name of the structure

For example, the following statement defines a variable named 
deptHead :
PayRoll deptHead;

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 77

77

Accessing Structure Members
CONCEPT: The dot operator (.) allows you to access structure members in a program
deptHead.empNumber = 475;

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 78

78

39
Object Oriented Programming 15/09/2019

INITIALIZATION
struct CityInfo
{
string cityName;
string state;
long population;
int distance;
};
A variable may then be defined with an initialization list, as shown in the following:
CityInfo location = {"Asheville", "NC", 50000, 28};

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 79

79

It’s important to note that you cannot initialize a structure member in the declaration of the
structure.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 80

80

40
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 81

81

Nested Structures
CONCEPT: It’s possible for a structure variable to be a member of another structure
variable.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 82

82

41
Object Oriented Programming 15/09/2019

File Operations
CONCEPT: A file is a collection of data that is usually stored on a computer’s disk.
Data can be saved to files and then later reused.

The first argument is a string containing the name of the file. 
The second argument is a file access flag that indicates the mode in which you wish to open the file. 
Here is an example.
dataFile.open("info.txt", ios::out);

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 83

83

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 84

84

42
Object Oriented Programming 15/09/2019

end‐of‐file marker . It is a character 
that marks the end of the file and 
is automatically written when the 
file is closed

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 85

85

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 86

86

43
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 87

87

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 88

88

44
Object Oriented Programming 15/09/2019

Problems with >> operator in reading file


Because the operator considers whitespace characters as delimiters

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 89

89

Member Functions for Reading and Writing Files
CONCEPT: File stream objects have member functions for more specialized file
reading and writing.

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 90

90

45
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 91

91

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 92

92

46
Object Oriented Programming 15/09/2019

Recursion

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 93

93

A recursive function is one that calls itself.
Infinite….
void message()
{
cout << "This is a recursive function.\n";
message();
}

Not infinite…
void message(int times)
{
if (times > 0)
{
cout << "This is a recursive function.\n";
message(times − 1);
}
}

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 94

94

47
Object Oriented Programming 15/09/2019

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 95

95

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 96

96

48
Object Oriented Programming 15/09/2019

That’s it

Reference: Starting out with C++ from Control Structures  through Objects by TOny Gaddis 97

97

49

You might also like