Sei sulla pagina 1di 49

AST21105

Object-Oriented Programming and Design

Topic 1
Introduction to C++

Lecture notes are based on textbook and various resources over the Internet
Top 10 Most Popular Programming Languages

Source: https://www.quora.com/How-many-programming-languages-are-there-in-the-world

2
Tiobe Index for August 2017
o C++ is around the number three.

Source: https://www.tiobe.com/tiobe-index/ 3
A "Truthful" Programming
Language Comic A warning here!
C++ isn't easy to learn.

4
Comic source: http://quantlabs.net/blog/2016/02/truthful-programming-language-comic/
A Brief History of C++
o C++ can be said a sequel or extension of the C
programming language.
o Quiz time: Then who invented the C language?
o Answer: By Dennis Ritchie between 1969 and
1973 at Bell Labs, and used to re-implement
the Unix operating system.
n Btw, who was the main inventor of Unix?
n Answer: Ken Thompson

One of my most productive days was


throwing away 1,000 lines of code.
Dennis Ritchie – C Language
Ken Thompson

5
A Brief History of C++
o Ken Thompson and Dennis Ritchie
o 1983 Turing Award
n "For their development of generic operating systems theory
and specifically for the implementation of the UNIX operating
system."
o 1999 US National Medal of Technology
n "For their invention of the
UNIX operating system
and the C programming
language."

6
A Brief History of C++
o In 1979, Bjarne Stroustrup (aged 69 already), a
Danish computer scientist of AT&T Bell Labs,
began work on extending the C language
with classes, and the new language was
called "C with Classes".
o C++, the successor of "C with classes",
was designed by Stroustrup in 1986, and
version 2.0 was introduced in 1989. Bjarne Stroustrup – C++ Language
o The design of C++ was guided by the
following THREE principles:
1. The use of classes would not result in programs executing any
more slowly than programs not using classes.
2. C programs should run as a subset of C++ programs.
3. No run-time inefficiency should be added to the language.

How do you pronounce "Bjarne Stroustrup?"

http://www.stroustrup.com/bs_faq.html#pronounce 7
Evolution of C++ Standards
o C++ is standardized by an ISO working group known
as JTC1/SC22/WG21. So far, it has published five
revisions of the C++ standard and is currently working on
the next revision, C++20.

Year C++ Standard Informal Name Remarks


1998 ISO/IEC 14882:1998 C++98
2003 ISO/IEC 14882:2003 C++03 Bug fixes
2011 ISO/IEC 14882:2011 C++11 Major revision
2014 ISO/IEC 14882:2014 C++14 Bug fixes
2017 ISO/IEC 14882:2017 C++17 Major revision
2020 to be determined C++20

8
Evolution of C++ Standards
o C++98 or C++03:
n 1985: first implementation, C++ is like a C translator
n 1989: add multiple inheritance, type safe linkage across
compilation units
n 1990: add templates (parameterized type), exception
handling
n 2003: fixed multiple problems reported with 1998 standard.
o C++11:
n New features included regular expression support, a
comprehensive randomization library, a new C++ time library,
atomics support, a standard threading library, a new for
loop syntax resembling foreach loops, the auto keyword, new
container classes, better support for unions and array-
initialization lists, and variadic templates.

9
Your First C++ Program
o It simply outputs the
Any program text after // or within /* */ are comments
following message - To describe and explain the operation of the program
- Comments are not translated by the compiler into executable code
Welcome to AST21105

// A very simple program


/*
Author: Garret Lai
Date: 14 January 2019
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Welcome to AST21105" << endl;
return 0;
}

10
Your First C++ Program
Pre-processor directive
o It simply outputs the - Preprocessor is a program that runs before the compiler.
following message - It is to handle directives that control what source code the
compiler sees as input.
Welcome to AST21105 - The #include directive instructs the pre-processor to copy the
contents of the specified file into the program.
- To use the output stream object (i.e. cout) to print a message
// A very simple program "Welcome to AST21105", we need to include the library
/* "iostream" into the program, as this library is where the output
Author: Garret Lai stream object locates.
Date: 14 January 2019
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Welcome to AST21105" << endl;
return 0;
}

"iostream" is the header file name of the standard I/O library in C++. What #include<iostream> does
is to tell the preprocessor that you want the contents of the header called iostream to be included in 11
your sources. This lets the compiler see the code about cin, cout, and a lot of other things.
Your First C++ Program
o It simply outputs the
following message
Welcome to AST21105
// A very simple program
/*
Author: Garret Lai
Date: 14 January 2019
*/

#include <iostream>
using namespace std; Indicate to use the standard I/O stream library in C++
- There is also a standard I/O library (header name: stdio.h) in C.
int main() - But we would like to use the one for C++.
{
cout << "Welcome to AST21105" << endl;
return 0;
}

"std" is an abbreviation of "standard"; std is the standard namespace. 12


Your First C++ Program
o It simply outputs the
following message
Welcome to AST21105
// A very simple program
/* Entry point / starting point of C++ program
Author: Garret Lai - Should be named as main
Date: 14 January 2019 - Return type should be int
*/

#include <iostream>
using namespace std;

int main()
{
cout << "Welcome to AST21105" << endl;
return 0;
} Java: main should be placed in a class
C++: main shouldn’t be placed in a class

13
Your First C++ Program
o It simply outputs the
following message
Welcome to AST21105
// A very simple program
/*
Author: Garret Lai
Date: 14 January 2019
*/

#include <iostream> Output statement


using namespace std; - Print "Welcome to AST21105"
- endl means "end of line" which moves cursor goes to the next line
int main()
{
cout << "Welcome to AST21105" << endl;
return 0;
}
End execution of the function main
- Control is returned to the operating system and 0
means the program ran successfully and no errors
occurred. 14
C++ Program Development Process

C++ Program 1 1. Develop C++ program


2. C++ source code is
C++ source code
preprocessed using
Preprocessor 2
C++ preprocessor
3. Preprocessed code is
Preprocessed code
compiled using C++
Compiler
compiler
3
(C++ compiler converts
Machine code preprocessed code to
machine code)
Execution 4
4. Compiled code
(machine code) is
executed

15
Preprocessor and Preprocessor Directives
o A preprocessor is a program that processes its input data
(C++ code in our case) to produce output that is used as input to
another program (essentially compiler).
o Preprocessor directives (started with symbol #) are instructions to
instruct preprocessor to perform tasks.
n The preprocessor first processes the program code and output a
transformed program text.
n Then, the compiler reads and compiles the transformed program text
o #include is one of the preprocessor directives.
n It instructs the preprocessor to replace the line #include with the
content of the included file:
o #include <iostream> // for pre-defined files
o #include "my_lib.h" // for user-defined files
(Note: there is no semicolon (;) after these statements)

Preprocessor directive in C++ starts with a # sign


16
C++ Compilers
o The list of C++ compilers is just too long.
n Wikipedia: https://en.wikipedia.org/wiki/List_of_compilers#C++_compilers
n Stroustrup's list: http://www.stroustrup.com/compilers.html But both are
incomplete.
o A few notable examples:
n GCC (GNU Project): open-source; it compiles C and C++; the
command for compiling a C++ program is called g++.
n Clang (an LLVM project): relatively new; very active
development; adopted in XCode on Mac.
n Intel C++ Compiler
n Microsoft Visual C++ Compiler

17
Difference and Similarity
between Java & C++

VS

!
Differences among Features to C++ and Java

o C++ and Java are quite similar


n The syntax that you have learned in Java will carry over (mostly,
anyway) to C++
n However, C++ is more complex :P
o Differences (below is only a small part of the full list)
1. Set of data types
2. Can use variables with no initialization
3. Automatic type conversion from int to double, double to int
4. Array (syntax is different,
and no array bound check >.<)
5. Different way to represent
boolean value (true / false)
6. Constant variable declaration
7. Input / output
8. Object assignment

19
Differences among Features
to C++ and Java

1. Set of data types


n Types are exactly the same except:
o No byte type in C++
o Boolean type in Java: boolean
Boolean type in C++: bool
o Characters in Java are represented in unicode (16-bit)
Characters in C++ are represented in ASCII (8-bit)
(More details on the next page)
o In C++, we can declare any of the numeric data types to be
unsigned, e.g.
n unsigned int val1; // val1 can store a value in range 0 to 232 – 1
n signed int val2; // val2 can store a value in range -231 to 231 – 1
n int val3; // val3 can store a value in range -231 to 231 – 1

20
Representation of Character in Computers
o Everything inside computer is a (binary)
number, character is also represented as a
number (one of the schemes is called
ASCII code)
n http://www.asciitable.com
n For example: ‘A’ = 65, ‘B’ = 66,
‘a’ = 97, ‘2’ = 50, ‘<tab>’ = 9
n To allow easy comparison between
characters
o ‘a’ < ‘b’ < ‘c’ < … < ‘z’
o ‘A’ < ‘B’ < ‘C’ < … < ‘Z’
o ‘0’ < ‘1’ < ‘2’ < … < ‘9’
n Being a programmer, it is not necessary to
memorize these encodings

21
Representation of Character in Computers
o Internal representation in C++
n Since there are 256 different characters to represent, the
integral data type char (8-bit) is used to store the ASCII code
of these characters in C++.
o Hence, int valA = (int)’A’;
will store the ASCII code of ‘A’ into the variable valA.
n No memorization is needed!
n Although char is an integral type, its behaviors are usually
different from int.
o For example, outputting a char with cout will print the
corresponding character, instead of underlying number
(ASCII code).

22
Differences among Features to C++ and Java
2. In C++, we are allowed to use variables that are not
initialized.
n Although this is allowed, this is NOT SUGGESTED (actually,
you SHOULDN’T do it).
n C++ standard allows this, and it is actually risky.
Update: Nowadays, most of the modern C++ compilers will show warnings
or even build errors if you use uninitialized variables, so lessening the worry.
// Use of un-initialized variables
Variable with no initialization #include <iostream>
What is the value in it? Garbage value!!! using namespace std;

int main()
{
int val;
cout << "The value of val = " << val << endl;
No error at all! ~.~ val *= 2;
cout << "The value of val = " << val << endl;
return 0;
} 23
Differences among Features to C++ and Java
3. In C++, conversion can be automatically carried out.
o For example, in assignment statements
o double d = 3.5; // initialize d with 3.5
o int x = 2; // initialize x with 2
o x = d; // assign truncated value of d to x
(x = d; is not valid in Java, but perfectly valid in C++.)
(C++ uses truncation to convert a floating point value to integer.)
o For example, in the initialization statement
o double d1 = 3.2; // initialize d1 with 3.2
o int x = d1; // initialize x with trunc(3.2) = 3
o double d2 = x; // initialize d2 with double value 3.0

24
Differences among Features to C++ and Java
4. Syntaxes for declaring an array in Java
and C++ are not the same
n Java:
Should create using new keyword
int n = 255;
int[ ] javaArr = new int[n];

n C++:
Can create an array with or without using new keyword
int n = 255; Memory allocated for the array
int* cppArr1 = new int[n]; WILL NOT BE released by C++,
you should do it yourself! How?

int cppArr2[255]; Will cover this later!

Memory allocated for the array


WILL BE released by C++! 25
Differences among Features to C++ and Java
o Also, array in C++ has no
bound check.
i.e. You can access out of
bound location!

// Use of un-initialized variables


#include <iostream>
using namespace std;

int main()
{
// Can be initialized as in Java
int cppArr[3] = { 1, 2, 3 };
cout << "Value of cppArr[4] = " << cppArr[4] << endl;
return 0;
}
Can even access out of bound location!
- cppArr[4] is actually not a valid location to access
- But NO ERROR at all (However, you SHOULDN’T DO IT)
26
Differences among Features to C++ and Java
o It is also okay to access array
with an negative index.
o Note: You should NOT do so (as
the behavior is undefined)!

// Use of un-initialized variables


#include <iostream>
using namespace std;

int main()
{
// Can be initialized as in Java
int cppArr[3] = { 1, 2, 3 };
cout << "Value of cppArr[-1] = " << cppArr[-1] << endl;
return 0;
}
Can even access out of bound location!
- cppArr[-1] is actually not a valid location to access
- But NO ERROR at all (However, you SHOULDN’T DO IT)
27
Differences among Features
to C++ and Java
5. Different way to represent boolean value true / false
n In Java, the only way to represent true boolean value is using
the keyword true, and false value using false, e.g.
o boolean val1 = true;
o boolean val2 = false;
n But in C++, we have more ways to represent boolean value
o We can still use keyword true to represent boolean value
true, false to represent boolean value false, e.g.
n bool val1 = true;
n bool val2 = false;
o Alternatively, 0 can be used to represent false, non-zero
value to represent true, e.g.
n bool val1 = 1; // 1 is non-zero value, which represents true
n bool val2 = 0; // 0 represents false

28
Differences among Features to C++ and Java
o C++ allows the condition controlling an if statement, while-
statement or do-while statement to be an integer expression.
o If the integer expression evaluates to 0, it is treated the same
as value false, if expression evaluates to non-zero, it is
treated as value true.

// non-zero value to represent true // equivalent program


#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main() int main()


{ {
int val = 1; int val = 1;
if (val) if (val == 1)
cout << "Yes, true!" << endl; cout << "Yes, true!" << endl;
else else
cout << "Oh, false!" << endl; cout << "Oh, false!" << endl;
return 0; return 0;
} }
29
Question
o What is the output of the following program?

#include <iostream>
using namespace std;
Be careful NOT to write the '=' sign for
int main() comparison of two operands in the if-
{
int a = 0;
statement's condition; Single = means
int b = 1; assignment; remember to write '=='
if (a = b) instead for comparison.
cout << "Yes, true!" << endl;
else
cout << "Oh, false!" << endl;
return 0;
}
Output:
Yes, true!

30
Differences among Features to C++ and Java
6. Constant variable declaration
n In Java, we define a constant variable using the keyword
"final", e.g.
o final int VAL = 10;
n But in C++, we use the keyword "const" to do so, e.g.
o const int VAL = 10;
#include <iostream>
using namespace std;

int main()
{
const double PI = 3.14159;
double r = 2.0;
cout << "Area of circle with R = 2 is " << PI*r*r << endl;
return 0;
}

31
Differences among Features to C++ and Java
7. C++ and Java do input and output (I/O) in very different
fashion.
n In C++, I/O can be done more easily compared with Java.
n To output something on the console screen, we use cout.

Syntax:
#include <iostream>
using namespace std; Have to put these statements

cout << <variable1 / value1 / string1 / expression1>


<< <variable2 / value2 / string2 / expression2>
<< …
<< <variablen / valuen / stringn / expressionn>;

// Go to the next line, we add "endl"


cout << <variable / value / string / expression> << … << endl;
where <variablen / valuen /stringn> is a variable,
a value, a string or an expression
32
Screen
Example: cout output:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello" << ‘ ’ << "World" << endl;
cout << "Hello" << " " << "World" << endl;

int a = 10;
int b = 20;
cout << "The value of a and b: " << a << " " << b << endl;

cout << 1 << " " << 2 << endl;

const double PI = 3.14159;


double r = 2.0;
cout << "Area of circle with R = 2 is " << PI*r*r << endl;
return 0;
}

cout << <variable / value / string / expression>;


33
monitor <variable / value / string / expression>
Differences among Features to C++ and Java
o To obtain data from keyboard, we use cin.

#include <iostream>
using namespace std;
Syntax:
int main() #include <iostream>
Have to put these statements
{ using namespace std;
// obtain integer values cin >> <variable1> >> <variable2> >> … >> <variablen>;
int val1, val2;
cin >> val1 >> val2; where <variable1>, <variable2>, …, <variablen> are variables
// obtain double values
int val3, val4;
cin >> val3 >> val4;
// obtain characters
char val5, val6; cin >> <variable>;
cin >> val5 >> val6;
return 0; keyboard <variable>
}

34
Differences among Features to C++ and Java
8. Object assignment
n This is amongst the biggest difference between C++ and
Java that we couldn't miss mentioning.
n When you declare a variable and create an "object", there
are different semantics about the data type of the variable in
Java and C++. Assigning one "object" to another also makes
a different result.
o The details are best explained in this post.
o Next slides strive to summarize certain main points.

35
Differences among Features to C++ and Java
8. Object assignment
n Suppose you declare a class named Foo; that also
introduces some types related to that class. In Java, the type
"Foo" would be a reference type. A reference is a pointer to
an object. Suppose you have a variable declared as Foo x;.
"x" is a reference (pointer) to an object of class Foo.
n In Java, objects are not values (the only types are primitive
types and reference types; there are no object types), so
you can never have a variable or expression whose value is
an object. Rather, objects are always hidden behind
references, and are manipulated through these references.
And, we call x an object reference.
n In C++, with a class named "Foo", the type "Foo" is an object
type. The values of this type are object values themselves.

36
Differences among Features to C++ and Java
8. Object assignment
n The C++ equivalent of the "Foo" reference type in Java would
be the pointer type "Foo *". In fact, C++ supports two kinds of
object creation: one is called static allocation; another is
called dynamic allocation.
o For static allocation, e.g. Foo x("A", 1);, x is a Foo object
allocated on the call stack. Note that x itself = the set of
values of Foo's data members, i.e. x = { name = A; id =1}.
o For dynamic allocation, e.g. Foo* x = new Foo("A", 1);, we
need to create a storage for Foo's data members on the
heap via the new operator, which returns an address of the
storage. The address is assigned to the pointer variable x.
This handling is similar to Java's situation (except that we
won't call x a pointer but a reference in Java).

37
Differences among Features to C++ and Java
Suppose class Foo has two fields
8. Object assignment name and id. In code (1) and (2),
x and y are pointers (or Java
n What was said gets pictured below. references) to objects. But in (3),
o Look at these three code snippets. x and y are objects (storing values
of name and id).

(1) Java: (2) C++(dynamic allocation): (3) C++(static allocation):


Foo x = new Foo("A",1); Foo* x = new Foo("A",1); Foo x("A",1);
Foo y = new Foo("B",2); Foo* y = new Foo("B",2); Foo y("B",2);

Foo Foo
name = A name = A
x id = 1 x id = 1
C++ pointers Memory
(or Java references) Foo for objects Foo objects
y
name = B y name = B
id = 2 id = 2

Stack Heap Stack


38
Differences among Features to C++ and Java
8. Object assignment
n Next, we have to understand an important difference: if we
assign y = x, then what would happen in code (1), (2) & (3)?
(1) Java: (2) C++(dynamic allocation): (3) C++(static allocation):
Foo x = new Foo("A",1); Foo* x = new Foo("A",1); Foo x("A",1);
Foo y = new Foo("B",2); Foo* y = new Foo("B",2); Foo y("B",2);
y = x; y = x; y = x;

Foo Foo
name = A name = A
x id = 1 x id = 1
C++ pointers Memory
(or Java references) Foo for objects Foo objects
y
X name = B y name = B
id = 2
A
id = 2
1
Stack Heap Stack

y = x → make y point to the object pointed by x 39y


y = x → copy content of x to
Differences among Features to C++ and Java
8. Object assignment
n The answer has been pictorially given on the last page.
n For (1) and (2), the assignment y = x means making y point to
the object pointed by x. So, two pointers x and y point to the
same object. There is no copying of data happened; x and y
now are actually sharing the object: if the program modifies the
object's content (i.e. name and id) via pointer x (e.g. x->name =
"C";), the change can be seen via pointer y (cout << y->name;
will print "C").
n For (3), the assignment y = x causes data copying from object x
to object y. That means y's original values of name and id will be
overwritten by x's ones.
Foo type Foo type
o Later, you'll learn how to
name = A name = A
customize the copying object x id = 1
object x id = 1
action by "overloading"
Foo type Foo type
the assignment (=) y=x
operator in C++. object y name = B object y name = A
id = 2 id = 1
40
Same Features between C++ and Java
o There are some features that are actually the same
in C++ and Java:
n Variable declaration
n Control statements
o if, if-else, if-else-if-else…
o switch
o Condition operator, i.e. ?
o for loop
o while loop
o do-while loop
n Operators
o Arithmetic (+, -, *, /, %, ++, --, +=, -=, *=, /=)
o Relational (>, >=, <, <=, ==, !=)
o Logical (&&, ||, !)
n Manual type casting

41
Standard Libraries
o C++ provides a number of libraries
(i.e. set of functions / methods).
o Let’s see some standard libraries defined by C++.
n cmath (Math functions)
n cctype (Character functions)
n cstring (Character string functions)
n ctime (Time-related functions)
n cstdlib (Commonly-used functions)
o To use cmath, you should to include the library by
#include <cmath>
using namespace std;
n You can include other libraries in similar fashion.
o Good reference: http://www.cplusplus.com/ref/

42
Standard Libraries (cmath)
o double ceil(double x)
n Smallest integer greater than or equal to x
o ceil(3.2) = 4, ceil(3.7) = 4, ceil(-3.2) = -3, ceil(-3.7) = -3
o double floor(double x)
n Largest integer less than or equal to x
o floor(3.2) = 3, floor(3.7) = 3, floor(-3.2) = -4, floor(-3.7) = -4
o Other rounding methods
n Cast as integer
o Truncate the floating-point part
(int)(3.2) = 3, (int)(3.7) = 3, (int)(-3.2) = -3, (int)(-3.7) = -3
o Round to nearest integer
round(3.2) = 3, round(3.7) = 4, round(-3.2) = -3, round(-3.7) = -4

43
Standard Libraries (cmath)
o double sqrt(double x)
n Square root of x
o sqrt(4) = 2
o int abs(int x)
n Absolute value of x (integer version)
o abs(-4) = 4
o double fabs(double x)
n Absolute value of x (floating-point version)
o fabs(-4.2) = 4.2
o Common errors
double x = -3.2; double absX = abs(x);
n C++ will convert x into int and then apply abs
n Result = 3, but not 3.2
n Use fabs instead

44
Standard Libraries (cmath)
o double exp(double x) o double sin(double x)
n ex n Sine of angle x (in radian)
o double pow o double asin(double x)
(double x, double y) n Angle (in radian) whose sine is x
n xy o double cos(double x)
o double log(double x) n Cosine of angle x (in radian)
n Natural log of x o double acos(double x)
o double log10(double x) n Angle (in radian) whose cosine is x
n Log base 10 of x o double tan(double x)
n Tangent of angle x (in radian)
o double atan(double x)
n Angle (in radian) whose tangent is x

45
File Input / Output
o Apart from obtaining data from keyboard and output the
data to screen, data can be read from and write to
computer files (generally in text format, i.e. .txt).
o Data read from file can be used as input of programs and
data write to file make it a permanent storage.
n We called this file I/O
o File I/O can be done easily in C++. It looks very much
similar to obtaining data from keyboard and output data to
screen.

46
Reminder: If you are using Visual Studio, please put the
text file under the root (top-level) directory of the project.
C++ File Input Pre-processor directive
- We need to include the library "fstream" into the program,
as this library is where the file input and output function locates

#include <iostream>
#include <fstream> Create input file stream and open the file
using namespace std; - ifstream fin;
Create an object fin that refers to input file stream
int main() (We could logically treat it as the file to be read) ;)
- fin.open("myinfile.txt");
{ This means to open the file "myinfile.txt" for reading
ifstream fin;
fin.open("myinfile.txt"); Check end of file
int value;
- fin.eof()
while(!fin.eof()) This checks whether we have reached the end of file.
{ If so, returns true. Return false, otherwise
fin >> value;
cout << value << endl; Read data from file
} - fin >> value;
fin.close(); This reads the next data from file and stores it in variable value
return 0;
}
2 Close the file
21 - fin.close();
211 Close the file.
Note: This is important as this releases the resources occupied
2110
myinfile.txt 21105 47
C++ File Output
Pre-processor directive
- We need to include the library "fstream" into the program,
as this library is where the file input and output function locates

#include <iostream> Create output file stream and open the file
#include <fstream> - ofstream fout;
using namespace std; Create an object fout that refers to output file stream
(We could logically treat it as the file to be written) ;)
- fout.open("myoutfile.txt");
int main() This means to open the file "myoutfile.txt" for writing
{
ofstream fout; Write data to file
fout.open("myoutfile.txt");
- fout << arr[i];
int arr[3] = { 10, 21, 25 }; This writes the data in the ith location of array to the file
for(int i=0; i<3; i++)
fout << arr[i] << endl;
Close the file
fout.close();
return 0; - fin.close();
Close the file.
} Note: This is important as this makes sure the data is actually
written to the file. If this statement is omitted, no data will be
10 actually written
21
25
myoutfile.txt

48
A Summary of Useful Functions for File I/O
o <input or output file variable>.open(<filename>)
n Open the file with name <filename> for file input or output
o <input file variable>.get(ch)
n Obtain the next character from file and put it in the char
variable ch
o <output file variable>.put(ch)
n Output the character stored in the char variable ch to file
o <input file variable>.eof()
n Check if we reach the end of file. If so, return true, otherwise
return false
o <input or output file variable>.close()
n Close the file

49

Potrebbero piacerti anche