163
Object Oriented Programming using C++ 1 Table of Content What is Object-Oriented Programming? ............................................................................ 2 Introduction to C++ ............................................................................................................ 3 Structure of a C++ program .............................................................................................. 10 Variables. Data types. Constants ...................................................................................... 13 Operators ........................................................................................................................... 21 Communication through console ...................................................................................... 27 Control Structures ............................................................................................................. 29 Functions (I)...................................................................................................................... 36 Functions (II). ................................................................................................................... 40 Arrays ................................................................................................................................ 46 Strings of Characters ......................................................................................................... 51 Pointers ............................................................................................................................. 56 Dynamic memory.............................................................................................................. 65 Structures .......................................................................................................................... 69 User defined data types ..................................................................................................... 73 Classes............................................................................................................................... 76 Constructors and destructors ............................................................................................. 78 Overloading Constructors ................................................................................................. 80 Overloading operators ....................................................................................................... 83 Relationships between classes .......................................................................................... 87 Polymorphism ................................................................................................................. 100 Templates ........................................................................................................................ 115 Namespaces..................................................................................................................... 121 Exception handling ......................................................................................................... 124 Advanced Class Type-casting ......................................................................................... 127 Preprocessor directives ................................................................................................... 130 Input/Output with files .................................................................................................... 133

Object Oriented Programing using C++

Embed Size (px)

Citation preview

Page 1: Object Oriented Programing using C++

Object Oriented Programming using C++

1

Table of Content

What is Object-Oriented Programming? ............................................................................ 2 Introduction to C++ ............................................................................................................ 3 Structure of a C++ program .............................................................................................. 10 Variables. Data types. Constants ...................................................................................... 13 Operators........................................................................................................................... 21 Communication through console ...................................................................................... 27 Control Structures ............................................................................................................. 29 Functions (I)...................................................................................................................... 36 Functions (II). ................................................................................................................... 40 Arrays................................................................................................................................ 46 Strings of Characters......................................................................................................... 51 Pointers ............................................................................................................................. 56 Dynamic memory.............................................................................................................. 65 Structures .......................................................................................................................... 69 User defined data types ..................................................................................................... 73 Classes............................................................................................................................... 76 Constructors and destructors............................................................................................. 78 Overloading Constructors ................................................................................................. 80 Overloading operators....................................................................................................... 83 Relationships between classes .......................................................................................... 87 Polymorphism................................................................................................................. 100 Templates........................................................................................................................ 115 Namespaces..................................................................................................................... 121 Exception handling ......................................................................................................... 124 Advanced Class Type-casting......................................................................................... 127 Preprocessor directives ................................................................................................... 130 Input/Output with files.................................................................................................... 133

Page 2: Object Oriented Programing using C++

Object Oriented Programming using C++

2

What is Object-Oriented Programming? Object-oriented programming propagates a slightly different approach to programming problems than the strategy which is usually used in C. The C-way is known as a `procedural approach': a problem is decomposed into subproblems and this process is repeated until the subtasks can be coded. Thus a conglomerate of functions is created, communicating through arguments and variables, global or local (or static).

In contrast, or maybe better: in addition to this, an object-oriented approach identifies the keywords in the problem. These keywords are then depicted in a diagram and arrows are drawn between these keywords to define an internal hierarchy. The keywords will be the objects in the implementation and the hierarchy defines the relationship between these objects. The term object is used here to describe a limited, well-defined structure, containing all information about some entity: data types and functions to manipulate the data.

As an example of an object-oriented approach, an illustration follows:

The employees and owner of a car dealer and auto garage company are paid as follows. First, mechanics who work in the garage are paid a certain sum each month. Second, the owner of the company receives a fixed amount each month. Third, there are car salesmen who work in the showroom and receive their salary each month plus a bonus per sold car. Finally, the company employs second-hand car purchasers who travel around; these employees receive their monthly salary, a bonus per bought car, and a restitution of their travel expenses.

When representing the above salary administration, the keywords could be mechanics, owner, salesmen and purchasers. The properties of such units are: a monthly salary, sometimes a bonus per purchase or sale, and sometimes restitution of travel expenses. When analyzing the problem in this manner we arrive at the following representation:

• The owner and the mechanics can be represented as the same type, receiving a given salary per month. The relevant information for such a type would be the monthly amount. In addition this object could contain data as the name, address and social security number.

• Car salesmen who work in the showroom can be represented as the same type as above but with extra functionality: the number of transactions (sales) and the bonus per transaction.

In the hierarchy of objects we would define the dependency between the first two objects by letting the car salesmen be `derived' from the owner and mechanics.

• Finally, there are the second-hand car purchasers. These share the functionality of the salesmen except for the travel expenses. The additional functionality would therefore consist of the expenses made and this type would be derived from the salesmen.

The hierarchy of the thus identified objects further illustrated in figure 1.

Page 3: Object Oriented Programing using C++

Object Oriented Programming using C++

3

figure 1: Hierarchy of objects in the salary administration.

The overall process in the definition of a hierarchy such as the above starts with the description of the most simple type. Subsequently more complex types are derived, while each derivation adds a little functionality. From these derived types, more complex types can be derived ad infinitum, until a representation of the entire problem can be made.

In C++ each of the objects can be represented in a class containing the necessary functionality to do useful things with the variables (called objects) of these classes. Not all of the functionality and not all of the properties of a class is usually available to objects of other classes. As we will see, classes tend to encapsulate their properties in such a way that they are not immediately accessible from the outside world. Instead, dedicated functions are normally used to reach or modify the properties of objects

Introduction to C++ Today you will get started on your way to becoming a proficient C++ programmer. You'll learn

• Why C++ is the emerging standard in software development. • The steps to develop a C++ program. • How to enter, compile, and link your first working C++ program.

A Brief History of C++

Computer languages have undergone dramatic evolution since the first electronic computers were built to assist in telemetry calculations during World War II. Early on, programmers worked with the most primitive computer instructions: machine language. These instructions were represented by long strings of ones and zeroes. Soon, assemblers were invented to map machine instructions to human-readable and -manageable mnemonics, such as ADD and MOV.

In time, higher-level languages evolved, such as BASIC and COBOL. These languages let people work with something approximating words and sentences, such as Let I = 100. These instructions were translated back into machine language by interpreters and compilers. An interpreter translates a program as it reads it, turning the program instructions, or code, directly into actions. A compiler translates the code into an intermediary form. This step is called compiling, and produces an object file. The compiler then invokes a linker, which turns the object file into an executable program.

Because interpreters read the code as it is written and execute the code on the spot, interpreters are easy for the programmer to work with. Compilers, however, introduce the extra steps of compiling and linking the code, which is inconvenient. Compilers produce a program that is very

Page 4: Object Oriented Programing using C++

Object Oriented Programming using C++

4

fast each time it is run. However, the time-consuming task of translating the source code into machine language has already been accomplished.

Another advantage of many compiled languages like C++ is that you can distribute the executable program to people who don't have the compiler. With an interpretive language, you must have the language to run the program.

For many years, the principle goal of computer programmers was to write short pieces of code that would execute quickly. The program needed to be small, because memory was expensive, and it needed to be fast, because processing power was also expensive. As computers have become smaller, cheaper, and faster, and as the cost of memory has fallen, these priorities have changed. Today the cost of a programmer's time far outweighs the cost of most of the computers in use by businesses. Well-written, easy-to-maintain code is at a premium. Easy- to-maintain means that as business requirements change, the program can be extended and enhanced without great expense.

Programs

The word program is used in two ways: to describe individual instructions, or source code, created by the programmer, and to describe an entire piece of executable software. This distinction can cause enormous confusion, so we will try to distinguish between the source code on one hand, and the executable on the other.

New Term: A program can be defined as either a set of written instructions created by a programmer or an executable piece of software.

Source code can be turned into an executable program in two ways: Interpreters translate the source code into computer instructions, and the computer acts on those instructions immediately. Alternatively, compilers translate source code into a program, which you can run at a later time. While interpreters are easier to work with, most serious programming is done with compilers because compiled code runs much faster. C++ is a compiled language.

Solving Problems

The problems programmers are asked to solve have been changing. Twenty years ago, programs were created to manage large amounts of raw data. The people writing the code and the people using the program were all computer professionals. Today, computers are in use by far more people, and most know very little about how computers and programs work. Computers are tools used by people who are more interested in solving their business problems than struggling with the computer.

Ironically, in order to become easier to use for this new audience, programs have become far more sophisticated. Gone are the days when users typed in cryptic commands at esoteric prompts, only to see a stream of raw data. Today's programs use sophisticated "user-friendly interfaces," involving multiple windows, menus, dialog boxes, and the myriad of metaphors with which we've all become familiar. The programs written to support this new approach are far more complex than those written just ten years ago.

As programming requirements have changed, both languages and the techniques used for writing programs have evolved. While the complete history is fascinating, this book will focus on the transformation from procedural programming to object-oriented programming.

Procedural, Structured, and Object-Oriented Programming

Page 5: Object Oriented Programing using C++

Object Oriented Programming using C++

5

Until recently, programs were thought of as a series of procedures that acted upon data. A procedure, or function, is a set of specific instructions executed one after the other. The data was quite separate from the procedures, and the trick in programming was to keep track of which functions called which other functions, and what data was changed. To make sense of this potentially confusing situation, structured programming was created.

The principle idea behind structured programming is as simple as the idea of divide and conquer. A computer program can be thought of as consisting of a set of tasks. Any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood.

As an example, computing the average salary of every employee of a company is a rather complex task. You can, however, break it down into these subtasks:

1. Find out what each person earns. 2. Count how many people you have. 3. Total all the salaries. 4. Divide the total by the number of people you have.

Totaling the salaries can be broken down into

1. Get each employee's record. 2. Access the salary. 3. Add the salary to the running total. 4. Get the next employee's record.

In turn, obtaining each employee's record can be broken down into

1. Open the file of employees. 2. Go to the correct record. 3. Read the data from disk.

Structured programming remains an enormously successful approach for dealing with complex problems. By the late 1980s, however, some of the deficiencies of structured Programming had became all too clear.

First, it is natural to think of your data (employee records, for example) and what you can do with your data (sort, edit, and so on) as related ideas.

Second, programmers found themselves constantly reinventing new solutions to old problems. This is often called "reinventing the wheel," and is the opposite of reusability. The idea behind reusability is to build components that have known properties, and then to be able to plug them into your program as you need them. This is modeled after the hardware world--when an engineer needs a new transistor, she doesn't usually invent one, she goes to the big bin of transistors and finds one that works the way she needs it to, or perhaps modifies it. There was no similar option for a software engineer.

Page 6: Object Oriented Programing using C++

Object Oriented Programming using C++

6

New Term: The way we are now using computers--with menus and buttons and windows--fosters a more interactive, event-driven approach to computer programming. Event-driven means that an event happens--the user presses a button or chooses from a menu--and the program must respond. Programs are becoming increasingly interactive, and it has became important to design for that kind of functionality.

Old-fashioned programs forced the user to proceed step-by-step through a series of screens. Modern event-driven programs present all the choices at once and respond to the user's actions.

Object-oriented programming attempts to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.

The essence of object-oriented programming is to treat data and the procedures that act upon the data as a single "object"--a self-contained entity with an identity and certain characteristics of its own.

C++ and Object-Oriented Programming

C++ fully supports object-oriented programming, including the four pillars of object-oriented development: encapsulation, data hiding, inheritance, and polymorphism. Encapsulation and Data Hiding When an engineer needs to add a resistor to the device she is creating, she doesn't typically build a new one from scratch. She walks over to a bin of resistors, examines the colored bands that indicate the properties, and picks the one she needs. The resistor is a "black box" as far as the engineer is concerned--she doesn't much care how it does its work as long as it conforms to her specifications; she doesn't need to look inside the box to use it in her design.

The property of being a self-contained unit is called encapsulation. With encapsulation, we can accomplish data hiding. Data hiding is the highly valued characteristic that an object can be used without the user knowing or caring how it works internally. Just as you can use a refrigerator without knowing how the compressor works, you can use a well-designed object without knowing about its internal data members.

Similarly, when the engineer uses the resistor, she need not know anything about the internal state of the resistor. All the properties of the resistor are encapsulated in the resistor object; they are not spread out through the circuitry. It is not necessary to understand how the resistor works in order to use it effectively. Its data is hidden inside the resistor's casing.

C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. Once created, a well-defined class acts as a fully encapsulated entity--it is used as a whole unit. The actual inner workings of the class should be hidden. Users of a well-defined class do not need to know how the class works; they just need to know how to use it. Inheritance and Reuse When the engineers at Acme Motors want to build a new car, they have two choices: They can start from scratch, or they can modify an existing model. Perhaps their Star model is nearly perfect, but they'd like to add a turbocharger and a six-speed transmission. The chief engineer would prefer not to start from the ground up, but rather to say, "Let's build another Star, but let's add these additional capabilities. We'll call the new model a Quasar." A Quasar is a kind of Star, but one with new features.

C++ supports the idea of reuse through inheritance. A new type, which is an extension of an existing type, can be declared. This new subclass is said to derive from the existing type and is sometimes called a derived type. The Quasar is derived from the Star and thus inherits all its qualities, but can add to them as needed. Inheritance and its application in C++ are discussed later on. The new Quasar might respond differently than a Star does when you press down on the

Page 7: Object Oriented Programing using C++

Object Oriented Programming using C++

7

accelerator. The Quasar might engage fuel injection and a turbocharger, while the Star would simply let gasoline into its carburetor. A user, however, does not have to know about these differences. He can just "floor it," and the right thing will happen, depending on which car he's driving.

C++ supports the idea that different objects do "the right thing" through what is called function polymorphism and class polymorphism. Poly means many, and morph means form. Polymorphism refers to the same name taking many forms, and is discussed later on .

How C++ Evolved

As object-oriented analysis, design, and programming began to catch on, Bjarne Stroustrup took the most popular language for commercial software development, C, and extended it to provide the features needed to facilitate object-oriented programming. He created C++, and in less than a decade it has gone from being used by only a handful of developers at AT&T to being the programming language of choice for an estimated one million developers worldwide. It is expected that by the end of the decade, C++ will be the predominant language for commercial software development.

While it is true that C++ is a superset of C, and that virtually any legal C program is a legal C++ program, the leap from C to C++ is very significant. C++ benefited from its relationship to C for many years, as C programmers could ease into their use of C++. To really get the full benefit of C++, however, many programmers found they had to unlearn much of what they knew and learn a whole new way of conceptualizing and solving programming problems.

The ANSI Standard

The Accredited Standards Committee, operating under the procedures of the American National Standards Institute (ANSI), is working to create an international standard for C++.

The draft of this standard has been published, and a link is available at www.libertyassociates.com.

The ANSI standard is an attempt to ensure that C++ is portable--that code you write for Microsoft's compiler will compile without errors, using a compiler from any other vendor. Further, because the code in this book is ANSI compliant, it should compile without errors on a Mac, a Windows box, or an Alpha.

For most students of C++, the ANSI standard will be invisible. The standard has been stable for a while, and all the major manufacturers support the ANSI standard. We have endeavored to ensure that all the code in this edition of this book is ANSI compliant.

Should I Learn C First?

The question inevitably arises: "Since C++ is a superset of C, should I learn C first?" Stroustrup and most other C++ programmers agree. Not only is it unnecessary to learn C first, it may be advantageous not to do so. This book attempts to meet the needs of people like you, who come to C++ without prior experience of C. In fact, this book assumes no programming experience of any kind.

Page 8: Object Oriented Programing using C++

Object Oriented Programming using C++

8

Preparing to Program

C++, perhaps more than other languages, demands that the programmer design the program before writing it. Trivial problems, such as the ones discussed in the first few chapters of this book, don't require much design. Complex problems, however, such as the ones professional programmers are challenged with every day, do require design, and the more thorough the design, the more likely it is that the program will solve the problems it is designed to solve, on time and on budget. A good design also makes for a program that is relatively bug-free and easy to maintain. It has been estimated that fully 90 percent of the cost of software is the combined cost of debugging and maintenance. To the extent that good design can reduce those costs, it can have a significant impact on the bottom-line cost of the project.

The first question you need to ask when preparing to design any program is, "What is the problem I'm trying to solve?" Every program should have a clear, well-articulated goal, and you'll find that even the simplest programs in this book do so.

The second question every good programmer asks is, "Can this be accomplished without resorting to writing custom software?" Reusing an old program, using pen and paper, or buying software off the shelf is often a better solution to a problem than writing something new. The programmer who can offer these alternatives will never suffer from lack of work; finding less-expensive solutions to today's problems will always generate new opportunities later.

Assuming you understand the problem, and it requires writing a new program, you are ready to begin your design.

Your Development Environment

This book makes the assumption that your computer has a mode in which you can write directly to the screen, without worrying about a graphical environment, such as the ones in Windows or on the Macintosh.

Your compiler may have its own built-in text editor, or you may be using a commercial text editor or word processor that can produce text files. The important thing is that whatever you write your program in, it must save simple, plain-text files, with no word processing commands embedded in the text. Examples of safe editors include Windows Notepad, the DOS Edit command, Brief, Epsilon, EMACS, and vi. Many commercial word processors, such as WordPerfect, Word, and dozens of others, also offer a method for saving simple text files.

The files you create with your editor are called source files, and for C++ they typically are named with the extension .CPP, .CP, or .C. In this book, we'll name all the source code files with the .CPP extension, but check your compiler for what it needs.

NOTE: Most C++ compilers don't care what extension you give your source code, but if you don't specify otherwise, many will use .CPP by default.

DO use a simple text editor to create your source code, or use the built-in editor that comes with your compiler. DON'T use a word processor that saves special formatting characters. If you do use a word processor, save the file as ASCII text. DO save your files with the .C, .CP, or .CPP extension. DO check your documentation for specifics about your compiler and linker to ensure that you know how to compile and link your programs.

Page 9: Object Oriented Programing using C++

Object Oriented Programming using C++

9

Compiling the Source Code

Although the source code in your file is somewhat cryptic, and anyone who doesn't know C++ will struggle to understand what it is for, it is still in what we call human-readable form. Your source code file is not a program, and it can't be executed, or run, as a program can.

To turn your source code into a program, you use a compiler. How you invoke your compiler, and how you tell it where to find your source code, will vary from compiler to compiler; check your documentation. In Borland's Turbo C++ you pick the RUN menu command or type

tc <filename>

from the command line, where <filename> is the name of your source code file (for example, test.cpp). Other compilers may do things slightly differently.

NOTE: If you compile the source code from the operating system's command line, you should type the following:

For the Borland C++ compiler: bcc <filename> For the Borland C++ for Windows compiler: bcc <filename> For the Borland Turbo C++ compiler: tc <filename> For the Microsoft compilers: cl <filename>

After your source code is compiled, an object file is produced. This file is often named with the extension .OBJ. This is still not an executable program, however. To turn this into an executable program, you must run your linker.

Creating an Executable File with the Linker

C++ programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with your compiler, that you purchased separately, or that you created and compiled. All C++ compilers come with a library of useful functions (or procedures) and classes that you can include in your program. A function is a block of code that performs a service, such as adding two numbers or printing to the screen. A class is a collection of data and related functions.

The steps to create an executable file are

1. Create a source code file, with a .CPP extension. 2. Compile the source code into a file with the .OBJ extension. 3. Link your OBJ file with any needed libraries to produce an executable program.

The Development Cycle

If every program worked the first time you tried it, that would be the complete development cycle: Write the program, compile the source code, link the program, and run it. Unfortunately, almost

Page 10: Object Oriented Programing using C++

Object Oriented Programming using C++

10

every program, no matter how trivial, can and will have errors, or bugs, in the program. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program.

Whatever type of bug you find, you must fix it, and that involves editing your source code, recompiling and relinking, and then rerunning the program.

Structure of a C++ program Probably the best way to start learning a programming language is with a program. So here is our first program:

// my first program in C++ #include <iostream.h> int main () { cout << "Hello World!"; return 0; }

Hello World!

The left side shows the source code for our first program, which we can name, for example, hiworld.cpp. The right side shows the result of the program once compiled and executed. The way to edit and compile a program depends on the compiler you are using. Depending on if it has a Development Interface or not and on its version. Consult section compilers and the manual or help included with your compiler if you have doubts on how to compile a C++ console program.

The previous program is the first program that most programming apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simpler programs that can be written in C++, but it already includes the basic components that every C++ program has. We are going to take a look at them one by one:

// my first program in C++ This is a comment line. All the lines beginning with two slash signs (//) are considered comments and do not have any effect in the behavior of the program. They can be used by the programmer to include short explanations or observations within the source itself. In this case, the line is a brief description of what our program does.

#include <iostream.h> Sentences that begin with a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler. In this case the sentence #include <iostream.h> tells the compiler's preprocessor to include the iostream standard header file. This specific file includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is used later in the program.

int main () This line corresponds to the beginning of the main function declaration. The main function is the point by where all C++ programs begin their execution. It is independent from

Page 11: Object Oriented Programing using C++

Object Oriented Programming using C++

11

whether it is at the beginning, at the end or by the middle of the code - its content is always the first to be executed when a program starts. In addition, for that same reason, it is essential that all C++ programs have a main function.

main goes followed by a pair of parenthesis () because it is a function. In C++ all functions are followed by a pair of parenthesis () that, optionally, can include arguments within them. The content of the main function follows immediately to its formal declaration enclosed between curly brackets ({}), as in our example.

cout << "Hello World"; This instruction does the most important thing in this program. cout is the standard output stream in C++ (usually the screen), and the full sentence inserts a sequence of characters (in this case "Hello World") into this output stream (the screen). cout is declared in the iostream.h header file, so in order to be able to use it that file must be included.

Notice that the sentence ends with a semicolon character (;). This character has the meaning of finishing the instruction and must be included after every instruction you include in any C++ program (one of the most common errors of C++ programmers is indeed to forget to include a semicolon ; at the end of each instruction).

return 0; The return instruction makes the main() function to finish and return the code that the instruction is followed by, in this case 0. This it is most usual way to terminate a program that has not found any errors during its execution. As you will see in coming examples, all C++ programs end with a sentence similar to this.

Therefore, you may have noticed that not all the lines of this program did an action. There were lines containing only comments (those beginning by //), lines with instructions for the compiler's preprocessor (those beginning by #), then there were lines that initiated the declaration of a function (in this case, the main function) and, finally lines with instructions (like the call to cout <<), these last ones were all included within the block delimited by the curly brackets ({}) of the main function.

The program has been structured in different lines in order to be more readable, but it is not compulsory to do so. For example, instead of

int main () { cout << " Hello World "; return 0; }

we could have written:

int main () { cout << " Hello World "; return 0; }

in just one line and this would have had exactly the same meaning.

In C++ the separation between instructions is specified with an ending semicolon (;) after each one. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.

Here is a program with some more instruction:

Page 12: Object Oriented Programing using C++

Object Oriented Programming using C++

12

// my second program in C++ #include <iostream.h> int main () { cout << "Hello World! "; cout << "I'm a C++ program"; return 0; }

Hello World! I'm a C++ program

In this case we used the cout << method twice in two different instructions. Once again, the separation in different lines of the code has just been done to give greater readability to the program, since main could perfectly have been defined thus:

int main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }

We were also free to divide the code in more lines if we considered it convenient: int main () { cout << "Hello World!"; cout << "I'm a C++ program"; return 0; }

And the result would have been exactly the same than in the previous examples.

Preprocessor directives (those that begin by #) are out of this rule since they are not true instructions. They are lines read and discarded by the preprocessor that finaly do not produce any code. These must be specified in their own line and do not require to include a semicolon (;) at the end.

Comments.

Comments are pieces of source code discarded from the code by the compiler. They do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.

C++ supports two ways to insert comments:

// line comment /* block comment */

The first of them - the line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, the block comment, discards everything between the /* characters and the next appearance of the */ characters, with the possibility to include several lines.

We are going to add comments to our second program:

/* my second program in C++ with more comments */ #include <iostream.h>

Hello World! I'm a C++ program

Page 13: Object Oriented Programing using C++

Object Oriented Programming using C++

13

int main () { cout << "Hello World! "; // says Hello World! cout << "I'm a C++ program"; // says I'm a C++ program return 0; }

If you include comments within the sourcecode of your programs without using the comment characters combinations //, /* nor */, the compiler will take them as if they were C++ instructions and, most likely, it will show up one or several error messages

Variables. Data types. Constants The usefulness of the "Hello World" programs shown in the previous section are something more than questionable. Since we have had to write several lines of code, compile them, and then execute the resulting program to obtain just a sentence on the screen as result. It is true, but programming is not limited only to print texts on screen, it would be much faster to simply write the output sentence by ourselves. In order to go a little further on and to become able to write programs that perform useful tasks that really save us work we need to introduce the concept of variable.

Let's think that I ask you to retain number 5 in your mental memory, and then I ask you to memorize also number 2. You have just stored two values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining numbers 6 (that is 5+1) and 2 in your memory. Values that now we could subtract and obtain 4 as result.

All this process that you have made is a simil of what a computer can do with two variables. This same process can be expressed in C++ with the following instruction set:

a = 5; b = 2; a = a + 1; result = a - b;

Obviously this is a very simple example since we have only used two small integer values, but consider that your computer can store several million of numbers like these at the same time and conduct sophisticated mathematical operations with them.

Therefore, we can define a variable as a portion of memory to store a determined value.

Each variable needs an identifier that distinguishes it from the others, for example, in the previous code the variable identifiers were a, b and result, but we could have called the variables with any name we have wanted to invent, whenever they were valid identifiers.

Identifiers

A valid identifier is a sequence of one or more letters, digits or underline symbols ( _ ). The length of an identifier is not limited, although for some compilers only the 32 first characters of an identifier are significant (the rest are not considered).

Neither spaces nor marked letters can be part of an identifier. Only letters, digits and underline characters are valid. In addition, variable identifiers should always begin with a letter. They can

Page 14: Object Oriented Programing using C++

Object Oriented Programming using C++

14

also begin with an underline character ( _ ), but this is usually reserved for external links. In no case they can begin with a digit.

Another rule that you have to consider when inventing your own identifiers is that they cannot match with any language's key word nor your compiler's specific ones since they could be confused with these, for example, the following expressions are always considered key words according to the ANSI-C++ standard and therefore they must not be used as identifiers:

asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t

Additionally, alternative representations for some operators do not have to be used as identifiers since they are reserved words under some circumstances:

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq

your compiler may also include some more specific reserved keywords. For example, many compilers which generate 16bits code (like some compilers for DOS) include also far, huge and near as key words.

Very important: The C++ language is "case sensitive", that means that a same identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example the variable RESULT is not the same one that the variable result nor variable Result.

Data types

When programming, we store the variables in our computer's memory, but the computer must know what we want to store in them since it is not going to occupy the same space in memory to store a simple number, a letter or a large number.

Our computer's memory is organized in bytes. A byte is the minimum amount of memory which we can manage. A byte can store a relatively small amount of data, usually an integer between 0 and 255 or one single character. But in addition, the computer can manipulate more complex data types that come from grouping several bytes, like long numbers or numbers with decimals. Next you have a list of the existing fundamental data types in C++, as well as the range of values that can be represented with each one of them:

DATA TYPES Name Bytes* Description Range*

char 1 character or integer 8 bits length. signed: -128 to 127 unsigned: 0 to 255

short 2 integer 16 bits length. signed: -32768 to 32767 unsigned: 0 to 65535

long 4 integer 32 bits length. signed:-2147483648 to 2147483647 unsigned: 0 to 4294967295

int * Integer. Its length traditionally depends on the length of the See short, long

Page 15: Object Oriented Programing using C++

Object Oriented Programming using C++

15

system's Word type, thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows 9x/2000/NT and systems that work under protected mode in x86 systems) it is 32 bits long (4 bytes).

float 4 floating point number. 3.4e + / - 38 (7 digits)

double 8 double precision floating point number. 1.7e + / - 308 (15 digits)

long double 10 long double precision floating point

number. 1.2e + / - 4932 (19 digits)

bool 1

Boolean value. It can take one of two values: true or false NOTE: this is a type recently added by the ANSI-C++ standard. Not all compilers support it. Consult section bool type for compatibility information.

true or false

wchar_t 2

Wide character. It is designed as a type to store international characters of a two-byte character set. NOTE: this is a type recently added by the ANSI-C++ standard. Not all compilers support it.

wide characters

* Values of columns Bytes and Range may vary depending on your system. The values included here are the most commonly accepted and used by almost all compilers.

In addition to these fundamental data types there also exist the pointers and the void parameter type specification, that we will see later.

Declaration of variables

In order to use a variable in C++, we must first declare it specifying which of the data types above we want it to be. The syntax to declare a new variable is to write the data type specifier that we want (like int, short, float...) followed by a valid variable identifier. For example:

int a; float mynumber;

Are valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, variables a and mynumber can be used within the rest of their scope in the program.

If you need to declare several variables of the same type and you want to save some writing work you can declare all of them in the same line separating the identifiers with commas. For example:

int a, b, c;

declares three variables (a, b and c) of type int , and has exactly the same meaning as if we had written:

int a; int b; int c;

Page 16: Object Oriented Programing using C++

Object Oriented Programming using C++

16

Integer data types (char, short, long and int) can be signed or unsigned according to the range of numbers that we need to represent. Thus to specify an integer data type we do it by putting the keyword signed or unsigned before the data type itself. For example:

unsigned short NumberOfSons; signed int MyAccountBalance;

By default, if we do not specify signed or unsigned it will be assumed that the type is signed, thus in the second declaration we could have written:

int MyAccountBalance;

with exactly the same meaning and being this last one the most usual, in fact, few source codes include the keyword signed as part of a compound type name.

The only exception to this rule is the char type that exists by itself and it is considered a diferent type than signed char and unsigned char.

Finally, signed and unsigned may also be used as a simple types, meaning the same as signed int and unsigned int respectivelly. The following two declarations are equivalent:

unsigned MyBirthYear; unsigned int MyBirthYear;

To see in action how looks like a declaration in a program, we are going to see the C++ code of the example about your mental memory proposed at the beginning of this section:

// operating with variables #include <iostream.h> int main () { // declaring variables: int a, b; int result; // process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0; }

4

Do not worry if something out of the varibale declarations sounds a bit strange to you. You will see the rest in detail in coming sections.

Initialization of variables

Page 17: Object Oriented Programing using C++

Object Oriented Programming using C++

17

When declaring a local variable, its value is undetermined by default. But you may want that a variable stores a concrete value since the moment in which it is declared. In order to do that, you have to append an equal sign followed by the value wanted to the variable declaration:

type identifier = initial_value ;

For example, if we want to declare an int variable called a that contains the value 0 since the moment in which it is declared, we could write:

int a = 0;

Additionally to this way of initializating variables (known as c-like), C++ has added a new way to initialize a variable: by enclosing the initial value between parenthesis ():

type identifier (initial_value) ;

For example:

int a (0);

Both ways are valid and equivalent in C++.

Scope of variables

All the variables that we are going to use must have been previously declared. An important difference between the C and C++ languages, is that in C++ we can declare variables anywhere in the source code, even between two executable sentences, and not only at the beginning of a block of instructions, like happens in C.

Anyway, it can be recommendable under some circumstances to follow the indications of the C language when declaring variables, since it can be useful at the time of debugging a program to have all the declarations grouped together. Therefore, the traditional C-like way to declare variables is to include their declaration at the beginning of each function (for local variables) or directly in the body of the program outside any function (for global variables).

Page 18: Object Oriented Programing using C++

Object Oriented Programming using C++

18

Global variables can be referred anywhere in the code, within any function, whenever it is after its declaration.

The scope of the local variables is limited to the code level in which they are declared. If they are declared at the beginning of a function (like in main) its scope is the whole main function. This means that if in the example above, moreover than the function main() another function existed, the local variables declared in main could not

be used in the other function and vice versa.

In C++, the scope of a local variable is given by the block in which it is declared (a block is a group of instructions grouped together within curly brackets {} signs). If it is declared within a function it will be a variable with function scope, if it is declared in a loop its scope will be only the loop, etc...

In addition to local and global scopes exists the external scope, that causes a variable to be visible not only in the same source file but in all other files which will be linked together with.

Constants: Literals.

A constant is any expression that has a fixed value. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings.

Integer Numbers

1776 707 -273

they are numerical constants that identify integer decimal numbers. Notice that to express a numerical constant we do not need to write quotes (") nor any special character. There is no doubt that it is a constant: whenever we write 1776 in a program we will be referring to the value 1776.

In addition to decimal numbers (those that all of us already know) C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal numbers (base 16). If we want to express an octal number we must precede it with a 0 character (zero character). And to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:

75 // decimal 0113 // octal 0x4b // hexadecimal

Page 19: Object Oriented Programing using C++

Object Oriented Programming using C++

19

All of them represent the same number: 75 (seventy five) expressed as a radix-10 number, octal and hexdecimal, respectively.

[ Note: You can find more information on hexadecimal and octal representations in the document Numerical radixes]

Floating Point Numbers They express numbers with decimals and/or exponent. They can include a decimal point, an e character (that expresses "by ten at the Xth height", where X is the following integer value) or both.

3.14159 // 3.14159 6.02e23 // 6.02 x 1023 1.6e-19 // 1.6 x 10-19 3.0 // 3.0

these are four valid numbers with decimals expressed in C++. The first number is PI, the second one is the number of Avogadro, the third is the electric charge of an electron (an extremely small number) -all of them approximated- and the last one is the number 3 expressed as a floating point numeric literal.

Characters and strings There also exist non-numerical constants, like:

'z' 'p' "Hello world" "How do you do?"

The first two expressions represent single characters, and the following two represent strings of several characters. Notice that to represent a single character we enclose it between single quotes (') and to express a string of more than one character we enclose them between double quotes (").

When writing both single characters and strings of characters in a constant way, it is necessary to put the quotation marks to distinguish them from possible variable identifiers or reserved words. Notice this:

x 'x'

x refers to variable x, whereas 'x' refers to the character constant 'x'.

Character constants and string constants have certain peculiarities, like the escape codes. These are special characters that cannot be expressed otherwise in the sourcecode of a program, like newline (\n) or tab (\t). All of them are preceded by an inverted slash (\). Here you have a list of such escape codes:

\n newline \r carriage return \t tabulation \v vertical tabulation \b backspace \f page feed

Page 20: Object Oriented Programing using C++

Object Oriented Programming using C++

20

\a alert (beep) \' single quotes (') \" double quotes (") \ question (?) \\ inverted slash (\)

For example:

'\n' '\t' "Left \t Right" "one\ntwo\nthree"

Additionally, you can express any character by its numerical ASCII code by writing an inverted slash bar character (\) followed by the ASCII code expressed as an octal (radix-8) or hexadecimal (radix-16) number. In the first case (octal) the number must follow immediately the inverted slash (for example \23 or \40), in the second case (hexacedimal), you must put an x character before the number (for example \x20 or \x4A). [Consult the document ASCII Code for more information about this type of escape code].

coonstants of string of characters can be extended by more than a single code line if each code line ends with an inverted slash (\):

"string expressed in \ two lines"

You can also concatenate several string constants separating them by one or several blankspaces, tabulators, newline or any other valid blank character:

"we form" "a single" "string" "of characters"

Defined constants (#define)

You can define your own names for constants that you use quite often without having to resource to variables, simply by using the #define preprocessor directive. This is its format:

#define identifier value

For example:

#define PI 3.14159265 #define NEWLINE '\n' #define WIDTH 100

they define three new constants. Once they are declared, you are able to use them in the rest of the code as any if they were any other constant, for example:

circle = 2 * PI * r; cout << NEWLINE;

In fact the only thing that the compiler does when it finds #define directives is to replace literally any occurrence of the them (in the previous example, PI, NEWLINE or WIDTH) by the code to which they have been defined (3.14159265, 'n' and 100, respectively). For this reason, #define constants are considered macro constants.

Page 21: Object Oriented Programing using C++

Object Oriented Programming using C++

21

The #define directive is not a code instruction, it is a directive for the preprocessor, reason why it assumes the whole line as the directive and does not require a semicolon (;) at the end of it. If you include a semicolon character (;) at the end, it will also be added when the preprocessor will substitute any occurence of the defined constant within the body of the program.

declared constants (const)

With the const prefix you can declare constants with a specific type exactly as you would do with a variable:

const int width = 100; const char tab = '\t'; const zip = 12440;

In case that the type was not specified (as in the last example) the compiler assumes that it is type int.

Operators Once we know of the existence of variables and constants we can begin to operate with them. For that purpose, C++ provides the operators, which in this language are a set of keywords and signs that are not part of the alphabet but are available in all keyboards. It is important to know them since they are the basis of the C++ language.

You do not have to memorize all the content of this page, since the details are only provided to serve you as a later reference in case you need it.

Assignation (=). The assignation operator serves to assign a value to a variable.

a = 5;

assigns the integer value 5 to variable a. The part at the left of the = operator is known as lvalue (left value) and the right one as rvalue (right value). lvalue must always be a variable whereas the right side can be either a constant, a variable, the result of an operation or any combination of them.

It is necessary to emphasize that the assignation operation always takes place from right to left and never at the inverse.

a = b;

assigns to variable a (lvalue) the value that contains variable b (rvalue) independently of the value that was stored in a at that moment. Consider also that we are only assigning the value of b to a and that a later change of b would not affect the new value of a.

For example, if we take this code (with the evolution of the variables' content in green color):

int a, b; // a:? b:? a = 10; // a:10 b:? b = 4; // a:10 b:4 a = b; // a:4 b:4

Page 22: Object Oriented Programing using C++

Object Oriented Programming using C++

22

b = 7; // a:4 b:7 it will give us as result that the value contained in a is 4 and the one contained in b is 7. The final modification of b has not affected a, although before we have declared a = b; (right-to-left rule).

A property that has the assignation operation in C++ over other programming languages is that an assignation operation can be used as rvalue (or part of an rvalue) for another assignation. For example:

a = 2 + (b = 5);

is equivalent to:

b = 5; a = 2 + b;

that means: first assign 5 to variable b and then assign to a the value 2 plus the result of the previous assignation of b (that is 5), remaining a with a final value of 7. Thus, the following expression is also valid in C++:

a = b = c = 5;

assigns 5 to the three variables a, b and c. Arithmetic operators ( +, -, *, /, % )

The five arithmetical operations supported by the language are: + addition - subtraction * multiplication / division % module

Operations of addition, subtraction, multiplication and division would not suppose an understanding challenge for you since they literally correspond with their respective mathematical operators.

The only one that may not be known by you is the module, specified with the percentage sign (%). Module is the operation that gives the rest of a division of two integer values. For example, if we write a = 11 % 3;, the variable a will contain 2 as result since 2 is the rest from dividing 11 between 3.

Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) A feature of assignation in C++ that contributes to its fame of sparing language when writing are the compound assignation operators (+=, -=, *= and /= among others), which allow to modify the value of a variable with one of the basic operators:

value += increase; is equivalent to value = value + increase; a -= 5; is equivalent to a = a - 5; a /= b; is equivalent to a = a / b; price *= units + 1; is equivalent to price = price * (units + 1);

and the same for all other operations. Increase and decrease.

Page 23: Object Oriented Programing using C++

Object Oriented Programming using C++

23

Another example of saving when writing code are the increase operator (++) and the decrease operator (--). They increase or reduce by 1 the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:

a++; a+=1; a=a+1;

are all equivalent in its functionality: the three increase by 1 the value of a.

Its existence is due to that in the first C compilers the three previous expressions produced different executable code according to which one was used. Nowadays this type of code optimization is generally done automatically by the compiler.

A characteristic of this operator is that it can be used both as a prefix or as a suffix. That means it can be written before the variable identifier (++a) or after (a++) and, although in so simple expressions like a++ or ++a they have exactly the same meaning, in other operations in which the result of the increase or decrease operation is evaluated as another expression they may have an important difference in their meaning: In case that the increase operator is used as a prefix (++a) the value is increased before the expression is evaluated and therefore the increased value is considered in the expression; in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the expression. Notice the difference:

Example 1 Example 2 B=3; A=++B; // A is 4, B is 4

B=3; A=B++; // A is 3, B is 4

In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and B is later increased.

Relational operators ( ==, !=, >, <, >=, <= ) In order to evaluate a comparison between two expressions we can use the Relational operators. As specified by the ANSI-C++ standard, the result of a relational operation is a bool value that can only be true or false, according to the result of the comparison.

We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other. Here is a list of the relational operators that can be performed in C++:

== Equal != Different > Greater than < Less than >= Greater or equal than <= Less or equal than

Here you have some examples: (7 == 5) would return false. (5 > 4) would return true. (3 != 2) would return true. (6 >= 6) would return true.

Page 24: Object Oriented Programing using C++

Object Oriented Programming using C++

24

(5 < 5) would return false. of course, instead of using only numberic constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6,

(a == 5) would return false. (a*b >= c) would return true since (2*3 >= 6) is it. (b+4 > a*c) would return false since (3+4 > 2*6) is it. ((b=2) == a) would return true.

Be aware. Operator = (one equal sign) is not the same as operator == (two equal signs), the first is an assignation operator (assigns the right side of the expression to the variable in the left) and the other (==) is a relational operator of equality that compares whether both expressions in the two sides of the operator are equal to each other. Thus, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also stores value 2, so being true the result of the operation.

In many compilers previous to the publication of the ANSI-C++ standard, as well as in the C language, the relational operations did not return a bool value true or false, rather they returned an int as result with a value of 0 in order to represent "false" and a value

different from 0 (generally 1) to represent "true". For more information, or if your compiler does not support the bool type, consult the section bool type.

Logic operators ( !, &&, || ). Operator ! is equivalent to boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to invert the value of it, producing false if its operand is true and true if its operand is false. It is like to say that it returns the opposite result of evaluating its operand. For example:

!(5 == 5) returns false because the expression at its right (5 == 5) would be true. !(6 <= 4) returns true because (6 <= 4) would be false. !true returns false. !false returns true.

Logic operators && and || are used when evaluating two expressions to obtain a single result. They correspond with boolean logic operations AND and OR respectively. The result of them depends on the relation between its two operands:

First Operand a

Second Operand b

result a && b

result a || b

true true true true

true false false true

false true false true

false false false false For example:

( (5 == 5) && (3 > 6) ) returns false ( true && false ). ( (5 == 5) || (3 > 6)) returns true ( true || false ).

Conditional operator ( ? ). The conditional operator evaluates an expression and returns a different value according to the evaluated expression, depending on whether it is true or false. Its format is:

condition ? result1 : result2

if condition is true the expression will return result1, if not it will return result2. 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5.

Page 25: Object Oriented Programing using C++

Object Oriented Programming using C++

25

7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2. 5>3 ? a : b returns a, since 5 is greater than 3. a>b ? a : b returns the greater one, a or b.

Bitwise Operators ( &, |, ^, ~, <<, >> ). Bitwise operators modify the variables considering the bits that represent the value they store, that means, their binary representation.

op asm Description

& AND Logical AND

| OR Logical OR

^ XOR Logical exclusive OR

~ NOT Complement to one (bit inversion)

<< SHL Shift Left

>> SHR Shift Right Explicit type casting operators

Type casting operators allows you to convert a datum of a given type to another. There are some ways to do this in C++, the most popular one, compatible with the C language is to precede the expression to be converted by the new type enclosed between parenthesis ():

int i; float f = 3.14; i = (int) f;

The previous code converts the float number 3.14 to an integer value (3). Here, the type casting operator was (int). Another way to do the same thing in C++ is using the constructor form: preceding the expression to be converted by the type and enclose the expression between parenthesis:

i = int ( f );

Both ways of type casting are valid in C++. And additionally ANSI-C++ added new type casting operators more specific for object oriented programming

sizeof() This operator accepts one parameter, that can be either a variable type or a variable itself and returns the size in bytes of that type or object:

a = sizeof (char);

This will return 1 to a because char is a one byte long type. The value returned by sizeof is a constant, so it is always determined before program execution.

Other operators Later in the tutorial we will see some few more operators, like the ones referring to pointers or the specifics for object-oriented programming. Each one is treated in its respective section.

Priority of operators

Page 26: Object Oriented Programing using C++

Object Oriented Programming using C++

26

When making complex expressions with several operands on it we may have some doubts about which operand is evaluated first and which later. For example, in this expression:

a = 5 + 7 % 2

we may doubt if it really means:

a = 5 + (7 % 2) with result 6, or a = (5 + 7) % 2 with result 0

The correct answer is the first of the two expressions, with a result of 6. There is an established order with the priority of each operator, and not only the arithmetic ones (those whose preference we may already know from mathematics) but for all the operators which can appear in C++. From greatest to lowest priority. The priority order is as follows:

Priority Operator Description Associativity

1 :: scope Left

2 () [ ] -> . sizeof Left ++ -- increment/decrement ~ Complement to one (bitwise) ! unary NOT & * Reference and Dereference (pointers) (type) Type casting

3

+ - Unary less sign

Right

4 * / % arithmetical operations Left

5 + - arithmetical operations Left

6 << >> bit shifting (bitwise) Left

7 < <= > >= Relational operators Left

8 == != Relational operators Left

9 & ^ | Bitwise operators Left

10 && || Logic operators Left

11 ?: Conditional Right

12 = += -= *= /= %= >>= <<= &= ^= |= Assignation Right

13 , Comma, Separator Left

Associativity defines -in the case that there are several operators of the same priority level- which one must be evaluated before, the rightmost one or the leftmost one.

All these precedence levels for operators can be manipulated or become more legible using parenthesis sign ( and ) like in this example:

a = 5 + 7 % 2;

might be written as:

a = 5 + (7 % 2); or a = (5 + 7) % 2;

Page 27: Object Oriented Programing using C++

Object Oriented Programming using C++

27

according to the operation that we wanted to perform.

So if you want to write a complicated expression and you are not sure of the precedence levels include always parenthesis. It will probably be also a more legible code.

Communication through console The console is the basic interface of computers, normally it is the set composed by the keyboard and the screen. The keyboard is generally the standard input device and the screen the standard output device.

In the iostream C++ library, standard input and output operations for a program are supported by two data streams: cin for input and cout for output. Additionally, there have also been implemented cerr and clog - these are two output streams specially designed to show error messages. They can be redirected to the standard output or to a log file.

Therefore cout (the standard output stream) is normally directed to the screen and cin (the standard input stream) is normally assigned to the keyboard.

By handling these two streams you will be able to interact with the user in your programs since you will be able to show messages in the screen and to receive his/her input from the keyboard.

Output (cout)

The cout stream is used in conjunction with the overloaded operator << (a pair of "less than" signs).

cout << "Output sentence"; // prints Output sentence on screen cout << 120; // prints number 120 on screen cout << x; // prints the content of variable x on screen

The << operator is known as insertion operator since it inserts the data that follows it into the stream that precedes it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and the variable x into the output stream cout. Notice that the first of the two sentences goes enclosed between double quotes (") because it is a string of characters. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variables. For example, these two sentences are very different:

cout << "Hello"; // prints Hello on screen cout << Hello; // prints the content of Hello variable on screen

The insertion operator (<<) may be used more than once in a same sentence: cout << "Hello, " << "I am " << "a C++ sentence";

this last sentence would print the message Hello, I am a C++ sentence on the screen. The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable:

cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode; If we supose that variable age contains the number 24 and the variable zipcode contains 90064 the output of the previous sentence would be:

Hello, I am 24 years old and my zipcode is 90064

It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, therefore, the following sentences:

cout << "This is a sentence."; cout << "This is another sentence.";

Page 28: Object Oriented Programing using C++

Object Oriented Programming using C++

28

will be shown followed in screen:

This is a sentence.This is another sentence.

even if we have written them in two different calls to cout. So, in order to perform a line break on output we must explicitly order it inserting a new-line character, that in C++ can be written as \n:

cout << "First sentence.\n "; cout << "Second sentence.\nThird sentence.";

produces the following output:

First sentence. Second sentence. Third sentence.

Additionally, to add a new-line, you may also use the endl manipulator. For example:

cout << "First sentence." << endl; cout << "Second sentence." << endl;

would print out:

First sentence. Second sentence.

The endl manipulator has a special behavior when it is used with buffered streams: they are flushed. But anyway cout is unbuffered by default.

You may use either the \n escape character or the endl manipulator in order to specify a line jump to cout. Notice the differences of use shown earlier.

Input (cin).

Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. This must go followed by the variable that will store the data that is going to be read. For example:

int age; cin >> age;

declares the variable age as an int and then waits for an input from cin (keyborad) in order to store it in this integer variable.

cin can only process the input from the keyboard once the RETURN key has been pressed. Thus, even if you request a single character cin will not process the input until the user presses RETURN once the character has been introduced.

You must always consider which is the type of the variable that you are using as container with cin extraction. If you request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters.

// i/o example Please enter an integer value: 702

Page 29: Object Oriented Programing using C++

Object Oriented Programming using C++

29

#include <iostream.h> int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

The value you entered is 702 and its double is 1404.

The user of a program may be one of the reasons that provoke errors in the simplest programs that use cin (like the one we have just seen). Since if you request an integer value and the user introduces its name (which is a string of characters) the result may bring your program to misoperate since it is not what we were expecting from the user. So when you use the data input provided by cin you will have to trust that the user of your program will be totally cooperative and that he will not introduce his name when an interger value is requested. More ahead, when we will see how to use strings of characters we will see possible solutions for the errors that can cause this type of user input.

You can also use cin to request more than one datum input from the user:

cin >> a >> b;

is equivalent to:

cin >> a; cin >> b;

In both cases the user must give two data, one for variable a and another for variable b that may be separated by any valid blank separator: a space, a tab character or a newline.

Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what and how has to perform our program.

With the introduction of control sequences we are going to have to introduce a new concept: the block of instructions. A block of instructions is a group of instructions separated by semicolons (;) but grouped in a block delimited by curly bracket signs: { and }.

Most of the control structures that we will see in this section allow a generic statement as parameter, this refers to either a single instruction or a block of instructions, as we want. If we want the statement to be a single instruction we do not need to enclose it between curly-brackets ({}). If we want the statement to be more than a single instruction we must enclose them between curly brackets ({}) forming a block of instructions.

Conditional structure: if and else

It is used to execute an instruction or block of instructions only if a condition is fulfilled. Its form is:

if (condition) statement

Page 30: Object Oriented Programing using C++

Object Oriented Programming using C++

30

where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional structure.

For example, the following code fragment prints out x is 100 only if the value stored in variable x is indeed 100:

if (x == 100) cout << "x is 100";

If we want more than a single instruction to be executed in case that condition is true we can specify a block of instructions using curly brackets { }:

if (x == 100) { cout << "x is "; cout << x; }

We can additionally specify what we want that happens if the condition is not fulfilled by using the keyword else. Its form used in conjunction with if is:

if (condition) statement1 else statement2

For example:

if (x == 100) cout << "x is 100"; else cout << "x is not 100";

prints out on the screen x is 100 if indeed x is worth 100, but if it is not -and only if not- it prints out x is not 100.

The if + else structures can be concatenated with the intention of verifying a range of values. The following example shows its use telling if the present value stored in x is positive, negative or none of the previous, that is to say, equal to zero.

if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

Remember that in case we want that more than a single instruction is executed we must group them in a block of instructions by using curly brackets { }.

Repetitive structures or loops

Loops have as objective to repeat a statement a certain number of times or while a condition is fulfilled. The while loop.

Its format is:

Page 31: Object Oriented Programing using C++

Object Oriented Programming using C++

31

while (expression) statement

and its function is simply to repeat statement while expression is true.

For example, we are going to make a program to count down using a while loop:

// custom countdown using while #include <iostream.h> int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!"; return 0; }

Enter the starting number > 8 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n be greater than 0 ), the block of instructions that follows will execute indefinite times while the condition (n>0) remains being true.

All the process in the program above can be interpreted according to the following script: beginning in main:

• 1. User assigns a value to n. • 2. The while instruction checks if (n>0). At this point there are two possibilities:

o true: execute statement (step 3,) o false: jump statement. The program follows in step 5..

• 3. Execute statement: cout << n << ", "; --n; (prints out n on screen and decreases n by 1).

• 4. End of block. Return Automatically to step 2. • 5. Continue the program after the block: print out FIRE! and end of program.

We must consider that the loop has to end at some point, therefore, within the block of instructions (loop's statement) we must provide some method that forces condition to become false at some moment, otherwise the loop will continue looping forever. In this case we have included --n; that causes the condition to become false after some loop repetitions: when n becomes 0, that is where our countdown ends.

Of course this is a so simple action for our computer that the whole countdown is performed instantly without practical delay between numbers.

The do-while loop. Format:

do statement while (condition);

Page 32: Object Oriented Programing using C++

Object Oriented Programming using C++

32

Its functionality is exactly the same as the while loop except that condition in the do-while is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following program echoes any number you enter until you enter 0.

// number echoer #include <iostream.h> int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }

Enter number (0 to end): 12345 You entered: 12345 Enter number (0 to end): 160277 You entered: 160277 Enter number (0 to end): 0 You entered: 0

The do-while loop is usually used when the condition that has to determine its end is determined within the loop statement, like in the previous case, where the user input within the block of intructions is what determines the end of the loop. If you never enter the 0 value in the previous example the loop will never end.

The for loop. Its format is:

for (initialization; condition; increase) statement;

and its main function is to repeat statement while condition remains true, like the while loop. But in addition, for provides places to specify an initialization instruction and an increase instruction. So this loop is specially designed to perform a repetitive action with a counter.

It works the following way:

1, initialization is executed. Generally it is a initial value setting for a counter varible. This is executed only once. 2, condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped. 3, statement is executed. As usual, it can be either a single instruction or a block of instructions enclosed within curly brackets { }. 4, finally, whatever is specified in the increase field is executed and the loop gets back to step 2.

Here is an example of countdown using a for loop.

// countdown using a for loop #include <iostream.h> int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!"; return 0; }

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

The initialization and increase fields are optional. They can be avoided but not the semicolon signs among them. Thus, for example we could write: for (;n<10;) if we want to specify no

Page 33: Object Oriented Programing using C++

Object Oriented Programming using C++

33

initialization neither increase; or for (;n<10;n++) if we want to include an increase field but not an initialization.

Optionally, using the comma operator (,) we can specify more than one instruction in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) is an instruction separator, it serves to separate more than one instruction where only one instruction is generally expected. For example, suppose that we wanted to intialize more than one variable in our loop:

for ( n=0, i=100 ; n!=i ; n++, i-- ) { // whatever here... }

This loop will execute 50 times if neither n nor i are modified within the loop:

n starts with 0 and i with 100, the condition is (n!=i) (that n be not equal to i). Beacuse n is increased by one and i decreased, the loop's condition will become false after the 50th loop, when both n and i will be equal to 50.

Bifurcation of control and jumps.

The break instruction. Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count down before it naturally finishes (an engine failure maybe):

// break loop example #include <iostream.h> int main () { int n; for (n=10; n>0; n--) { cout << n << ", "; if (n==3) { cout << "countdown aborted!"; break; } } return 0; }

10, 9, 8, 7, 6, 5, 4, countdown aborted!

The continue instruction. The continue instruction causes the program to skip the rest of the loop in the present iteration as if the end of the statement block would have been reached, causing it to jump to the following iteration. For example, we are going to skip the number 5 in our countdown:

Page 34: Object Oriented Programing using C++

Object Oriented Programming using C++

34

// break loop example #include <iostream.h> int main () { for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; } cout << "FIRE!"; return 0; }

10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!

The goto instruction. It allows to make an absolute jump to another point in the program. You should use this feature carefully since its execution ignores any type of nesting limitation.

The destination point is identified by a label, which is then used as argument for the goto instruction. A label is made of a valid identifier followed by a colon (:)

This instruction does not have a concrete utility in structured or object oriented programming aside from those that low-level programming fans may find to it. For example, here is our countdown loop using goto:

// goto loop example #include <iostream.h> int main () { int n=10; loop: cout << n << ", "; n--; if (n>0) goto loop; cout << "FIRE!"; return 0; }

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

The exit function. exit is a function defined in cstdlib (stdlib.h) library.

The purpose of exit is to terminate the running program with an specific exit code. Its prototype is:

void exit (int exit code);

The exit code is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means an error happened.

The selective Structure: switch.

The syntax of the switch instruction is a bit peculiar. Its objective is to check several possible constant values for an expression, something similar to what we did at the beginning of this section with the linking of several if and else if sentences. Its form is the following:

switch (expression) { case constant1: block of instructions 1 break; case constant2:

Page 35: Object Oriented Programing using C++

Object Oriented Programming using C++

35

block of instructions 2 break; . . . default: default block of instructions }

It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes block of instructions 1 until it finds the break keyword, moment at which the program will jump to the end of the switch selective structure. If expression was not equal to constant1 it will check if expression is equivalent to constant2. If it is, it will execute block of instructions 2 until it finds the break keyword. Finally, if the value of expression has not matched any of the previously specified constants (you may specify as many case sentences as values you want to check), the program will execute the instructions included in the default: section, if this one exists, since it is optional.

Both following code fragments are equivalent:

switch example if-else equivalent

switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; }

if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; } else { cout << "value of x unknown"; }

I have commented before that the syntax of the switch instruction is a bit peculiar. Notice the inclusion of the break instructions at the end of each block. This is necessary because if for example we did not include it after block of instructions 1 the program would not jump to the end of the switch selective block (}) and it would follow executing the rest of blocks of instructions until the first appearing of the break instruction or the end of the switch selective block. This makes unnecessary to include curly brackets { } in each of the cases, and can also be useful to execute one same block of instructions for different possible values for the expression evaluated, for example:

switch (x) { case 1: case 2: case 3: cout << "x is 1, 2 or 3"; break; default: cout << "x is not 1, 2 nor 3"; }

Notice that switch can only be used to compare an expression with different constants. Thus we cannot put variables (case (n*2):) or ranges (case (1..3):) because they are not valid constants.

If you need to check ranges or values that are not constants use a concatenation of if and else if sentences.

Page 36: Object Oriented Programing using C++

Object Oriented Programming using C++

36

Functions (I). Using functions we can structure our programs in a more modular way, accessing to all the potential that structured programming in C++ can offer to us.

A function is a block of instructions that is executed when it is called from some other point of the program. The following is its format:

type name ( argument1, argument2, ...) statement

where: · type is the type of data returned by the function. · name is the name by which it will be possible to call the function. · arguments (as many as wanted can be specified). Each argument consists of a type of data followed by its identifier, like in a variable declaration (for example, int x) and which acts within the function like any other variable. They allow to pass parameters to the function when it is called. The different parameters are separated by commas. · statement is the function's body. It can be a single instruction or a block of instructions. In the latter case it must be delimited by curly brackets {}.

Here you have the first function example:

// function example #include <iostream.h> int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; }

The result is 8

In order to examine this code, first of all remember something said at the beginning of this tutorial: a C++ program always begins its execution by the main function. So we will begin by there.

We can see how the main function begins by declaring the variable z of type int. Right after that we see a call to addition function. If we pay attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function itself some code lines above:

Page 37: Object Oriented Programing using C++

Object Oriented Programming using C++

37

The parameters have a clear correspondence. Within the main function we called to addition passing two values: 5 and 3 that correspond to the int a and int b parameters declared for the function addition.

At the moment at which the function is called from main, the control is lost by main function and passed to function addition. The value of both parameters passed in the call (5 and 3) are copied to the local variables int a and int b within the function.

Function addition declares a new variable (int r;), and by means of the expression r=a+b;, it assigns to r the result of a plus b. Because the passed parameters for a and b are 5 and 3 respectively the result is 8.

The following line of code:

return (r);

finalizes function addition, and returns the control back to the function that has called it (main) following the program by the same point in which it was interrupted by the call to addition. But additionally, return was called with the content of variable r (return (r);), that at that moment was 8, so this value is said to be returned by the function.

The value returned by a function is the value given to the function when it is evaluated. Therefore, z will store the value returned by addition (5, 3), that is 8. To explain it somehow you can imagine that the call to a function (addition (5,3)) is literally replaced by the value it returns (8).

The following line of code in main is:

cout << "The result is " << z;

that, as you may already suppose, produces the printing of the result on the screen.

Page 38: Object Oriented Programing using C++

Object Oriented Programming using C++

38

Scope of variables [re]

You must consider that the scope of variables declared within a function or any other block of instructions is only the own function or the own block of instructions and cannot be used outside of them. For example, in the previous example it had been impossible to use the variables a, b or r directly in function main since they were local variables to the function addition. Also, it had been impossible to use the variable z directly within

the function addition, since this was a local variable to the function main.

Therefore, the scope of local variables is limited to the same nesting level in which they are declared. Nevertheless you can also declare global variables that are visible from any point of the code, inside and outside any function. In order to declare global variables you must do it outside any function or block of instructions, that means, directly in the body of the program.

And here is another example about functions:

// function example #include <iostream.h> int subtraction (int a, int b) { int r; r=a-b; return (r); } int main () { int x=5, y=3, z; z = subtraction (7,2); cout << "The first result is " << z << '\n'; cout << "The second result is " << subtraction (7,2) << '\n'; cout << "The third result is " << subtraction (x,y) << '\n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << '\n'; return 0; }

The first result is 5 The second result is 5 The third result is 2 The fourth result is 6

In this case we have created the function subtraction. The only thing that this function does is to subtract both passed parameters and to return the result.

Nevertheless, if we examine the function main we will see that we have made several calls to function subtraction. We have used some different calling methods so that you see other ways or moments when a function can be called.

Page 39: Object Oriented Programing using C++

Object Oriented Programming using C++

39

In order to understand well these examples you must consider once again that a call to a function could perfectly be replaced by its return value. For example the first case (that you should already know beacause it is the same pattern that we have used in previous examples):

z = subtraction (7,2); cout << "The first result is " << z;

If we replace the function call by its result (that is 5), we would have:

z = 5; cout << "The first result is " << z;

As well as

cout << "The second result is " << subtraction (7,2);

has the same result that the previous call, but in this case we made the call to subtraction directly as a parameter for cout. Simply imagine that we had written:

cout << "The second result is " << 5;

since 5 is the result of subtraction (7,2).

In the case of

cout << "The third result is " << subtraction (x,y);

The only new thing that we introduce is that the parameters of subtraction are variables instead of constants. That is perfectly valid. In this case the values passed to the function subtraction are the values of x and y, that are 5 and 3 respectively, giving 2 as result.

The fourth case is more of the same. Simply to comment that instead of:

z = 4 + subtraction (x,y);

we could perfectly have put:

z = subtraction (x,y) + 4;

with exactly the same result. Notice that the semicolon sign (;) goes at the end of the whole expression. It does not necessarily have to go right after the function call. The explanation might be once again that you imagine that a function can be replaced by its result:

z = 4 + 2; z = 2 + 4;

Functions with no types. The use of void.

If you remember the syntax of a function declaration:

type name ( argument1, argument2 ...) statement

Page 40: Object Oriented Programing using C++

Object Oriented Programming using C++

40

you will see that it is obligatory that this declaration begins with a type, that is the type of the data that will be returned by the function with the return instruction. But what if we want to return no value?

Imagine that we want to make a function just to show a message on the screen. We do not need that it returns any value, moreover, we either need to receive no parameters. For that was devised the void type in C language. Take a look at:

// void function example #include <iostream.h> void dummyfunction (void) { cout << "I'm a function!"; } int main () { dummyfunction (); return 0; }

I'm a function!

Although in C++ it is not necessary to specify void its use is considered suitable to stand out that it is a function without parameters or arguments and not something else.

What you must always be aware of is that the format for calling a function includes to specify its name and to enclose between parenthesis the arguments. The non-existence of arguments do not exempts us from the obligation to put parenthesis, for that reason the call to dummyfunction is

dummyfunction ();

This stands out that it is a call to a function and not the name of a variable nor anything else.

Functions (II). Arguments passed by value and by reference.

Until now, in all the functions we have seen, the parameters passed to the functions have been passed by value. This means that when calling to a function with parameters, what we have passed to the function were values but never the specified variables themselves. For example, suppose that we called to our first function addition using the following code :

int x=5, y=3, z; z = addition ( x , y );

What we did in this case was to call function addition passing the values of x and y, that means 5 and 3 respectively, not the variables themselves.

Page 41: Object Oriented Programing using C++

Object Oriented Programming using C++

41

This way, when function addition is being called the value of its variables a and b become 5 and 3 respectively, but any modification of a or b within the function addition will not affect the values of x and y outside it, because variables x and y were not passed themselves to the the function, only their values.

But there might be some cases where you need to manipulate from inside a function the value of an external variable. For that purpose we have to use arguments passed by reference, as in the function duplicate of the following example:

// passing parameters by reference #include <iostream.h> void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } int main () { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; }

x=2, y=6, z=14

The first thing that should call your attention is that in the declaration of duplicate the type of each argument went followed by an ampersand sign (&), that indeed serves to specify that the variable has to be passed by reference instead of by value, as usual.

When passing a variable by reference we are passing the variable itself and any modification that we do to that parameter within the function will have effect in the passed variable outside it.

To express it somehow, we have associated a, b and c with the parameters used when calling the function (x, y and z) and any change that we do on a within the function will affect the value of x outside. Any change that we do on b will affect y, and the same with c and z. That is why our program's output, that shows the values stored in x, y and z after the call to duplicate, shown the values of the three variables of main duplicated.

If when declaring the following function:

void duplicate (int& a, int& b, int& c)

we had declared it thus:

void duplicate (int a, int b, int c)

Page 42: Object Oriented Programing using C++

Object Oriented Programming using C++

42

that is, without the ampersand (&) signs, we would have not passed the variables by reference, but their values, and therefore, the output on screen for our program would have been the values of x, y and z without having been modified.

This type of declaration "by reference" using the ampersand (&) sign is exclusive of C++. In C language we had to use pointers to do something equivalent.

Passing by reference is an effective way to allow a function to return more than one single value. For example, here is a function that returns the previous and next numbers of the first parameter passed.

// more than one returning value #include <iostream.h> void prevnext (int x, int& prev, int& next) { prev = x-1; next = x+1; } int main () { int x=100, y, z; prevnext (x, y, z); cout << "Previous=" << y << ", Next=" << z; return 0; }

Previous=99, Next=101

Default values in arguments.

When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the function. To do that we simply have to assign a value to the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is stepped on and the passed value is used. For example:

// default values in functions #include <iostream.h> int divide (int a, int b=2) { int r; r=a/b; return (r); } int main () { cout << divide (12); cout << endl; cout << divide (20,4); return 0; }

6 5

As we can see in the body of the program there are two calls to the function divide. In the first one:

divide (12)

Page 43: Object Oriented Programing using C++

Object Oriented Programming using C++

43

we have only specified one argument, but the function divide allows up to two. So the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter lacks (notice the function declaration, which finishes by int b=2). Therefore the result of this function call is 6 (12/2).

In the second call:

divide (20,4)

there are two parameters, so the default assignation (int b=2) is being stepped on by the passed parameter, that is 4. Being the result equal to 5 (20/4).

Overloaded functions.

Two different functions can have the same name if the prototype of their arguments are different, that means that you can put the same name to more than one function if they have either a different number of arguments or different types in their arguments. For example,

// overloaded function #include <iostream.h> int divide (int a, int b) { return (a/b); } float divide (float a, float b) { return (a/b); } int main () { int x=5,y=2; float n=5.0,m=2.0; cout << divide (x,y); cout << "\n"; cout << divide (n,m); return 0; }

2 2.5

In this case we have defined two functions with the same name, but one of them accepts two arguments of type int and the other accepts them of type float. The compiler knows which to call in each case examining the types when the function is called, if it is called with two ints as arguments it calls to the function that has two int arguments in the prototype and if it is called with two floats it will call to the one which has two floats in its prototype.

For simplicity I have included the same code within both functions, but this is not compulsory. You can make two function with the same name but completely different behaviors.

inline functions.

The inline directive can be included before a function declaration to specify that the function must be compiled as code in the same point where it is called. This is equivalent to declare a macro, and its advantage is only appreciated in very short functions, in which the resulting code from compiling the program may be faster if the overhead of calling a function (stacking of arguments) is avoided.

Page 44: Object Oriented Programing using C++

Object Oriented Programming using C++

44

The format for its declaration is: inline type name ( arguments ... ) { instructions ... } and the call is just like the call to any other function. It is not necessary to include the inline keyword before each call, only in the declaration.

Recursivity.

Recursivity is the property that functions have to be called by themselves. It is useful for some tasks like for example some sorting methods or to calculate the factorial of a number. For example, to obtain the factorial of a number (n) its mathematical formula is:

n! = n * (n-1) * (n-2) * (n-3) ... * 1

more concretely, 5! (factorial of 5) would be:

5! = 5 * 4 * 3 * 2 * 1 = 120

and a recursive function to do that could be this:

// factorial calculator #include <iostream.h> long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else return (1); } int main () { long l; cout << "Type a number: "; cin >> l; cout << "!" << l << " = " << factorial (l); return 0; }

Type a number: 9 !9 = 362880

Notice how in function factorial we included a call to itself, but only if the argument is greater than 1, since otherwise the function would perform an infinite recursive loop in which once it arrived to 0 it would continue multiplying by all the negative numbers (probably provoking a stack overflow error on runtime).

This function has a limitation because of the data type used in its design (long) for more simplicity. In a standard system, the type long would not allow to store factorials greater than 12!.

Prototyping functions.

Until now, we have defined the functions before the first appearance of a call to it, that generally was in main, leaving the function main for the end. If you try to repeat some of the examples of functions described until now but placing the function main before any other function that is called from within it, you will most likely obtain an error. The reason is that to be able to call to a function this one must have been declared previously (it must be known), like we have done in all our examples.

Page 45: Object Oriented Programing using C++

Object Oriented Programming using C++

45

But there is an alternative way to avoid to write all the code of all functions before they can be used in main or in another function. It is by prototyping functions. This consists in making a previous shorter declaration of the complete definition but quite significant so that the compiler can know the arguments and the return type needed.

Its form is:

type name ( argument_type1, argument_type2, ...);

It is identical to the header of a function definition, except:

• It does not include a statement for the function. That means that it does not include the body with all the instructions that are usually enclose within curly brackets { }.

• It ends with a semicolon sign (;). • In the argument enumeration it is enough to put the type of each argument. The inclusion

of a name for each argument as in the definition of a standard function is optional, although recommendable.

For example:

// prototyping #include <iostream.h> void odd (int a); void even (int a); int main () { int i; do { cout << "Type a number: (0 to exit)"; cin >> i; odd (i); } while (i!=0); return 0; } void odd (int a) { if ((a%2)!=0) cout << "Number is odd.\n"; else even (a); } void even (int a) { if ((a%2)==0) cout << "Number is even.\n"; else odd (a); }

Type a number (0 to exit): 9 Number is odd. Type a number (0 to exit): 6 Number is even. Type a number (0 to exit): 1030 Number is even. Type a number (0 to exit): 0 Number is even.

This example is indeed not an example of effectiveness, I am sure that at this point you can already make a program with the same result using only the half of code lines. But this example ilustrates how protyping works. Moreover, in this concrete case the prototyping of -at least- one of the two functions is necessary.

The first things that we see are the prototypes of functions odd and even:

void odd (int a); void even (int a);

Page 46: Object Oriented Programing using C++

Object Oriented Programming using C++

46

that allows these functions to be used before they are completely defined, for example, in main, which now is located in a more logical place: the beginning of the program's code.

Nevertheless, the specific reason why this program needs that at least one of the functions to be prototyped, is because in odd there is a call to even and in even there is a call to odd, reason why if none of the two functions had been previously declared, an error would have happened, since either odd would not be visible from even (because it has not still been declared), or even would not be visible from odd.

Many programmers recommend that all functions be prototyped. It is also my recommendation, mainly in case that there are many functions or in the case that these are very long, since, having the prototype of all the functions in the same place can spare us some time when consulting how to call it or even ease the creation of a header file.

Arrays Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name.

That means that, for example, we can store 5 values of type int without having to declare 5 different variables each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with an unique identifier.

For example, an array to contain 5 integer values of type int called billy could be represented this way:

where each blank panel represents an element of the array, that in this case are integer values of type int. These are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length .

Like any other variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];

where type is a valid object type (int, float...), name is a valid variable identifier and the elements field, that goes enclosed within brackets [], specifies how many of these elements does contain the array.

Thus, to declare billy as shown above it would be something as simple as the following sentence:

int billy [5];

NOTE: The elements field within brackets [] when declaring an array must be a constant value, since arrays are blocks of static memory of a given size and the compiler must be able to determine exactly how much memory must assign to the array before any instruction is considered.

Page 47: Object Oriented Programing using C++

Object Oriented Programming using C++

47

Initializing arrays.

When declaring an array of local scope (within a function), if we do not specify otherwise, it will not be initialized, so its content is undetermined until we store some values in it.

If we declare a global array (outside any function) its content will be initialized with all its elements filled with zeros. Thus, if in the global scope we declare:

int billy [5];

every element of billy will be set initialy to 0:

But additionally, when we declare an Array, we have the possibility to assign initial values to each one of its elements using curly brackets { }. For example:

int billy [5] = { 16, 2, 77, 40, 12071 };

this declaration would have created an array like the following one:

The number of elements in the array that we initialized within curly brackets { } must match the length in elements that we declared for the array enclosed within square brackets [ ]. For example, in the example of the billy array we have declared that it had 5 elements and in the list of initial values within curly brackets { } we have set 5 different values, one for each element.

Because this can be considered an useless repetition, C++ includes the possibility of leaving empty the brackets [ ], being the size of the Array defined by the number of values included between curly brackets { }:

int billy [] = { 16, 2, 77, 40, 12071 };

Access to the values of an Array.

In any point of the program in which the array is visible we can access individually anyone of its values for reading or modifying it as if it was a normal variable. The format is the following:

name[index]

Following the previous examples in which billy had 5 elements and each of those elements was of type int, the name which we can use to refer to each element is the following one:

For example, to store the value 75 in the third element of billy a suitable sentence would be:

Page 48: Object Oriented Programing using C++

Object Oriented Programming using C++

48

billy[2] = 75;

and, for example, to pass the value of the third element of billy to the variable a, we could write:

a = billy[2];

Therefore, for all the effects, the expression billy[2] is like any variable of type int. With the same properties.

Notice that the third element of billy is specified billy[2], since first is billy[0], second billy[1], and therefore, third is billy[2]. By this same reason, its last element is billy[4]. Since if we wrote billy[5], we would be acceding to the sixth element of billy and therefore exceeding the size of the array.

In C++ it is perfectly valid to exceed the valid range of indices for an Array, which can cause certain difficultly detectable problems, since they do not cause compilation errors but they can cause unexpected results or serious errors during execution. The reason why this is allowed will be seen ahead when we begin to use pointers.

At this point it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays. They perform two differt tasks: one, is to set the size of arrays when declaring them; and second, to specify indices for a concrete array element when referring to it. We must simply take care of not confusing these two possible uses of brackets [ ] with arrays:

int billy[5]; // declaration of a new Array (begins with a type name) billy[2] = 75; // access to an element of the Array.

Other valid operations with arrays:

billy[0] = a; billy[a] = 75; b = billy [a+2]; billy[billy[a]] = billy[2] + 5;

// arrays example #include <iostream.h> int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; }

12206

Multidimensional Arrays

Multidimensional arrays can be described as arrays of arrays. For example, a bidimensional array can be imagined as a bidimensional table of a uniform concrete data type.

Page 49: Object Oriented Programing using C++

Object Oriented Programming using C++

49

jimmy represents a bidimensional array of 3 per 5 values of type int. The way to declare this array would be:

int jimmy [3][5];

and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:

jimmy[1][3]

(remember that array indices always begin by 0).

Multidimensional arrays are not limited to two indices (two dimensions). They can contain so many indices as needed, although it is rare to have to represent more than 3 dimensions. Just consider the amount of memory that an array with many indices may need. For example:

char century [100][365][24][60][60];

assigns a char for each second contained in a century, that is more than 3 billion chars! What would consume about 3000 megabytes of RAM memory if we could declare it.

Multidimensional arrays are nothing else than an abstraction, since we can simply obtain the same results with a simple array by putting a factor between its indices:

int jimmy [3][5]; is equivalent to int jimmy [15]; (3 * 5 = 15)

with the only difference that the compiler remembers for us the depth of each imaginary dimension. Serve as example these two pieces of code, with exactly the same result, one using bidimensional arrays and the other using only simple arrays:

// multidimensional array #include <iostream.h> #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n,m; int main () {

// pseudo-multidimensional array #include <iostream.h> #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT * WIDTH]; int n,m; int main () {

Page 50: Object Oriented Programing using C++

Object Oriented Programming using C++

50

for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n][m]=(n+1)*(m+1); } return 0; }

for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n * WIDTH + m]=(n+1)*(m+1); } return 0; }

none of the programs above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:

We have used defined constants (#define) to simplify possible future modifications of the program, for example, in case that we decided to enlarge the array to a height of 4 instead of 3 it would be enough by changing the line:

#define HEIGHT 3

by

#define HEIGHT 4

with no need to make any other modifications to the program.

Arrays as parameters

At some moment we may need to pass an array to a function as a parameter. In C++ is not possible to pass by value a complete block of memory as a parameter, even if it is ordered as an array, to a function, but it is allowed to pass its address, which has almost the same practical effect and is a much faster and more efficient operation.

In order to admit arrays as parameters the only thing that we must do when declaring the function is to specify in the argument the base type for the array that it contains, an identifier and a pair of void brackets []. For example, the following function:

void procedure (int arg[])

admits a parameter of type "Array of int" called arg. In order to pass to this function an array declared as:

int myarray [40];

it would be enough with a call like this:

procedure (myarray);

Here you have a complete example:

Page 51: Object Oriented Programing using C++

Object Oriented Programming using C++

51

// arrays as parameters #include <iostream.h> void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "\n"; } int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray,3); printarray (secondarray,5); return 0; }

5 10 15 2 4 6 8 10

As you can see, the first argument (int arg[]) admits any array of type int, wathever its length is, for that reason we have included a second parameter that says to the function the length of each array that we pass to it as the first parameter so that the for loop that prints out the array can know the range to check in the passed array.

In a function declaration is also possible to include multidimensional arrays. Its format for a tridimensional one is:

base_type[][depth][depth]

for example, a function with a multidimensional array as argument could be:

void procedure (int myarray[][3][4])

notice that the first brackets [] are void and the following ones not. This must always be thus because the compiler must be able to determine within the function which is the depth of each additional dimension.

Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for less experienced programmers. I recommend the reading of chapter 3.3, Pointers for a better understanding of how arrays operate.

Strings of Characters In all programs seen until now, we have used only numerical variables, used to express numbers exclusively. But in addition to numerical variables there also exist strings of characters, that allow us to represent successions of characters, like words, sentences, names, texts, et cetera. Until now we have only used them as constants, but we have never considered variables able to contain them.

In C++ there is no specific elemental variable type to store strings of characters. In order to fulfill this feature we can use arrays of type char, which are successions of char elements. Remember that this data type (char) is the one used to store a single character, for that reason arrays of them are generally used to make strings of single characters.

For example, the following array (or string of characters):

Page 52: Object Oriented Programing using C++

Object Oriented Programming using C++

52

char jenny [20];

can store a string up to 20 characters long. You may imagine it thus:

This maximum size of 20 characters is not required to be always fully used. For example, jenny could store at some moment in a program either the string of characters "Hello" or the string "Merry christmas". Therefore, since the array of characters can store shorter strings than its total length, there has been reached a convention to end the valid content of a string with a null character, whose constant can be written 0 or '\0'.

We could represent jenny (an array of 20 elements of type char) storing the strings of characters "Hello" and "Merry Christmas" in the following way:

Notice how after the valid content it is included a null character ('\0') in order to indicate the end of string. The panels in gray color represent indeterminate values.

Initialization of strings

Because strings of characters are ordinary arrays they fulfill all teir same rules. For example, if we want to initialize a string of characters with predetermined values we can do it in a similar way to any other array:

char mystring[] = { 'H', 'e', 'l', 'l', 'o', '\0' };

In this case we would have declared a string of characters (array) of 6 elements of type char initialized with the characters that compose Hello plus a null character '\0'.

Nevertheless, string of characters have an additional way to initialize its values: using constant strings.

In the expressions we have used in examples of previous chapters there have already appeared several times constants that represented entire strings of characters. These are specified enclosed between double quotes ("), for example:

"the result is: "

is a constant string that we have propably used in some occasion.

Unlike single quotes (') which allow to specify single character constants, double quotes (") are constants that specify a succession of characters. These strings enclosed between double quotes have always a null character ('\0') automatically appended at the end.

Page 53: Object Oriented Programing using C++

Object Oriented Programming using C++

53

Therefore we could initialize the string mystring with values by any of these two ways:

char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char mystring [] = "Hello";

In both cases the Array or string of characters mystring is declared with a size of 6 characters (elements of type char): the 5 characters that compose Hello plus a final null character ('\0') which specifies the end of the string and that, in the second case, when using double quotes (") it is automatically appended.

Before going further, I notice to you that the assignation of multiple constants like double-quoted constants (") to arrays are only valid when initializing the array, that is, at the moment when declared. Expressions within the code like:

mystring = "Hello"; mystring[] = "Hello";

are not valid for arrays, like neither would be:

mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };

So remember: We can "assign" a multiple constant to an Array only at the moment of initializing it. The reason will be more comprehensible when you know a bit more about pointers, since then it will be clarified that an array is simply a constant pointer pointing to an allocated block of memory. And because of this constantnes, the array itself can not be assigned any value, but we can assing values to each of the elements of the array.

At the moment of initializing an Array it is a special case, since it is not an assignation, although the same equal sign (=) is used. Anyway, have always present the rule previously underlined.

Assigning values to strings

Therefore, since the lvalue of an assignation can only be an element of an array and not the entire array, what would be valid is to assign a string of characters to an array of char using a method like this:

mystring[0] = 'H'; mystring[1] = 'e'; mystring[2] = 'l'; mystring[3] = 'l'; mystring[4] = 'o'; mystring[5] = '\0';

But as you may think, this does not seem to be a very practical method. Generally for assigning values to an array, and more specifically to a string of characters, a series of functions like strcpy are used. strcpy (string copy) is defined in the cstring (string.h) library and can be called the following way:

strcpy (string1, string2);

Page 54: Object Oriented Programing using C++

Object Oriented Programming using C++

54

This does copy the content of string2 into string1. string2 can be either an array, a pointer, or a constant string, so the following line would be a valid way to assign the constant string "Hello" to mystring:

strcpy (mystring, "Hello");

For example:

// setting value to string #include <iostream.h> #include <string.h> int main () { char szMyName [20]; strcpy (szMyName,"J. Soulie"); cout << szMyName; return 0; }

J. Soulie

Look how we have needed to include <string.h> header in order to be able to use function strcpy.

Although we can always write a simple function like the following setstring with the same operating than cstring's strcpy:

// setting value to string #include <iostream.h> void setstring (char szOut [], char szIn []) { int n=0; do { szOut[n] = szIn[n]; } while (szIn[n++] != '\0'); } int main () { char szMyName [20]; setstring (szMyName,"J. Soulie"); cout << szMyName; return 0; }

J. Soulie

Another frequently used method to assign values to an array is by using directly the input stream (cin). In this case the value of the string is assigned by the user during program execution.

When cin is used with strings of characters it is usually used with its getline method, that can be called following this prototype:

cin.getline ( char buffer[], int length, char delimiter = ' \n');

where buffer is the address where to store the input (like an array, for example), length is the maximum length of the buffer (the size of the array) and delimiter is the character used to determine the end of the user input, which by default - if we do not include that parameter - will be the newline character ('\n').

Page 55: Object Oriented Programing using C++

Object Oriented Programming using C++

55

The following example repeats whatever you type on your keyboard. It is quite simple but serves as example on how you can use cin.getline with strings:

// cin with strings #include <iostream.h> int main () { char mybuffer [100]; cout << "What's your name? "; cin.getline (mybuffer,100); cout << "Hello " << mybuffer << ".\n"; cout << "Which is your favourite team? "; cin.getline (mybuffer,100); cout << "I like " << mybuffer << " too.\n"; return 0; }

What's your name? Juan Hello Juan. Which is your favourite team? Inter Milan I like Inter Milan too.

Notice how in both calls to cin.getline we used the same string identifier (mybuffer). What the program does in the second call is simply step on the previous content of buffer by the new one that is introduced.

If you remember the section about communication through console, you will remember that we used the extraction operator (>>) to receive data directly from the standard input. This method can also be used instead of cin.getline with strings of characters. For example, in our program, when we requested an input from the user we could have written:

cin >> mybuffer;

this would work, but this method has the following limitations that cin.getline has not:

• It can only receive single words (no complete sentences) since this method uses as delimiter any occurrence of a blank character, including spaces, tabulators, newlines and carriage returns.

• It is not allowed to specify a size for the buffer. What makes your program unstable in case that the user input is longer than the array that will host it.

For these reasons it is recommendable that whenever you require strings of characters coming from cin you use cin.getline instead of cin >>.

Converting strings to other types

Due to that a string may contain representations of other data types like numbers it might be useful to translate that content to a variable of a numeric type. For example, a string may contain "1977", but this is a sequence of 5 chars not so easily convertable to a single integer data type. The cstdlib (stdlib.h) library provides three useful functions for this purpose:

• atoi: converts string to int type. • atol: converts string to long type. • atof: converts string to float type.

All of these functions admit one parameter and return a value of the requested type (int, long or float). These functions combined with getline method of cin are a more reliable way to get the user input when requesting a number than the classic cin>> method:

Page 56: Object Oriented Programing using C++

Object Oriented Programming using C++

56

// cin and ato* functions #include <iostream.h> #include <stdlib.h> int main () { char mybuffer [100]; float price; int quantity; cout << "Enter price: "; cin.getline (mybuffer,100); price = atof (mybuffer); cout << "Enter quantity: "; cin.getline (mybuffer,100); quantity = atoi (mybuffer); cout << "Total price: " << price*quantity; return 0; }

Enter price: 2.75 Enter quantity: 21 Total price: 57.75

Functions to manipulate strings

The cstring library (string.h) defines many functions to perform some manipulation operations with C-like strings (like already explained strcpy). Here you have a brief with the most usual: strcat: char* strcat (char* dest, const char* src);

Appends src string at the end of dest string. Returns dest. strcmp: int strcmp (const char* string1, const char* string2);

Compares strings string1 and string2. Returns 0 is both strings are equal. strcpy: char* strcpy (char* dest, const char* src);

Copies the content of src to dest. Returns dest. strlen: size_t strlen (const char* string);

Returns the length of string.

NOTE: char* is the same as char[]

Pointers We have already seen how variables are memory cells that we can access by an identifier. But these variables are stored in concrete places of the computer memory. For our programs, the computer memory is only a succession of 1 byte cells (the minimum size for a datum), each one with a unique address.

A good simile for the computer memory can be a street in a city. On a street all houses are consecutively numbered with an unique identifier so if we talk about 27th of Sesame Street we will be able to find that place without loss, since there must be only one house with that number and, in addition, we know that that house will be between houses 26 and 28.

In the same way in which houses in a street are numbered, the operating system organizes the memory with unique and consecutive numbers, so if we talk about location 1776 in the memory, we know that there is only one location with that address and also that is between addresses 1775 and 1777.

Address (dereference) operator (&).

At the moment in which we declare a variable this one must be stored in a concrete location in this succession of cells (the memory). We generally do not decide where the variable is to be

Page 57: Object Oriented Programing using C++

Object Oriented Programming using C++

57

placed - fortunately that is something automatically done by the compiler and the operating system on runtime, but once the operating system has assigned an address there are some cases in which we may be interested in knowing where the variable is stored.

This can be done by preceding the variable identifier by an ampersand sign (&), which literally means "address of". For example:

ted = &andy;

would assign to variable ted the address of variable andy, since when preceding the name of the variable andy with the ampersand (&) character we are no longer talking about the content of the variable, but about its address in memory.

We are going to suppose that andy has been placed in the memory address 1776 and that we write the following:

andy = 25; fred = andy; ted = &andy;

the result will be the one shown in the following diagram:

We have assigned to fred the content of variable andy as we have done in many other occasions in previous sections of this tutorial, but to ted we have assigned the address in memory where the operating system stores the value of andy, that we have imagined that it was 1776 (it can be any address, I have just invented this one). The reason is that in the allocation of ted we have preceded andy with an ampersand (&) character.

The variable that stores the address of another variable (like ted in the previous example) is what we call a pointer. In C++ pointers have certain virtues and they are used very often. More ahead we will see how this type of variables are declared.

Reference operator (*)

Using a pointer we can access directly to the value stored in the variable pointed by it just by preceding the pointer identifier with the reference operator asterisk (*), that can be literally translated to "value pointed by". Thus, following with the values of the previous example, if we write:

beth = *ted;

(that we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25.

Page 58: Object Oriented Programing using C++

Object Oriented Programming using C++

58

You must clearly differenciate that ted stores 1776, but *ted (with an asterisk * before) refers to the value stored in the address 1776, that is 25. Notice the difference to include or not the reference asterisk (I have included an explanatory commentary of how each expression could be read):

beth = ted; // beth equal to ted ( 1776 ) beth = *ted; // beth equal to value pointed by ted ( 25 )

Operator of address or dereference (&) It is used as a variable prefix and can be translated as "address of", thus: &variable1 can be read as "address of variable1"

Operator of reference (*) It indicates that what has to be evaluated is the content pointed by the expression considered as an address. It can be translated by "value pointed by". * mypointer can be read as "value pointed by mypointer".

At this point, and following with the same example initiated above where:

andy = 25; ted = &andy;

you should be able to clearly see that all the following expressions are true: andy == 25 &andy == 1776 ted == 1776 *ted == 25

The first expression is quite clear considering that its assignation was andy=25;. The second one uses the address (or derefence) operator (&) that returns the address of the variable andy, that we imagined to be 1776. The third one is quite obvious since the second was true and the assignation of ted was ted = &andy;. The fourth expression uses the reference operator (*) that, as we have just seen, is equivalent to the value contained in the address pointed by ted, that is 25.

So, after all that, you may also infer that while the address pointed by ted remains unchanged the following expression will also be true:

*ted == andy

Declaring variables of type pointer

Due to the ability of pointers to reference directly to the value where they point to it becomes necessary to specify which data type a pointer points to when declaring it, since it is not the same to point to a char than to an int or a float type.

Page 59: Object Oriented Programing using C++

Object Oriented Programming using C++

59

Thus, the declaration of pointers follow this form:

type * pointer_name;

where type is the type of data pointed, not the type of the pointer itself. For example:

int * number; char * character; float * greatnumber;

they are three declarations of pointers. Each one of them points to a different data type, but the three are pointers and in fact the three occupy the same amount of space in memory (the size of a pointer depends on the operating system), but the data to which they point do not occupy the same nor are of the same type, one is int, another one is char and the other one float.

I emphasize that this asterisk (*) that we put when declaring a pointer means only that: that it is a pointer, and does not have to be confused with the reference operator that we have seen a bit earlier and that is also written with an asterisk (*). They are simply two different tasks represented with the same sign.

// my first pointer #include <iostream.h> int main () { int value1 = 5, value2 = 15; int * mypointer; mypointer = &value1; *mypointer = 10; mypointer = &value2; *mypointer = 20; cout << "value1==" << value1 << "/ value2==" << value2; return 0; }

value1==10 / value2==20

Notice how the values of value1 and value2 have changed indirectly. First we have assigned to mypointer the address of value1 using the deference ampersand sign (&) and then we have assigned 10 to the value pointed by mypointer, that it is pointing to the address of value1, so we have modified value1 indirectly.

In order that you can see that a pointer may take several different values during the same program we have repeated the process with value2 and the same pointer.

Here is an example a bit more complicated:

// more pointers #include <iostream.h> int main () { int value1 = 5, value2 = 15; int *p1, *p2; p1 = &value1; // p1 = address of value1 p2 = &value2; // p2 = address of value2 *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1

value1==10 / value2==20

Page 60: Object Oriented Programing using C++

Object Oriented Programming using C++

60

p1 = p2; // p1 = p2 (pointer assignation) *p1 = 20; // value pointed by p1 = 20 cout << "value1==" << value1 << "/ value2==" << value2; return 0; }

I have included as comments on each line how the code can be read: ampersand (&) as "address of" and asterisk (*) as "value pointed by". Notice that there are expressions with pointers p1 and p2 with and without asterisk. The meaning of putting or not a reference asterisk is very different: An asterisk (*) followed by the pointer refers to the place pointed by the pointer, whereas a pointer without an asterisk (*) refers to the value of the pointer itself, that is, the address where it is pointing to.

Another thing that can call your attention is the line:

int *p1, *p2;

that declares the two pointers of the previous example putting an asterisk (*) for each pointer. The reason is that the type for all the declarations of the same line is int (and not int*). The explanation is because of the level of precedence of the reference operator asterisk (*) that is the same one that the declaration of types, therefore, because they are associative operators from the right, the asterisk are evaluated first than the type. We have talked about this although it is enough that you know clearly that -unless you include parenthesis- you will have to put an asterisk (*) before each pointer you declare.

Pointers and arrays

The concept of array goes very bound to the one of pointer. In fact, the identifier of an array is equivalent to the address of its first element, like a pointer is equivalent to the address of the first element that it points to, so in fact they are the same thing. For example, supposing these two declarations:

int numbers [20]; int * p;

the following allocation would be valid:

p = numbers;

At this point p and numbers are equivalent and they have the same properties, with the only difference that we could assign another value to the pointer p whereas numbers will always point to the first of the 20 integer numbers of type int with which it was defined. So, unlike p, that is an ordinary variable pointer, numbers is a constant pointer (indeed that is an Array: a constant pointer). Therefore, although the previous expression was valid, the following allocation is not:

numbers = p;

because numbers is an array (constant pointer), and no values can be assigned to constant identifiers.

Due to its character of variables all the expressions that include pointers in the following example are perfectly valid:

Page 61: Object Oriented Programing using C++

Object Oriented Programming using C++

61

// more pointers #include <iostream.h> int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; }

10, 20, 30, 40, 50,

In chapter "Arrays" we used several times barcket signs [] in order to specify the index of the element from the Array that we wanted to refer. Well, the bracket signs operator [] are known as offset operators and they are equivalent to add the number within brackets to the address of a pointer. For example, both following expressions:

a[5] = 0; // a [offset of 5] = 0 *(a+5) = 0; // pointed by (a+5) = 0

are equivalent and valid either if a is a pointer or if it is an array.

Pointer initialization

When declaring pointers we may want to explicitly specify to which variable we want they to point to,

int number; int *tommy = &number;

this is equivalent to:

int number; int *tommy; tommy = &number;

When a pointer assignation takes place we are always assigning the address where it points to, never the value pointed. You must consider that at the moment of declaring a pointer, the asterisk (*) indicates only that it is a pointer, in no case indicates a reference operator asterisk (*). Remember, they are two different operators, although they are written with the same sign. Thus, we must take care of not confusing the previous with:

int number; int *tommy; *tommy = &number;

that anyway would not have much sense in this case.

Like in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment of declaring the variable pointer:

char * terry = "hello";

Page 62: Object Oriented Programing using C++

Object Oriented Programming using C++

62

in this case static storage is reserved for containing "hello" and a pointer to the first char of this memory block (that corresponds to 'h') is assigned to terry. If we imagine that "hello" is stored at addresses 1702 and following, the previous declaration could be outlined thus:

it is important to indicate that terry contains value 1702 and not 'h' nor "hello", although 1702 points to these last ones.

The pointer terry points to a string of characters and can be used exactly as if it was an Array (remember that an array is just a constant pointer). For example, if our temper changed and we wanted to replace the 'o' by a '!' sign in the content pointed by terry, we could do it by any of the following two ways:

terry[4] = '!'; *(terry+4) = '!';

remember that to write terry[4] is just the same as to write *(terry+4), although the most usual expression is the first one. With any of those two expressions would happen something like this:

Arithmetic of pointers

To conduct arithmetical operations on pointers is a little different than to conduct them on other integer data types. To begin, only addition and subtraction operations are allowed to be conducted, the others make no sense in the world of pointers. But both addition and subtraction have a different behavior with pointers according to the size of the data type to which they point to.

When we saw the different data types that exist, we saw that some occupy more or less space than others in the memory. For example, in the case of integer numbers, char occupies 1 byte, short occupies 2 bytes and long occupies 4.

Let's suppose that we have 3 pointers:

char *mychar; short *myshort; long *mylong;

and that we know that they point to memory locations 1000, 2000 and 3000 respectively.

Page 63: Object Oriented Programing using C++

Object Oriented Programming using C++

63

So if we write:

mychar++; myshort++; mylong++;

mychar, as you may expect, would contain the value 1001. Nevertheless, myshort would contain the value 2002, and mylong would contain 3004. The reason is that when adding 1 to a pointer we are making it to point to the following element of the same type with which it has been defined, and therefore the size in bytes of the type pointed is added to the pointer.

This is applicable both when adding and subtracting any number to a pointer. It would happen exactly the same if we write:

mychar = mychar + 1; myshort = myshort + 1; mylong = mylong + 1;

It may result important to warn you that both increase (++) and decrease (--) operators have a greater priority than the reference operator asterisk (*), therefore the following expressions may lead to confussion:

*p++; *p++ = *q++;

The first one is equivalent to *(p++) and what it does is to increase p (the address where it points to - not the value that contains). The second, because both increase operators (++) are after the expressions to be evaluated and not before, first the value of *q is assigned to *p and then they are both q and p increased by one. It is equivalent to:

*p = *q; p++; q++;

Like always, I recommend you the use of parenthesis () in order to avoid unexpected results.

Pointers to pointers

Page 64: Object Oriented Programing using C++

Object Oriented Programming using C++

64

C++ allows the use of pointers that point to pointers, that these, on its turn, point to data. In order to do that we only need to add an asterisk (*) for each level of reference:

char a; char * b; char ** c; a = 'z'; b = &a; c = &b;

this, supposing the randomly chosen memory locations of 7230, 8092 and 10502, could be described thus:

(inside the cells there is the content of the variable; under the cells its location)

The new thing in this example is variable c, which we can talk about in three different ways, each one of them would correspond to a different value:

c is a variable of type (char **) with a value of 8092 *c is a variable of type (char*) with a value of 7230 **c is a variable of type (char) with a value of'z'

void pointers

The type of pointer void is a special type of pointer. void pointers can point to any data type, from an integer value or a float to a string of characters. Its sole limitation is that the pointed data cannot be referenced directly (we can not use reference asterisk * operator on them), since its length is always undetermined, and for that reason we will always have to resort to type casting or assignations to turn our void pointer to a pointer of a concrete data type that we can refer.

One of its utilities may be for passing generic parameters to a function:

// integer increaser #include <iostream.h> void increase (void* data, int type) { switch (type) { case sizeof(char) : (*((char*)data))++; break; case sizeof(short): (*((short*)data))++; break; case sizeof(long) : (*((long*)data))++; break; } } int main () { char a = 5; short b = 9; long c = 12; increase (&a,sizeof(a)); increase (&b,sizeof(b));

6, 10, 13

Page 65: Object Oriented Programing using C++

Object Oriented Programming using C++

65

increase (&c,sizeof(c)); cout << (int) a << ", " << b << ", " << c; return 0; }

sizeof is an operator integrated in the C++ language that returns a constant value with the size in bytes of its parameter, so, for example, sizeof(char) is 1, because char type is 1 byte long.

Pointers to functions

C++ allows to operate with pointers to functions. The greater utility of that is for passing a function as a parameter to another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we must declare it like the prototype of the function but enclosing between parenthesis () the name of the function and inserting a pointer asterisk (*) before. It might not be a very handsome syntax, but it is how it is done in C++:

// pointer to functions #include <iostream.h> int addition (int a, int b) { return (a+b); } int subtraction (int a, int b) { return (a-b); } int (*minus)(int,int) = subtraction; int operation (int x, int y, int (*functocall)(int,int)) { int g; g = (*functocall)(x,y); return (g); } int main () { int m,n; m = operation (7, 5, &addition); n = operation (20, m, minus); cout <<n; return 0; }

8

In the example, minus is a global pointer to a function that has two parameters of type int, this one is immediately assigned to point to the function subtraction, all in a single line:

int (* minus)(int,int) = subtraction;

Dynamic memory Until now, in our programs, we have only had as much memory as we have requested in declarations of variables, arrays and other objects that we included, having the size of all of them to be fixed before the execution of the program. But, What if we need a variable amount of memory that can only be determined during the program execution (runtime)?, for example, in case that we need an user input to determine the necessary amount of space.

The answer is dynamic memory, for which C++ integrates the operators new and delete.

Page 66: Object Oriented Programing using C++

Object Oriented Programming using C++

66

Operators new and delete are exclusive of C++. More ahead in this same section are shown the C equivalents for these operators.

Operators new and new[ ] In order to request dynamic memory it exists the operator new. new goes followed by a data type and optionally the number of elements required within brackets []. It returns a pointer to the beginning of the new block of assigned memory. Its form is:

pointer = new type

or

pointer = new type [elements]

The first expression is used to assign memory to contain one single element of type. The second one is used to assign a block (an array) of elements of type. For example:

int * bobby; bobby = new int [5];

in this case, the operating system has assigned space for 5 elements of type int in the heap and it has returned a pointer to its beginning that has been assigned to bobby. Therefore, now, bobby points to a valid block of memory with space for 5 int elements.

You could ask what is the difference between declaring a normal array and assigning memory to a pointer as we have just done. The most important one is that the size of an array must be a constant value, which limits its size to what we decide at the moment of designing the program before its execution, whereas the dynamic memory allocation allows to assign memory during the execution of the program using any variable, constant or combination of both as size.

The dynamic memory is generally managed by the operating system, and in the multitask interfaces can be shared between several applications, so there is a possibility that the memory exhausts. If this happens and the operating system cannot assign the memory that we request with the operator new, a null pointer will be returned. For that reason is recommendable to always verify if after a call to instruction new the returned pointer is null:

int * bobby; bobby = new int [5]; if (bobby == NULL) { // error assigning memory. Take measures. };

Operator delete. Since the necessity of dynamic memory is usually limited to concrete moments within a program, once this one is no longer needed it shall be freed so that it become available for future requests of dynamic memory. For this exists the operator delete, whose form is:

Page 67: Object Oriented Programing using C++

Object Oriented Programming using C++

67

delete pointer;

or

delete [] pointer;

The first expression should be used to delete memory alloccated for a single element, and the second one for memory allocated for multiple elements (arrays). In most compilers both expressions are equivalent and can be used without distinction, although indeed they are two different operators and so must be considered for operator overloading

// rememb-o-matic #include <iostream.h> #include <stdlib.h> int main () { char input [100]; int i,n; long * l, total = 0; cout << "How many numbers do you want to type in? "; cin.getline (input,100); i=atoi (input); l= new long[i]; if (l == NULL) exit (1); for (n=0; n<i; n++) { cout << "Enter number: "; cin.getline (input,100); l[n]=atol (input); } cout << "You have entered: "; for (n=0; n<i; n++) cout << l[n] << ", "; delete[] l; return 0; }

How many numbers do you want to type in? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32,

This simple example that memorizes numbers does not have a limited amount of numbers that can be introduced, thanks to that we request to the system as much space as it is necessary to store all the numbers that the user wishes to introduce.

NULL is a constant value defined in manyfold C++ libraries specially designed to indicate null pointers. In case that this constant is not defined you can do it yourself by defining it to 0:

#define NULL 0

It is indifferent to put 0 or NULL when checking pointers, but the use of NULL with pointers is widely extended and it is recommended for greater legibility. The reason is that a pointer is barely compared or set directly to a numerical literal constant except precisely number 0, and this way this action is symbolically masked.

Dynamic memory in ANSI-C

Operators new and delete are exclusive of C++ and they are not available in C language. In C language, in order to assign dynamic memory we have to resort to library stdlib.h. We are going to see them, since they are also valid in C++ and they are used in some existing programs.

The function malloc It is the generic function to assign dynamic memory to pointers. Its prototype is:

Page 68: Object Oriented Programing using C++

Object Oriented Programming using C++

68

void * malloc (size_t nbytes);

where nbytes is the number of bytes that we want to be assigned to the pointer. The function returns a pointer of type void*, reason why we have to type cast the value to the type of the destination pointer, for example:

char * ronny; ronny = (char *) malloc (10);

This assigns to ronny a pointer to an usable block of 10 bytes. When we want to assign a block of data of a different type other than char (different from 1 byte) we must multiply the number of elements desired by the size of each element. Luckyly we have at our disposition the operator sizeof, that returns the size of a data type of a concrete datum.

int * bobby; bobby = (int *) malloc (5 * sizeof(int));

This piece of code assigns to bobby a pointer to a block of 5 integers of type int, this size can be equal to 2, 4 or more bytes according to the system where the program is compiled.

The function calloc. calloc is very similar to malloc in its operation, its main difference is in its prototype:

void * calloc (size_t nelements, size_t size);

since it admits 2 parameters instead of one. These two parameters are multiplied to obtain the total size of the memory block to be assigned. Usually the first parameter (nelements) is the number of elements and the second one (size) serves to specify the size of each element. For example, we could define bobby with calloc thus:

int * bobby; bobby = (int *) calloc (5, sizeof(int));

Another difference between malloc and calloc is that calloc initializates all its elements to 0.

The function realloc. It changes the size of a block of memory already assigned to a pointer.

void * realloc (void * pointer, size_t size);

pointer parameter receives a pointer to an already assigned memory block or a null pointer, and size specifies the new size that the memory block shall have. The function assigns size bytes of memory to the pointer. The function may need to change the location of the memory block for that the new size can fit, in that case the present content of the block is copied to the new one to guarantee that the existing data is not lost. The new pointer is returned by the function. If it has not been posible to assign the memory block with the new size it returns a null pointer but the pointer specified as parameter and its content remains unchanged.

The function free. It releases a block of dynamic memory previously assigned using malloc, calloc or realloc.

void free (void * pointer);

This function must only be used to release memory assigned with functions malloc, calloc and realloc

Page 69: Object Oriented Programing using C++

Object Oriented Programming using C++

69

Structures Data structures.

A data structure is a set of diverse types of data that may have different lengths grouped together under a unique declaration. Its form is the following:

struct model_name { type1 element1; type2 element2; type3 element3; . . } object_name;

where model_name is a name for the model of the structure type and the optional parameter object_name is a valid identifier (or identifiers) for structure object instantiations. Within curly brackets { } they are the types and their sub-identifiers corresponding to the elements that compose the structure.

If the structure definition includes the parameter model_name (optional), that parameter becomes a valid type name equivalent to the structure. For example:

struct products { char name [30]; float price; } ; products apple; products orange, melon;

We have first defined the structure model products with two fields: name and price, each of a different type. We have then used the name of the structure type (products) to declare three objects of that type: apple, orange and melon.

Once declared, products has become a new valid type name like the fundamental ones int, char or short. And since then we have been able to declare objects (variables) of that type.

The optional field object_name that can go at the end of the structure declaration serves to directly declare objects of the structure type. For example, to declare the structure objects apple, orange and melon like we have done previously we could also have done it this way:

struct products { char name [30]; float price; } apple, orange, melon;

Moreover, in cases like the last one in which we took advantage of the declaration of the structure model to declare objects of it, the parameter model_name (in this case products) becomes optional. Although if model_name is not included it will not be possible to declare more objects of this same model later.

It is important to clearly differentiate between what is a structure model, and what a structure object. Using the terms we used with variables, the model is the type, and the object is the variable. We can instantiate many objects (variables) from a single model (type).

Once we have declared our three objects of a determined structure model (apple, orange and melon) we can operate with the fields that form them. To do that we have to use a point (.) inserted between the object name and the field name. For example, we could operate with anyone of these elements as if they were standard variables of their respective types:

Page 70: Object Oriented Programing using C++

Object Oriented Programming using C++

70

apple.name apple.price orange.name orange.price melon.name melon.price

each one being of its corresponding data type: apple.name, orange.name and melon.name are of type char[30], and apple.price, orange.price and melon.price are of type float.

We are going to leave apples, oranges and melons and go with an example about movies:

// example about structures #include <iostream.h> #include <string.h> #include <stdlib.h> struct movies_t { char title [50]; int year; } mine, yours; void printmovie (movies_t movie); int main () { char buffer [50]; strcpy (mine.title, "2001 A Space Odyssey"); mine.year = 1968; cout << "Enter title: "; cin.getline (yours.title,50); cout << "Enter year: "; cin.getline (buffer,50); yours.year = atoi (buffer); cout << "My favourite movie is:\n "; printmovie (mine); cout << "And yours:\n "; printmovie (yours); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }

Enter title: Alien Enter year: 1979 My favourite movie is: 2001 A Space Odyssey (1968) And yours: Alien (1979)

The example shows how can we use the elements of a structure and the structure itself as normal variables. For example, yours.year is a valid variable of type int, as well as mine.title is a valid array of 50 chars.

Notice that either mine and yours are also treated as valid variables of type movie_t when being passed to the function printmovie(). Therefore, one of the most important advantages of structures is that we can refer either to their elements individually or to all the structure as a block.

Structures are a feature used very often to build data bases, specially if we consider the possibility of building arrays of them.

Page 71: Object Oriented Programing using C++

Object Oriented Programming using C++

71

// array of structures #include <iostream.h> #include <stdlib.h> #define N_MOVIES 5 struct movies_t { char title [50]; int year; } films [N_MOVIES]; void printmovie (movies_t movie); int main () { char buffer [50]; int n; for (n=0; n<N_MOVIES; n++) { cout << "Enter title: "; cin.getline (films[n].title,50); cout << "Enter year: "; cin.getline (buffer,50); films[n].year = atoi (buffer); } cout << "\nYou have entered these movies:\n"; for (n=0; n<N_MOVIES; n++) printmovie (films[n]); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }

Enter title: Alien Enter year: 1979 Enter title: Blade Runner Enter year: 1982 Enter title: Matrix Enter year: 1999 Enter title: Rear Window Enter year: 1954 Enter title: Taxi Driver Enter year: 1975 You have entered these movies: Alien (1979) Blade Runner (1982) Matrix (1999) Rear Window (1954) Taxi Driver (1975)

Pointers to structures

Like any other type, structures can be pointed by pointers. The rules are the same as for any fundamental data type: The pointer must be declared as a pointer to the structure:

struct movies_t { char title [50]; int year; }; movies_t amovie; movies_t * pmovie;

Here amovie is an object of struct type movies_t and pmovie is a pointer to point to objects of struct type movies_t. So, the following, as with fundamental types, would also be valid:

pmovie = &amovie;

Ok, we will now go with another example, that will serve us to introduce a new operator:

// pointers to structures #include <iostream.h> #include <stdlib.h> struct movies_t { char title [50]; int year; };

Enter title: Matrix Enter year: 1999 You have entered: Matrix (1999)

Page 72: Object Oriented Programing using C++

Object Oriented Programming using C++

72

int main () { char buffer[50]; movies_t amovie; movies_t * pmovie; pmovie = & amovie; cout << "Enter title: "; cin.getline (pmovie->title,50); cout << "Enter year: "; cin.getline (buffer,50); pmovie->year = atoi (buffer); cout << "\nYou have entered:\n"; cout << pmovie->title; cout << " (" << pmovie->year << ")\n"; return 0; }

The previous code includes a new important introduction: operator ->. This is a reference operator that is used exclusively with pointers to structures and pointers to classes. It allows us not to have to use parenthesis on each reference to a structure member. In the example we used:

movies->title

that could be translated to:

(*movies).title

both expressions movies->title and (*movies).title are valid and mean that we are evaluating the element title of the structure pointed by movies. You must distinguish it clearly from:

*movies.title

that is equivalent to

*(movies.title)

and that would serve to evaluate the value pointed by element title of structure movies, that in this case (where title is not a pointer) it would not make much sense. The following panel summarizes possible combinations of pointers and structures:

Expression Description Equivalent

movies.title Element title of structure movies

movies->title Element title of structure pointed by movies (*movies).title

*movies.title Value pointed by element title of structure movies *(movies.title)

Nesting structures

Structures can also be nested so that a valid element of a structure can also be another structure.

Page 73: Object Oriented Programing using C++

Object Oriented Programming using C++

73

struct movies_t { char title [50]; int year; } struct friends_t { char name [50]; char email [50]; movies_t favourite_movie; } charlie, maria; friends_t * pfriends = &charlie;

Thus, after the previous declaration we could use the following expressions:

charlie.name maria.favourite_movie.title charlie.favourite_movie.year pfriends->favourite_movie.year

(where, by the way, the last two expressions are equivalent).

The concept of structures that has been discussed in this section is the same used in C language, nevertheless, in C++, the structure concept has been extended up to the same functionality of a class with the peculiarity that all of its elements are considered public.

User defined data types We have already seen a sort of data type that is defined by the user (programmer): the structures. But moreover than these there are other sorts of user defined data types:

Definition of own types (typedef).

C++ allows us to define our own types based on other existing data types. In order to do that we shall use keyword typedef, whose form is:

typedef existing_type new_type_name ;

where existing_type is a C++ fundamental or any other defined type and new_type_name is the name that the new type we are going to define will receive. For example:

typedef char C; typedef unsigned int WORD; typedef char * string_t; typedef char field [50];

In this case we have defined four new data types: C, WORD, string_t and field as char, unsigned int, char* and char[50] respectively, that we could perfectly use later as valid types:

C achar, anotherchar, *ptchar1; WORD myword; string_t ptchar2; field name;

typedef can be useful to define a type that is repeatedly used within a program and it is possible that we need to change it in a later version, or if a type you want to use has a too long name and you want it to be shorter.

Page 74: Object Oriented Programing using C++

Object Oriented Programming using C++

74

Unions

Unions allows a same portion of memory to be accessed as different data types, being in fact all of them the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different:

union model_name { type1 element1; type2 element2; type3 element3; . . } object_name;

All the elements of the union declaration occupy the same space of memory. Its size is the one of the greatest element of the declaration. For example:

union mytypes_t { char c; int i; float f; } mytypes;

defines three elements:

mytypes.c mytypes.i mytypes.f

each one of a different data type. Like all of them are referring to a same location in memory the modification of one of the elements will afect to the value of all of them.

One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. For example,

union mix_t{ long l; struct { short hi; short lo; } s; char c[4]; } mix;

defines three names that allow us to access the same group of 4 bytes: mix.l, mix.s and mix.c and which we can use according to how we want to access it, as long, short or char respectively. I have mixed types, arrays and structures in the union so that you can see the different ways how we can access to the data:

Anonymous unions

In C++ we have the option that unions be anonymous. If we include a union in a structure without any object name (the one that goes after the curly brackets { }) the union will be anonymous and

Page 75: Object Oriented Programing using C++

Object Oriented Programming using C++

75

we will be able to access to the elements directly by its name. For example, look at the difference between these two declarations:

union anonymous union

struct { char title[50]; char author[50]; union { float dollars; int yens; } price; } book;

struct { char title[50]; char author[50]; union { float dollars; int yens; }; } book;

The only difference between the two pieces of code is that in the first one we gave a name to the union (price) and in the second not. The difference is when accessing to members dollars and yens of an object. In the first case it would be:

book.price.dollars book.price.yens

whereas in the second it would be:

book.dollars book.yens

Once again I remind you that because it is a union, the fields dollars and yens occupy the same space in the memory so they cannot be used to store two different values, that means that you can include a price in dollars or yens but not both.

Enumerations (enum)

Enumerations serve to create data types to contain something different that is not limited neither to numerical or character constants nor constants true and false. Its form is the following:

enum model_name { value1, value2, value3, . . } object_name;

For example, we could create a new type of variable called color to store colors with the following declaration:

enum colors_t {black, blue, green, cyan, red, purple, yellow, white};

Notice that we do not include any fundamental data type in the declaration. To say it somehow we have created a new data type without being based on any other existing one: the type color_t, whose possible values are the colors that we have enclosed within curly brackets {}. For example, once declared the colors_t enumeration the following expressions will be valid:

colors_t mycolor; mycolor = blue; if (mycolor == green) mycolor = red;

Page 76: Object Oriented Programing using C++

Object Oriented Programming using C++

76

In fact our enumerated data type is compiled as an integer and their possible values are any type of integer constant specified. If this one is not specified, the integer value equivalent to the first possible value is 0 and the following ones follow a +1 progression. Thus, in our data type colors_t that we defined before, white would be equivalent to 0, blue would be equivalent to 1, green to 2 and so on.

If we explicitly specify an integer value for some of the possible values of our enumerated type (for example the first one) the following values will be the increases of this, for example:

enum months_t { january=1, february, march, april, may, june, july, august, september, october, november, december} y2k;

in this case, variable y2k of the enumerated type months_t can contain any of the 12 possible values that go from january to december and that are equivalent to values between 1 and 12, not between 0 and 11 since we have made january to be equal to 1.

Classes A class is a logical method to organize data and functions in a same structure. They are declared using keyword class, whose functionality is similar to the one of the C keyword struct, but with the possibility of including functions as members, moreover than only data.

Its form is:

class class_name { permission_label_1: member1; permission_label_2: member2; ... } object_name;

where class_name is a name for the class (user defined type) and the optional field object_name is one, or several, valid object identifiers. The body of the declaration can contain members, that can be either data or function declarations, and optionally permission labels, that can be any of these three keywords: private:, public: or protected:. They make reference to the permission which the following members acquire:

• private members of a class are accessible only from other members of its same class or from its "friend" classes.

• protected members are accessible, in addition to from members of the same class and friend classes, also from members of its derived classes.

• Finally, public members are accessible from anywhere where the class is visible.

If we declare members of a class before including any permission label the members they are considered private, since it is the default permission that the members of a class declared with the class keyword acquire.

For example:

class CRectangle { int x, y; public: void set_values (int,int);

Page 77: Object Oriented Programing using C++

Object Oriented Programming using C++

77

int area (void); } rect;

Declares class CRectangle and an object called rect of this class (type). This class contains four members: two variables of type int (x and y) in the private section (because private is the default permission) and two functions in the public section: set_values() and area(), of which we have only included the prototype.

Notice the difference between class name and object name: In the previous example, CRectangle was the class name (i.e., the user-defined type), whereas rect was an object of type CRectangle. Is the same difference that int and a have in the following declaration:

int a;

int is the class name (type) and a is the object name (variable).

On successive instructions in the body of the program we can refer to any of the public members of the object rect as if they were normal functions or variables, just by putting the object's name followed by a point and then the class member (like we did with C structs). For example:

rect.set_value (3,4); myarea = rect.area();

but we will not be able to refer to x nor y since they are private members of the class and they could only be referred from other members of that same class. Confused? Here is the complete example of class CRectangle:

// classes example #include <iostream.h> class CRectangle { int x, y; public: void set_values (int,int); int area (void) {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); }

area: 12

The new thing in this code is the operator :: of scope included in the definition of set_values(). It is used to declare a member of a class outside it. Notice that we have defined the behavior of function area() within the definition of the CRectangle class - given its extreme simplicity. Whereas set_values() has only its protype declared within the class but its definition is outside. In this outside declaration we must use the operator of scope ::.

The scope operator (::) allows to specify the class to which the member being declared belongs to, granting exactly the same scope properties as if it was directly defined within the class. For example, in the function set_values() of the previous code, we have resorted to the variables x and y, that are members of class CRectangle and that are only visible inside it and its members (since they are private).

Page 78: Object Oriented Programing using C++

Object Oriented Programming using C++

78

The only difference between defining a class member function completely within its class and to include only the prototype, is that in the first case the function will automatically be considered inline by the compiler, while in the second it will be a normal (not-inline) class member function.

The reason why we have made x and y private members (remember that if nothing else is said all members of a class defined with keyword class have private access) it is because we have already defined a function to introduce those values in the object (set_values()) and therefore the rest of the program does not have why to access directly to them. Perhaps in a so simple example as this one you do not see a great utility protecting those two variables, but in greater projects it may be very important that values cannot be modified in an unexpected way (unexpected way, from the point of view of the object).

One of the greater advantages of classes is that we can declare several different objects from it. For example, following with the previous example of class CRectangle, we could have declared the object rectb in addition to the object rect :

// class example #include <iostream.h> class CRectangle { int x, y; public: void set_values (int,int); int area (void) {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; }

rect area: 12 rectb area: 30

Notice that does not give the same result the call to rect.area() than the call to rectb.area(). To explain it somehow, each object of class CRectangle has its own variables x and y, and its own functions set_value() and area().

On that is based the concept of object and object-oriented programming. In that data and functions are properties of the object, instead of the usual view of objects as function parameters in structured programming. In this and the following sections we will discuss advantages of this methodology.

In this concrete case, the class (type of object) to which we were talking about is CRectangle, of which there are two instances, or objects: rect and rectb, each one with its own member variables and member functions.

Constructors and destructors Objects generally need to initialize variables or to assign dynamic memory during their process of creation to become totally operative and to avoid returning unexpected values during their

Page 79: Object Oriented Programing using C++

Object Oriented Programming using C++

79

execution. For example, what would happen if in the previous example we called the function area() before having called function set_values? Probably an indetermined result since the members x and y would have never been assigned a value.

In order to avoid that, a class can include a special function: a constructor, which can be declared by naming a member function with the same name as the class. This constructor function will be called automatically when a new instance of the class is created (when declaring a new object or allocating an object of that class) and only then. We are going to implement CRectangle including a constructor:

// classes example #include <iostream.h> class CRectangle { int width, height; public: CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; }

rect area: 12 rectb area: 30

As you can see, the result of this example is identical to the previous one. In this case we have only replaced the function set_values, that no longer exists, by a class constructor. Notice the way in which the parameters are passed to the constructor at the moment at which the instances of the class are created:

CRectangle rect (3,4); CRectangle rectb (5,6);

You can also see how neither the prototype nor the later constructor declaration include a return value, even no void type, this must always be thus. A constructor never returns a value nor the void has to be specified. Right how we have not done in the previous example.

The Destructor fulfills the opposite functionality. This is automatically called when an object is released from the memory, either because its scope of existence has finished (for example, if it was defined as a local object within a function and the function ends) or because it is an object dynamically assigned and is released using operator delete.

The destructor must have the same name as the class with a tilde (~) as prefix and it must return no value.

The use of destructors is specially suitable when an object assigns dynamic memory during its life and at the moment of being destroyed we want to release the memory that it has used.

// example on constructors and destructors rect area: 12

Page 80: Object Oriented Programing using C++

Object Oriented Programming using C++

80

#include <iostream.h> class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area (void) {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int; height = new int; *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }

rectb area: 30

Overloading Constructors Like any other function, a constructor can also be overloaded with several functions that have the same name but different type or number of parameters. Remember that the compiler will execute the one that matches at the moment at which a function with that name is called. In this case, at the moment at which a class object is declared.

In fact, in the cases where we declare a class and we do not specify any constructor the compiler automatically assumes two overloaded constructors ("default constructor" and "copy constructor"). For example, for the class:

class CExample { public: int a,b,c; void multiply (int n, int m) { a=n; b=m; c=a*b; }; };

with no constructors, the compiler automatically assumes that it has the following constructor member functions:

• Empty constructor It is a constructor with no parameters defined as nop (empty block of instructions). It does nothing.

CExample::CExample () { };

• Copy constructor It is a constructor with only one parameter of its same type that assigns to every nonstatic class member variable of the object a copy of the passed object.

• CExample::CExample (const CExample& rv) { • a=rv.a; b=rv.b; c=rv.c;

Page 81: Object Oriented Programing using C++

Object Oriented Programming using C++

81

• }

It is important to indicate that both default constructors: the empty construction and the copy constructor exist only if no other constructor is explicitly declared. In case that any constructor with any number of parameters is declared none of these two default constructors will exist, so if you want them to be there, you shall define your own ones.

Of course, you can also overload the class constructor providing different constructors for when you pass parameters between parenthesis and when you do not (empty):

// overloading class constructors #include <iostream.h> class CRectangle { int width, height; public: CRectangle (); CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle () { width = 5; height = 5; } CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb; cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; }

rect area: 12 rectb area: 25

In this case rectb was declared without parameters, so it has been initialized with the constructor that has no parameters, which declares both width and height with a value of 5.

Notice that if we declare a new object and we do not want to pass parameters to it we do not have to include parentheses ():

CRectangle rectb; // right CRectangle rectb(); // wrong!

Pointers to classes

It is perfectly valid to create pointers pointing to classes, in order to do that we must simply consider that once declared, the class becomes a valid type, so use the class name as the type for the pointer. For example:

CRectangle * prect;

is a pointer to an object of class CRectangle.

Page 82: Object Oriented Programing using C++

Object Oriented Programming using C++

82

As it happens with data structures, to refer directly to a member of an object pointed by a pointer you should use operator ->. Here is an example with some possible combinations:

// pointer to classes example #include <iostream.h> class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,8); cout << "a area: " << a.area() << endl; cout << "*b area: " << b->area() << endl; cout << "*c area: " << c->area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; return 0; }

a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56

Next you have a summary on how can you read some pointer and class operators (*, &, ., ->, [ ]) that appear in the previous example:

*x can be read: pointed by x &x can be read: address of x x.y can be read: member y of object x (*x).y can be read: member y of object pointed by x x->y can be read: member y of object pointed by x (equivalent to the previous one) x[0] can be read: first object pointed by x x[1] can be read: second object pointed by x x[n] can be read: (n+1)th object pointed by x

Be sure you understand the logic of all of these before going on.

Classes defined with keyword struct

C++ language has extended the C keyword struct to the same functionality of the C++ class keyword except that its members are public by default instead of being private.

Anyway, due to that both class and struct have almost the same functionality in C++, struct is usually used for data-only structures and class for classes that have procedures and member functions.

Page 83: Object Oriented Programing using C++

Object Oriented Programming using C++

83

Overloading operators C++ incorporates the option to use language standard operators between classes in addition to between fundamental types. For example:

int a, b, c; a = b + c;

is perfectly valid, since the different variables of the addition are all fundamental types. Nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect):

struct { char product [50]; float price; } a, b, c; a = b + c;

The assignation of a class (or struct) to other one of the same type is allowed (default copy constructor). What would produce an error would be the addition operation, that in principle is not valid between non-fundamental types.

But thanks to the C++ ability to overload operators, we can get to do that objects derived from composed types as the previous one can accept operators which would not be accepted otherwise or even modify the effect of operators which they already admit. Here is a list of all the operators that can be overloaded:

+ - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |= && || %= [] () new delete

To overload an operator we only need to write a class member function whose name is operator followed by the operator sign that we want to overload, following this prototype:

type operator sign (parameters);

Here you have an example that includes the operator +. We are going to sum the bidimensional vectors a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple as to add the two x coordinates to obtain the resulting x coordinate and to add the two y coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3).

// vectors: overloading operators example #include <iostream.h> class CVector { public: int x,y; CVector () {}; CVector (int,int); CVector operator + (CVector); }; CVector::CVector (int a, int b) { x = a; y = b; } CVector CVector::operator+ (CVector param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); }

4,3

Page 84: Object Oriented Programing using C++

Object Oriented Programming using C++

84

int main () { CVector a (3,1); CVector b (1,2); CVector c; c = a + b; cout << c.x << "," << c.y; return 0; }

If you are baffled seeing so many CVector consider that some of them make reference to the class name CVector and others are functions with that name (constructor and destructor). Do not confuse them:

CVector (int, int); // function name CVector (constructor) CVector operator+ (CVector); // function operator+ that returns CVector type

The function operator+ of class CVector is the one that is in charge of overloading the arithmetic operator +. This one can be called by any of these two ways:

c = a + b; c = a.operator+ (b);

Notice also that we have incuded the empty constructor (without parameters) and we have defined it with a no-op block of instructions:

CVector () { };

this is necessary, since there already exists another constructor,

CVector (int, int);

and if so none of the default constructors will exist in CVector if we do not explicitly declare it like we have done. Otherwise the declaration

CVector c;

included in main() would not be valid.

Anyway, I have to warn you that a no-op block is not a recommended implementation for a constructor, since it does not fulfill the minimum functionality that a constructor should have, which is the initialization of all the variables in the class. In our case this constructor leaves variables x and y undefined. Therefore, a more advisable declaration would have been something similar to this:

CVector () { x=0; y=0; };

that for simplicity I have not included in the code.

As well as a class includes by deafult an empty and a copy constructor, it also includes a default definition for the assignation operator (=) between two classes of the same type. This copies the whole content of the non-static data members of the parameter object (the one at the right side of the sign) to the one at the left side. Of course, you can redefine it to any other functionality that you want for this operator, like for example, copy only certain class members.

Page 85: Object Oriented Programing using C++

Object Oriented Programming using C++

85

The overload of operators does not force its operation to bear a relation to the mathematical or usual meaning of the operator, although it is recommended. For example, it is not very logical to use operator + to subtract two classes or operator == to fill with zeros a class, although it is perfectly possible to do so.

Although the prototype of a function operator+ can seem obvious since it takes the right side of the operator as the parameter for the function operator+ of the left side object, other operators are not so clear. Here you have a table with a summary on how the different operator functions must be declared (replace @ by the operator in each case):

Expression Operator (@) Function member Global function @a + - * & ! ~ ++ -- A::operator@() operator@(A) a@ ++ -- A::operator@(int) operator@(A, int)

a@b + - * / % ^ & | < > == != <= >= << >> && || ,

A::operator@(B) operator@(A, B)

a@b = += -= *= /= %= ^= &= |= <<= >>= [ ] A::operator@(B) -

a(b, c...) () A::operator()(B, C...) - a->b -> A::operator->() -

* where a is an object of class A, b is an object of class B and c is an object of class C.

You can see in this panel that there are two ways to overload some class operators: as member function and as global function. Its use is indistinct, nevertheless I remind you that functions that are not members of a class cannot access the private or protected members of the class unless the global function is friend of the class (friend is explained later).

The keyword this

The keyword this represents within a class the address in memory of the object of that class that is being executed. It is a pointer whose value is always the address of the object.

It can be used to check if a parameter passed to a member function of an object is the object itself. For example,

// this #include <iostream.h> class CDummy { public: int isitme (CDummy& param); }; int CDummy::isitme (CDummy& param) { if (&param == this) return 1; else return 0; } int main () {

yes, &a is b

Page 86: Object Oriented Programing using C++

Object Oriented Programming using C++

86

CDummy a; CDummy* b = &a; if ( b->isitme(a) ) cout << "yes, &a is b"; return 0; }

It is also frequenty used in operator= member functions that return objects by reference (avoiding the use of temporary objects). Following with the vector's examples seen before we could have written an operator= function like this:

CVector& CVector::operator= (const CVector& param) { x=param.x; y=param.y; return *this; }

In fact this is a probable default code generated for the class if we include no operator= member function.

Static members

A class can contain static members, either data and functions.

Static data members of a class are also known as "class variables", because their content does not depend on any object. There is only one unique value for all the objects of that same class.

For example, it may be used for that a variable within a class can contain the number of objects of that class declared, like in the following example:

// static members in classes #include <iostream.h> class CDummy { public: static int n; CDummy () { n++; }; ~CDummy () { n--; }; }; int CDummy::n=0; int main () { CDummy a; CDummy b[5]; CDummy * c = new CDummy; cout << a.n << endl; delete c; cout << CDummy::n << endl; return 0; }

7 6

In fact, static members have the same properties as global variables but enjoying class scope. For that reason, and to avoid that they may be declared several times, according to ANSI-C++ standard, we can only include the protype (declaration) in the class declaration but not the definition (initialization). In order to initialize a static data-member we must include a formal definition outside the class, in the global scope, like in the previous example.

Page 87: Object Oriented Programing using C++

Object Oriented Programming using C++

87

Because it is a unique variable for all the objects of the same class, this can be referred as a member of any object of that class or even directly by the class name (of course this is only valid for static members):

cout << a.n; cout << CDummy::n;

These two calls included in the previous example are referring to the same variable: the static variable n within class CDummy.

Once again, I remind you that in fact it is a global variable. The only difference is its name outside the class.

Like we may include static data within a class we can also include static functions. They represent the same: they are global functions that are called as if they were object members of a given class. They can only refer to static data, in no case to nonstatic members of the class, as well as they do not allow the use of keyword this, since it makes reference to an object pointer and these functions in fact are not members of any object but directly members of the class.

Relationships between classes Friend functions (friend keyword)

In the previous section we have seen that there were three levels of internal protection for the different members of a class: public, protected and private. In the case of members protected and private, these could not be accessed from outside the same class at which they are declared. Nevertheless, this rule can be transgressed with the use of friend keyword in a class, by means of which we can allow an external function to gain access to the protected and private members of a class.

In order to allow an external function to have access to the private and protected members of a class we have to declare the prototye of the external function that will gain access preceded by the keyword friend within the class declaration that shares its members. In the following example we declare the friend function duplicate:

// friend functions #include <iostream.h> class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} friend CRectangle duplicate (CRectangle); }; void CRectangle::set_values (int a, int b) { width = a; height = b; }

24

Page 88: Object Oriented Programing using C++

Object Oriented Programming using C++

88

CRectangle duplicate (CRectangle rectparam) { CRectangle rectres; rectres.width = rectparam.width*2; rectres.height = rectparam.height*2; return (rectres); } int main () { CRectangle rect, rectb; rect.set_values (2,3); rectb = duplicate (rect); cout << rectb.area(); }

From within the duplicate function, that is friend of CRectangle, we have been able to access to the members width and height of different objects of type CRectangle. Notice that neither in the declaration of duplicate() nor in its later use in main() we have considered duplicate as a member of class CRectangle. It isn't.

The friend functions can serve, for example, to conduct operations between two different classes. Generally the use of friend functions is out of an object-oriented programming methodology, so whenever it is possible better try to use members of the own class to make the process. Like in the previous example, in which would have been shorter to integrate duplicate() within the class.

Friend classes (friend)

Like we have the possibility to define a friend function, we can also define a class as friend of another one, allowing that the second one accesses to the protected and private members of the first one.

// friend class #include <iostream.h> class CSquare; class CRectangle { int width, height; public: int area (void) {return (width * height);} void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) {side=a;} friend class CRectangle; }; void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4);

16

Page 89: Object Oriented Programing using C++

Object Oriented Programming using C++

89

rect.convert(sqr); cout << rect.area(); return 0; }

In this example we have declared CRectangle as friend of CSquare for that CRectangle can access to the protected and private members of CSquare, more concretely to CSquare::side, that defines the square side width.

You may also see as a new thing the first instruction of the program, that is the empty prototype of class CSquare, this is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous definition for CSquare this class would not be visible from within the definition of CRectangle.

Consider that friendships are not corresponded if we do not explicitly specify it. In our CSquare example CRectangle is considered as a class friend, but CRectangle does not do the proper thing with CSquare, so CRectangle can access to the protected and private members of CSquare but not the reverse way. Although nothing prevents us to declare also CSquare as friend of CRectangle.

Inheritance between classes

When programming in C, it is common to view problem solutions from a top-down approach: functions and actions of the program are defined in terms of sub-functions, which again are defined in sub-sub-functions, etc.. This yields a hierarchy of code: main() at the top, followed by a level of functions which are called from main(), etc..

In C++ the dependencies between code and data can also be defined in terms of classes which are related to other classes. But the relation which is described here is of a different kind: a class can be defined by means of an older, pre-existing, class. This leads to a situation in which a new class has all the functionality of the older class, and additionally introduces its own specific functionality. Instead of composition, where a given class contains another class, we mean here derivation, where a given class is another class.

Another term for derivation is inheritance: the new class inherits the functionality of an existing class, while the existing class does not appear as a data member in the definition of the new class. When speaking of inheritance the existing class is called the base class, while the new class is called the derived class.

Derivation of classes is often used when the methodology of C++ program development is fully exploited. In this chapter we will first address the syntactical possibilities which C++ offers to derive classes from other classes. Then we will address some of the possibilities which are thus offered by C++.

classes are identified during the problem analysis, after which objects of the defined classes can be declared to represent entities of the problem at hand. The classes are placed in a hierarchy, where the top-level class contains the least functionality. Each new derivation (and hence descent in the class hierarchy) adds new functionality compared to yet existing classes.

In this chapter we shall use a simple vehicle classification system to build a hierarchy of classes. The first class is Vehicle, which implements as its functionality the possibility to set or retrieve the weight of a vehicle. The next level in the object hierarchy are land-, water- and air vehicles.

Page 90: Object Oriented Programing using C++

Object Oriented Programming using C++

90

The initial object hierarchy is illustrated in figure 11.

figure 11: Initial object hierarchy of vehicles.

Related types

The relationship between the proposed classes representing different kinds of vehicles is further illustrated here. The figure shows the object hierarchy in vertical direction: an Auto is a special case of a Land vehicle, which in turn is a special case of a Vehicle.

The class Vehicle is thus the ` greatest common denominator' in the classification system. For the sake of the example we implement in this class the functionality to store and retrieve the weight of a vehicle:

class Vehicle { public: Vehicle(); Vehicle(unsigned wt); unsigned getweight() const; void setweight(unsigned wt); private: unsigned weight; }; Using this class, the weight of a vehicle can be defined as soon as the corresponding object is created. At a later stage the weight can be re-defined or retrieved.

To represent vehicles which travel over land, a new class Land can be defined with the functionality of a Vehicle, while adding its own specific information and functionality. Assume that we are interested in the speed of land vehicles and in their weights. The relationship between Vehicles and Lands could of course be represented with composition, but that would be awkward: composition would suggest that a Land vehicle contains a vehicle, while the relationship should be that the Land vehicle is a special case of a vehicle.

A relationship in terms of composition would also introduce needless code. E.g., consider the following code fragment which shows a class Land using composition (only the setweight() functionality is shown):

Page 91: Object Oriented Programing using C++

Object Oriented Programming using C++

91

class Land { public: void setweight(unsigned wt); private: Vehicle v; // composed Vehicle }; void Land::setweight(unsigned wt) { v.setweight(wt); } Using composition, the setweight() function of the class Land only serves to pass its argument to Vehicle::setweight(). Thus, as far as weight handling is concerned, Land::setweight() introduces no extra functionality, just extra code. Clearly this code duplication is superfluous: a Land should be a Vehicle; it should not contain a Vehicle.

The intended relationship is better achieved using inheritance: Land is derived from Vehicle, in which Vehicle is the base class of the derivation. Here is how such inheritance is achieved:

class Land: public Vehicle { public: Land(); Land(unsigned wt, unsigned sp); void setspeed(unsigned sp); unsigned getspeed() const; private: unsigned speed; }; By postfixing the class name Land in its definition by : public Vehicle the derivation is realized: the class Land now contains all the functionality of its base class Vehicle plus its own specific information and functionality. The extra functionality consists here of a constructor with two arguments and interface functions to access the speed data member. (The derivation in this example mentions the keyword public: public derivation. C++ also implements private derivation and protected derivation, both of which are not often used and which we will therefore leave to the reader to uncover.). To illustrate the use of the derived class Land consider the following example: Land veh(1200, 145); int main() { cout << "Vehicle weighs " << veh.getweight() << endl << "Speed is " << veh.getspeed() << endl; } This example shows two features of derivation. First, getweight() is no direct member of a Land. Nevertheless it is used in veh.getweight(). This member function is an implicit part of the class, inherited from its ` parent' vehicle.

Second, although the derived class Land now contains the functionality of Vehicle, the private fields of Vehicle remain private in the sense that they can only be accessed by member functions of Vehicle itself. This means that the member functions of Land must use the interface functions (getweight(), setweight()) to address the weight field; just as any other code outside the Vehicle class. This restriction is necessary to enforce the principle of data hiding. The class Vehicle could, e.g.,

Page 92: Object Oriented Programing using C++

Object Oriented Programming using C++

92

be recoded and recompiled, after which the program could be relinked. The class Land itself could remain unchanged.

Actually, the previous remark is not quite right: If the internal organization of Vehicle changes, then the internal organization of Land objects, containing the data of Vehicle, changes as well. This means that objects of the Land class, after changing Vehicle, might require more (or less) memory than before the modification. However, in such a situation we still don't have to worry about the use of member functions of the parent class Vehicle in the class Land. We might have to recompile the Land sources, though, as the relative locations of the data members within the Land objects will have changed due to the modification of the Vehicle class.

As a rule of thumb, classes which are derived from other classes must be fully recompiled (but don't have to be modified) after changing the data organization of their base classes. As adding new member functions to the base class doesn't alter the data organization, no recompilation is needed after adding new member functions. (A subtle point to note, however, is that adding a new member function that happens to be the first virtual member function of a class results in a hidden pointer to a table of pointers to virtual functions. In the following example we assume that the class Auto, representing automobiles, should contain the weight, speed and name of a car. This class is therefore derived from Land:

class Auto: public Land { public: Auto(); Auto(unsigned wt, unsigned sp, char const *nm); Auto(Auto const &other); ~Auto(); Auto const &operator=(Auto const &other); char const *getname() const; void setname(char const *nm); private: char const *name; }; In the above class definition, Auto is derived from Land, which in turn is derived from Vehicle. This is called nested derivation: Land is called Auto's direct base class, while Vehicle is called the indirect base class.

Note the presence of a destructor, a copy constructor and an overloaded assignment operator in the class Auto. Since this class uses a pointer to reach dynamically allocated memory, these members should be part of the class interface.

The constructor of a derived class

As mentioned earlier, a derived class inherits the functionality from its base class. In this section we shall describe the effects of the inheritance on the constructor of a derived class.

As will be clear from the definition of the class Land, a constructor exists to set both the weight and the speed of an object. The poor-man's implementation of this constructor could be:

Land::Land (unsigned wt, unsigned sp) { setweight(wt);

Page 93: Object Oriented Programing using C++

Object Oriented Programming using C++

93

setspeed(sp); } This implementation has the following disadvantage. The C++ compiler will generate code to call the default constructor of a base class from each constructor in the derived class, unless explicitly instructed otherwise.

Consequently, in the above implementation the default constructor of Vehicle is called, which probably initializes the weight of the vehicle, only to be redefined immediately thereafter by the function setweight().

A better approach is of course directly to call the constructor of Vehicle that expects an unsigned weight argument. The syntax to achieve this is to mention the constructor to be called (supplied with an argument) immediately following the argument list of the constructor of the derived class itself. The use of such a base class initializer is shown below:

Land::Land(unsigned wt, unsigned sp) : Vehicle(wt) { setspeed(sp); }

The destructor of a derived class

Destructors of classes are automatically called when an object is destroyed. This rule also holds true for objects of classes that are derived from other classes. Assume we have the following situation: class Base { public: ~Base(); }; class Derived { public: ~Derived(); }; int main() { Derived derived; } At the end of the main() function, the derived object ceases to exists. Hence, its destructor Derived::~Derived() is called. However, since derived is also a Base object, the Base::~Base() destructor is called as well.

It is this not necessary to call the Base::~Base() destructor explicitly from the Derived::~Derived() destructor.

Constructors and destructors are called in a stack-like fashion: when derived is constructed, the appropriate Base constructor is called first, then the appropriate Derived constructor is called. When derived is destroyed, the Derived destructor is called first, and then the Base destructor is called for

Page 94: Object Oriented Programing using C++

Object Oriented Programming using C++

94

that object. In general, a derived class destructor is called before a base class destructor is called.

Redefining member functions

The functionality of all members which are defined in a base class (and which are therefore also available in derived classes) can be redefined. This feature is illustrated in this section.

Let's assume that the vehicle classification system should be able to represent trucks, consisting of two parts: the front engine, which pulls a trailer. Both the front engine and the trailer have their own weights, but the getweight() function should return the combined weight.

The definition of a Truck therefore starts with the class definition, derived from Auto but it is then expanded to hold one more unsigned field representing the additional weight information. Here we choose to represent the weight of the front part of the truck in the Auto class and to store the weight of the trailer in an additional field:

class Truck: public Auto { public: Truck(); Truck(unsigned engine_wt, unsigned sp, char const *nm, unsigned trailer_wt); void setweight(unsigned engine_wt, unsigned trailer_wt); unsigned getweight() const; private: unsigned trailer_weight; }; Truck::Truck(unsigned engine_wt, unsigned sp, char const *nm, unsigned trailer_wt) : Auto(engine_wt, sp, nm) { trailer_weight = trailer_wt; } Note that the class Truck now contains two functions already present in the base class Auto: setweight() and getweight(). � The redefinition of setweight() poses no problems: this function is simply redefined to perform actions which are specific to a Truck object. � The redefinition of setweight(), however, will hide Auto::setweight(): for a Truck only the setweight() function having two unsigned arguments can be used. � The Vehicle's setweight() function remains available for a Truck, but it must now be called explicitly, as Auto::setweight() is now hidden from view. This latter function is hidden, even though Auto::setweight() has only one unsigned argument. To implement Truck::setweight() we could write: void Truck::setweight(unsigned engine_wt, unsigned trailer_wt) { trailer_weight = trailer_wt; Auto::setweight(engine_wt); // note: Auto:: is required } � Outside of the class the Auto-version of setweight() is accessed through the scope resolution operator. So, if a Truck t needs to set its Auto weight, it must use t.Auto::setweight(x);

Page 95: Object Oriented Programing using C++

Object Oriented Programming using C++

95

� An alternative to using the scope resolution operator is explicitly to include a member function having the same function prototype as the base class member function in the class interface as an inline function. This might be an elegant solution for the occasional situation. E.g., we add the following to the interface of the class Truck: void setweight(unsigned engine_wt) { Auto::setweight(engine_wt); } Now the single argument setweight() member function can be used by Truck objects without having to use the scope resolution operator. As the function is defined inline, no overhead of an extra function call is involved. � The function getweight() also is already defined in Auto, as it was inherited from Vehicle. In this case, the class Truck should redefine this member function to allow for the extra (trailer) weight in the Truck: unsigned Truck::getweight() const { return ( // sum of: Auto::getweight() + // engine part plus trailer_weight // the trailer ); } The next example shows the actual use of the member functions of the class Truck to display several weights: int main() { Land veh(1200, 145); Truck lorry(3000, 120, "Juggernaut", 2500); lorry.Vehicle::setweight(4000); cout << endl << "Truck weighs " << lorry.Vehicle::getweight() << endl << "Truck + trailer weighs " << lorry.getweight() << endl << "Speed is " << lorry.getspeed() << endl << "Name is " << lorry.getname() << endl; } Note the explicit call of Vehicle::setweight(4000): assuming setweight(unsigned engine_wt) is not part of the interface of the class Truck, it must be called explicitly, using the Vehicle:: scope resolution, as the single argument function setweight() is hidden from direct view in the class Truck.

The situation with Vehicle::getweight() and Truck::getweight() is a different one: here the function Truck::getweight() is a redefinition of Vehicle::getweight(), so in order to reach Vehicle::getweight() a scope resolution operation (Vehicle::) is required.

Multiple inheritance

Up to now, a class was always derived from one base class. C++ also allows supports multiple derivation, in which a class is derived from several base classes and hence inherits the functionality of multiple parent classes at the same time. In cases where multiple inheritance is considered, it should be defensible to consider the newly derived class an instantiation of both base classes. If that is not really the case, composition might be more appropriate. In general, linear derivation, in which only one base class is used, is seen much more frequently than

Page 96: Object Oriented Programing using C++

Object Oriented Programming using C++

96

multiple derivation. Most objects have a primary purpose, and that's it. But then, consider the prototype of an object for which multiple inheritance was used to its extreme: the Swiss army knife! This object is a knife, it is a pair of scissors, it is a can-operner, it is a corkscrew, it is ....

How can we construct a `Swiss army knife' in C++? First we need (at least) two base classes. For example, let's assume we are designing a toolkit for the layout of a cockpit instrument panel in an aircraft. We design all kinds of instruments, like an artifical horizon and an altimeter. One of the components that is often seen in aircraft is a nav-com set: a combination of a navigational beacon receiver (the `nav' part) and a radio communication unit (the `com'-part). To define the nav-com set, we first design the NavSet class. For the time being, its data members are omitted:

class NavSet { public: NavSet(Intercom &intercom, VHF_Dial &dial); unsigned getActiveFrequency() const; unsigned getStandByFrequency() const; void setStandByFrequency(unsigned freq); unsigned toggleActiveStandby(); void setVolume(unsigned level); void identEmphasis(bool on_off); }; In the class's contructor we assume the availability of the classes Intercom, which is used by the pilot to listen to the information that is transmitted through the navigational beacon, and a class VHF_Dial which is used to represent visually what the NavSet receives.

Next we construct the ComSet class. Again, omitting the data members:

class ComSet { public: ComSet(Intercom &intercom); unsigned getFrequency() const; unsigned getPassiveFrequency() const; void setPassiveFrequency(unsigned freq); unsigned toggleFrequencies(); void setAudioLevel(unsigned level); void powerOn(bool on_off); void testState(bool on_off); void transmit(Message &message); }; In this class we can receive messages, which are transmitted though the Intercom, but we can also transmit messages, using a Message object which is passed to the ComSet object using its transmit() member function.

Now we're ready to construct the NavCom set:

class NavComSet: public ComSet, public NavSet { public: NavComSet(Intercom &intercom, VHF_Dial &dial); };

Page 97: Object Oriented Programing using C++

Object Oriented Programming using C++

97

Done. Now we have defined a NavComSet which is both a NavSet and a ComSet: the possibilities of either base class are now available in the derived class, using multiple derivation.

With multiple derivation, please note the following:

� The keyword public is present before both base class names (NavSet and ComSet). This is so because the default derivation in C++ is private: the keyword public must be repeated before each base class specification. The base classes do not have to have the same kind of derivation: one base class could have public derivation, another base class could use protected derivation, yet another base class could use private derivation. � The multiply derived class NavComSet introduces no additional functionality of its own, but merely combines two existing classes into a new aggregate class. Thus, C++ offers the possibility to simply sweep multiple simple classes into one more complex class.

This feature of C++ is frequently used. Usually it pays to develop `simple' classes each having a simple, well-defined functionality. More complex classes can always be constructed from these simpler building blocks.

� Here is the implementation of The NavComSet constructor: NavComSet::NavComSet(Intercom &intercom, VHF_Dial &dial) : ComSet(intercom), NavSet(intercom, VHF_Dial) {} The constructor requires no extra code: Its only purpose is to activate the constructors of its base classes. The order in which the base class initializers are called is not dictated by their calling order in the constructor code, but by the order in which the base classes are specified in the class interface. � the NavComSet class definition needs no extra data members or member functions: here (and often) the inherited interfaces provide all the required functionality and data for the multiply derived class to operate properly. Of course, while defining the base classes, we made life easy on ourselves by strictly using different member function names. So, there is a function setVolume() in the NavSet class and a function setAudioLevel() in the ComSet class. A bit cheating, since we could expect that bits units in fact use a composed object Amplifier, which deals with the volume setting. A revised class might then either use a Amplifier &getAmplifier() const member function, and leave it to the application to set up its own interface to the amplifier, or access functions for, e.g., the volume are made available through the NavSet and ComSet classes as, normally, member functions having the same names (e.g., setVolume()). In situations where two base classes use the same member function names, special provisions need to be made to prevent ambiguity: � The intended base class can explicitly be specified, using the base class name and scope resolution operator in combination with the doubly occurring member function name: NavComSet navcom(intercom, dial); navcom.NavSet::setVolume(5); // sets the NavSet volume level navcom.ComSet::setVolume(5); // sets the ComSet volume level � The class interface is extended by member functions which do the explicitation for the user of the class. These extra functions will normally be defined as inline: class NavComSet: public ComSet, public NavSet { public: NavComSet(Intercom &intercom, VHF_Dial &dial); void comVolume(unsigned volume) {

Page 98: Object Oriented Programing using C++

Object Oriented Programming using C++

98

ComSet::setVolume(volume); } void navVolume(unsigned volume) { NavSet::setVolume(volume); } }; � If the NavComSet class is obtained from a third party, and should not be altered, a wrapper class could be used which does the previous explicitatioon for us in our own programs: class MyNavComSet: public NavComSet { public: MyNavComSet(Intercom &intercom, VHF_Dial &dial) : NavComSet(intercom, dial); {} void comVolume(unsigned volume) { ComSet::setVolume(volume); } void navVolume(unsigned volume) { NavSet::setVolume(volume); } };

Conversions between base classes and derived classes

When inheritance is used in the definition of classes, it can be said that an object of a derived class is at the same time an object of the base class. This has important consequences for the assignment of objects, and for the situation where pointers or references to such objects are used. Both situations will be discussed next.

Conversions in object assignments

We start by defining two objects, one of a base class and one of a derived class: ComSet com(intercom); NavComSet navcom(intercom2, dial2); The object navcom is constructed using an Intercom and a Dial object. However, a NavComSet is at the same time a ComSet, which makes the assignment from navcom (a derived class object) to com (a base class object) possible: com = navcom; The effect of this assignment should be that the object com will now communicate with intercom2. As a ComSet does not have a VHF_Dial, the navcom's dial is ignored by the assignment: when assigning a base class object from a derived class object only the base class data members are assigned, other data members are ignored. The assignment from a base class object to a derived class object, however, is problematic: In a statement like

Page 99: Object Oriented Programing using C++

Object Oriented Programming using C++

99

navcom = com; it isn't clear how to reassign the NavComSet's VHF_Dial data member as they are missing in the ComSet object com. Such an assignment is therefore refused by the compiler.

The following general rule applies: in assignments in which base class objects and derived class objects are involved, assignments in which data are dropped is legal. However, assignments in which data would remain unspecified is not allowed. Of course, it is possible to redefine an overloaded assignment operator to allow the assignment of a derived class object by a base class object. E.g., to achieve compilability of a statement

navcom = com; the class NavComSet must have an overloaded assignment operator function accepting a ComSet object for its argument. It would be the programmer's responsibility to decide what to do with the missing data.

Conversions in pointer assignments

We return to our Vehicle classes, and define the following objects and pointer variable: Land land(1200, 130); Auto auto(500, 75, "Daf"); Truck truck(2600, 120, "Mercedes", 6000); Vehicle *vp; Now we can assign the addresses of the three objects of the derived classes to the Vehicle pointer: vp = &land; vp = &auto; vp = &truck; Each of these assignments is acceptable. However, an implicit conversion of the derived class to the base class Vehicle is used, since vp is defined as a pointer to a Vehicle. Hence, when using vp only the member functions which manipulate the weight can be called as this is the only functionality of a Vehicle, which is the object vp points to, as far as the compiler can tell.

The same reasoning holds true for references to Vehicles. If, e.g., a function is defined with a Vehicle reference parameter, the function may be passed an object of a class that is derived from Vehicle. Inside the function, the specific Vehicle members of the object of the derived class remain accessible. This analogy between pointers and references holds true in general. Remember that a reference is nothing but a pointer in disguise: it mimics a plain variable, but actually it is a pointer.

This restricted functionality furthermore has an important consequence for the class Truck. After the statement vp = &truck, vp points to a Truck object. So, vp->getweight() will return 2600 instead of 8600 (the combined weight of the cabin and of the trailer: 2600 + 6000), which would have been returned by t.getweight().

When a function is called via a pointer to an object, then the type of the pointer and not the type of the object itself determines which member functions are available and executed. In other

Page 100: Object Oriented Programing using C++

Object Oriented Programming using C++

100

words, C++ implicitly converts the type of an object reached via a pointer to the type of the pointer.

If the actual type of the object to which a pointer points is known, an explicit type cast can be used to access the full set of member functions that are available for the object:

Truck truck; Vehicle *vp; vp = &truck; // vp now points to a truck object Truck *trp; trp = reinterpret_cast<Truck *>(vp); cout << "Make: " << trp->getname() << endl; Here, the second to last statement specifically casts a Vehicle * variable to a Truck *. As is usually the case with type casts, this code is not without risk: it will only work if vp really points to a Truck. Otherwise the program may behave unexpectedly.

Polymorphism C++ provides the tools to derive classes from base classes, and to use base class pointers to address derived objects. As we've seen, when using a base class pointer to address an object of a derived class, the type of the pointer determines which member function will be used. This means that a Vehicle *vp, pointing to a Truck object, will incorrectly compute the truck's combined weight in a statement like vp->getweight(). The reason for this should now be clear: vp calls Vehicle::getweight() and not Truck::getweight(), even though vp actually points to a Truck.

Fortunately, a remedy is available. In C++ it is possible for a Vehicle *vp to call a function Truck::getweight() when the pointer actually points to a Truck.

The terminology for this feature is polymorphism: it is as though the pointer vp changes its type from a base class pointer to a pointer to the class of the object it actually points to. So, vp might behave like a Truck * when pointing to a Truck, and like an Auto * when pointing to an Auto etc.. (In one of the StarTrek movies, Capt. Kirk was in trouble, as usual. He met an extremely beautiful lady who, however, later on changed into a hideous troll. Kirk was quite surprised, but the lady told him: ``Didn't you know I am a polymorph?'')

Polymorphism is realized by a feature called late binding. This refers to the fact that the decision which function to call (a base class function or a function of a derived class) cannot be made compile-time, but is postponed until the program is actually executed: the actual member function to be used is selected run-time.

Virtual functions

The default behavior of the activation of a member function via a pointer or reference is that the type of the pointer (or reference) determines the function that is called. E.g., a Vehicle * will activate Vehicle's member functions, even when pointing to an object of a derived class. This is referred to

Page 101: Object Oriented Programing using C++

Object Oriented Programming using C++

101

as early or static binding, since the type of function is known compile-time. The late or dynamic binding is achieved in C++ using virtual member functions.

A member function becomes a virtual member function when its declaration starts with the keyword virtual. Once a function is declared virtual in a base class, it remains a virtual member function in all derived classes; even when the keyword virtual is not repeated in a derived class.

As far as the vehicle classification system is concerned the two member functions getweight() and setweight() might well be declared virtual. The relevant sections of the class definitions of the class Vehicle and Truck are shown below. Also, we show the implementations of the member functions getweight() of the two classes:

class Vehicle { public: virtual int getweight() const; virtual void setweight(int wt); }; class Truck: public Auto { public: void setweight(int engine_wt, int trailer_wt); int getweight() const; }; int Vehicle::getweight() const { return (weight); } int Truck::getweight() const { return (Auto::getweight() + trailer_wt); } Note that the keyword virtual only needs to appear in the definition of the Vehicle base class. There is no need (but there is also no penalty) to repeat it in derived classes: once virtual, always virtual. On the other hand, a function may be declared virtual anywhere in a class hierarchy: the compiler will be perfectly happy if getweight() is declared virtual in Auto, rather than in Vehicle. However, the specific characteristics of virtual member functions would then, for the member function getweight(), only appear with Auto (and its derived classes) pointers or references. With a Vehicle pointer, static binding will remain to be used. The effect of late binding is illustrated below: Vehicle v(1200); // vehicle with weight 1200 Truck t(6000, 115, // truck with cabin weight 6000, speed 115, "Scania", // make Scania, trailer weight 15000 15000); Vehicle *vp; // generic vehicle pointer int main() { vp = &v; // see (1) below cout << vp->getweight() << endl; vp = &t; // see (2) below cout << vp->getweight() << endl; cout << vp->getspeed() << endl; // see (3) below }

Page 102: Object Oriented Programing using C++

Object Oriented Programming using C++

102

Since the function getweight() is defined virtual, late binding is used: � at (1), Vehicle::getweight() is called. � at (2) Truck::getweight() is called. � at (3) a syntax error is generated. The member getspeed() is no member of Vehicle, and hence not callable via a Vehicle*. The example illustrates that when using a pointer to a class, only the functions which are members of that class can be called. These functions may be virtual, but this only influences the type of binding (early vs. late).

A virtual member function cannot be a static member function: a virtual member function is still a ordinary member function in that it has a this pointer. As static member functions have no this pointer, they cannot be declared virtual.

Virtual destructors

When the operator delete releases memory which is occupied by a dynamically allocated object, or when an object goes out of scope, the appropriate destructor is called to ensure that memory allocated by the object is also deleted. Vehicle *vp = new Land(1000, 120); delete vp; // object destroyed In this example an object of a derived class (Land) is destroyed using a base class pointer (Vehicle *). For a `standard' class definition this will mean that the destructor of Vehicle is called, instead of the destructor of the Land object. This not only results in a memory leak when memory is allocated in Land, but it will also prevent any other task, normally performed by the derived class' destructor from being completed (or, better: started). A Bad Thing.

In C++ this problem is solved using a virtual destructor. By applying the keyword virtual to the declaration of a destructor the appropriate derived class destructor is activated when the argument of the delete operator is a base class pointer. In the following partial class definition the declaration of such a virtual destructor is shown:

class Vehicle { public: virtual ~Vehicle(); virtual unsigned getweight() const; }; By declaring a virtual destructor, the above delete operation (delete vp) will correctly call de the Land's destructor, rather than the Vehicle's destructor.

From this discussion we are now able to formulate the following situations in which a destructor should be defined:

� A destructor should be defined when memory is allocated and managed by objects of the class. � A virtual destructor should be defined if the class contains at least one virtual member function. In the second case, the destructor will have no special tasks to perform. The virtual destructor will therefore often be defined empty. For example, the definition of Vehicle::~Vehicle() may be as simple as: Vehicle::~Vehicle() {}

Page 103: Object Oriented Programing using C++

Object Oriented Programming using C++

103

Often this will be part of the class interface as an inline destructor.

Pure virtual functions

Until now the base class Vehicle contained its own, concrete, implementations of the virtual functions getweight() and setweight(). In C++ it is however also possible only to mention virtual member functions in a base class, without actually defining them. The functions are concretely implemented in a derived class. This approach defines a protocol, which has to be followed in the derived classes. This implies that derived classes must take care of the actual definition: the C++ compiler will not allow the definition of an object of a class in which one or more member functions are left undefined. The base class thus enforces a protocol by declaring a function by its name, return value and arguments. The derived classes must take care of the actual implementation. The base class itself defines therefore only a model or mold, to be used when other classes are derived. Such base classes are also called abstract classes.

The functions which are only declared in the base class are called pure virtual functions. A function is made pure virtual by preceding its declaration with the keyword virtual and by postfixing it with = 0. An example of a pure virtual function occurs in the following listing, where the definition of a class Object requires the implementation of the conversion operator operator string():

#include <string> class Object { public: virtual operator string() const = 0; }; Now, all classes derived from Object must implement the operator string() member function, or their objects cannot be constructed. This is neat: all objects derived from Object can now always be considered string objects, so they can, e.g., be inserted into ostream objects.

Should the virtual destructor of a base class be a pure virtual function? The answer to this question is no: a class such as Vehicle should not require derived classes to define a destructor. In contrast, Object::operator string() can be a pure virtual function: in this case the base class defines a protocol which must be adhered to.

Realize what would happen if we would define the destructor of a base class as a pure virtual destructor: according to the compiler, the derived class object can be constructed: as its destructor is defined, the derived class is not a pure abstract class. However, inside the derived class destructor, the destructor of its base class is implicitly called. This destructor was never defined, and the linker will loudly complain about an undefined reference to, e.g., Virtual::~Virtual().

Often, but not necessarily always, pure virtual member functions are const member functions. This allows the construction of constant derived class objects. In other situations this might not be necessary (or realistic), and non-constant member functions might be required. The general rule for const member functions applies also to pure virtual functions: if the member function will alter the object's data members, it cannot be a const member function. Often abstract base classes have no data members. However, the prototype of the pure virtual member function must be used again in derived classes. If the implementation of a pure virtual function in a derived class alters the data of the derived class object, than that function cannot be declared as a const member function. Therefore, the constructor of an abstract base class should well consider whether a pure virtual member function should be a const member function or not.

Page 104: Object Oriented Programing using C++

Object Oriented Programming using C++

104

Virtual functions in multiple inheritance

it is possible to derive a class from several base classes. Such a derived class inherits the properties of all its base classes. Of course, the base classes themselves may be derived from classes yet higher in the hierarchy.

A slight difficulty in multiple inheritance may arise when more than one `path' leads from the derived class to the base class. This is illustrated in the code example below: a class Derived is doubly derived from a class Base:

class Base { public: void setfield(int val) { field = val; } int getfield() const { return (field); } private: int field; }; class Derived: public Base, public Base { }; Due to the double derivation, the functionality of Base now occurs twice in Derived. This leads to ambiguity: when the function setfield() is called for a Derived object, which function should that be, since there are two? In such a duplicate derivation, many C++ compilers will refuse to generate code and will (correctly) identify an error.

The above code clearly duplicates its base class in the derivation. Such a duplication can here easily be easily. But duplication of a base class can also occur through nested inheritance, where an object is derived from, say, an Auto and from an Air. Such a class would be needed to represent, e.g., a flying car (such as the one in James Bond vs. the Man with the Golden Gun...). An AirAuto would ultimately contain two Vehicles, and hence two weight fields, two setweight() functions and two getweight() functions.

Ambiguity in multiple inheritance

Let's investigate closer why an AirAuto introduces ambiguity, when derived from Auto and Air. � An AirAuto is an Auto, hence a Land, and hence a Vehicle. � However, an AirAuto is also an Air, and hence a Vehicle. The duplication of Vehicle data is further illustrated in figure 12

figure 12: Duplication of a base class in multiple derivation.

The internal organization of an AirAuto is shown in figure 13

Page 105: Object Oriented Programing using C++

Object Oriented Programming using C++

105

figure 13: Internal organization of an AirAuto object.

The C++ compiler will detect the ambiguity in an AirAuto object, and will therefore fail to compile a statement like:

AirAuto cool; cout << cool.getweight() << endl; The question of which member function getweight() should be called, cannot be answered by the compiler. The programmer has two possibilities to resolve the ambiguity explicitly: � First, the function call where the ambiguity occurs can be modified. This is done with the scope resolution operator: // let's hope that the weight is kept in the Auto // part of the object.. cout << cool.Auto::getweight() << endl; Note the position of the scope operator and the class name: before the name of the member function itself. � Second, a dedicated function getweight() could be created for the class AirAuto: int AirAuto::getweight() const { return(Auto::getweight()); } The second possibility from the two above is preferable, since it relieves the programmer who uses the class AirAuto of special precautions.

However, apart from these explicit solutions, there is a more elegant one, which will be introduced in the next section.

Virtual base classes

more than one object of the class Vehicle is present in an AirAuto. The result is not only an ambiguity in the functions which access the weight data, but also the presence of two weight fields. This is somewhat redundant, since we can assume that an AirAuto has just one weight.

We can achieve the situation that only one Vehicle will be contained in an AirAuto. This is done by ensuring that the base class which is multiply present in a derived class, is defined as a virtual base class. For the class AirAuto this means that the derivation of Land and Air is changed:

class Land: virtual public Vehicle {

Page 106: Object Oriented Programing using C++

Object Oriented Programming using C++

106

// etc }; class Air: virtual public Vehicle { // etc }; class AirAuto: public Land, public Air { }; The virtual derivation ensures that via the Land route, a Vehicle is only added to a class when a virtual base class was not yet present. The same holds true for the Air route. This means that we can no longer say via which route a Vehicle becomes a part of an AirAuto; we can only say that there is an embedded Vehicle object. The internal organization of an AirAuto after virtual derivation is shown in figure 14

figure 14: Internal organization of an AirAuto object when the base classes are virtual.

There are several points worth noting when using virtual derivation:

� When base classes of a class usng multiple derivation are themselves virtually derived from a base class (as shown above), the base class constructor which is normally called when the derived class constructor is called is no longer called: its base class initializer: ignored base class initializer is ignored. Instead, the base class constructor will be callled independently from the derived class constructors. Assume we have two classes, Derived1 and Derived2, both (possibly virtually) derived from Base. We will address the question which constructors will be called when a class Final: public Derived1, public Derived2 is defined. To distinguish the several constructors that are involved, we will use Base1() to indicate the Base class constructor that is called as base class initializer for Derived1 (and analogously: Base2() belonging to Derived2), while Base() indicates the default constructor of the class Base. Apart from the Base class constructor, we use Derived1() and Derived2() to indicate the base class initializers for the class Final. We now distinguid the following situation, for constructors of the class Final: public Derived1, public Derived2: � classes: Derived1: public Base, Derived2: public Base

This is the normal, non virtual multiple derivation. There are two Base classes in the Final object, and the following constructors will be called (in the mentioned order):

� Base1(), Derived1(), Base2(), Derived2()

� classes: Derived1: public Base, Derived2: virtual public Base

Only Derived2 uses virtual derivation. For the Derived2 part the base class initializer will be omitted, and the default Base class constructor will be called. Furthermore, this `detached' base class constructor will be called first:

Page 107: Object Oriented Programing using C++

Object Oriented Programming using C++

107

� Base(), Base1(), Derived1(), Derived2() Note that Base() is called first, not Base1(). Also note that, as only one derived class uses virtual derivation, there are still two Base class objects in the eventual Final class. Merging of base classes only occurs with multiple virtual base classes.

� classes: Derived1: virtual public Base, Derived2: public Base

Only Derived1 uses virtual derivation. For the Derived1 part the base class initializer will now be omitted, and the default Base class constructor will be called instead. Note the difference with the first case: Base1() is replaced by Base(). Should Derived1 happen to use the default Base constructor, no difference would be noted here with the first case:

� Base(), Derived1(), Base2(), Derived2()

� classes: Derived1: virtual public Base, Derived2: virtual public Base

Here both derived classes use virtual derivation, and so only one Base class object will be present in the Final class. Note that now only one Base class constructor is called: for the detached (merged) Base class object:

� Base(), Derived1(), Derived2()

� Virtual derivation is, in contrast to virtual functions, a pure compile-time issue: whether a derivation is virtual or not defines how the compiler builds a class definition from other classes. Summarizing, using virtual derivation avoids ambiguity in the calling of member functions of a base class. Furthermore, duplication of data members is avoided.

When virtual derivation is not appropriate

In contrast to the previous definition of a class such as AirAuto, situations may arise where the double presence of the members of a base class is appropriate. class Truck: public Auto { public: Truck(); Truck(int engine_wt, int sp, char const *nm, int trailer_wt); void setweight(int engine_wt, int trailer_wt); int getweight() const; private: int trailer_weight; }; Truck::Truck(int engine_wt, int sp, char const *nm, int trailer_wt) : Auto(engine_wt, sp, nm) { trailer_weight = trailer_wt; } int Truck::getweight() const { return ( // sum of: Auto::getweight() + // engine part plus

Page 108: Object Oriented Programing using C++

Object Oriented Programming using C++

108

trailer_wt // the trailer ); } This definition shows how a Truck object is constructed to contain two weight fields: one via its derivation from Auto and one via its own int trailer_weight data member. Such a definition is of course valid, but it could also be rewritten. We could derive a Truck from an Auto and from a Vehicle, thereby explicitly requesting the double presence of a Vehicle; one for the weight of the engine and cabin, and one for the weight of the trailer. A small point of interest here is that a derivation like class Truck: public Auto, public Vehicle is not accepted by the C++ compiler: a Vehicle is already part of an Auto, and is therefore not needed. An intermediate class solves the problem: we derive a class TrailerVeh from Vehicle, and Truck from Auto and from TrailerVeh. All ambiguities concerning the member functions are then be solved for the class Truck: class TrailerVeh: public Vehicle { public: TrailerVeh(int wt); }; TrailerVeh::TrailerVeh(int wt) : Vehicle(wt) { } class Truck: public Auto, public TrailerVeh { public: Truck(); Truck(int engine_wt, int sp, char const *nm, int trailer_wt); void setweight(int engine_wt, int trailer_wt); int getweight() const; }; Truck::Truck(int engine_wt, int sp, char const *nm, int trailer_wt) : Auto(engine_wt, sp, nm), TrailerVeh(trailer_wt) { } int Truck::getweight() const { return ( // sum of: Auto::getweight() + // engine part plus TrailerVeh::getweight() // the trailer ); }

Run-Time Type identification

C++ offers two ways to retrieve the type of objects and expressions while the program is running. The possibilities of C++'s run-time type identification are somewhat limited compared to languages like Java. Normally, C++ uses static type checking and static type identification. Static type checking and determination is possibly safer and certainly more efficient than run-time type

Page 109: Object Oriented Programing using C++

Object Oriented Programming using C++

109

identification, and should therefore be used wherever possible. Nonetheles, C++ offers run-time type identification by providing the dynamic cast and typeid operators. � The dynamic_cast<>() operator can be used to convert a base class base class: coverting to derived class pointer or reference to a derived class pointer or reference. � The typeid operator returns the actual type of an expression. These operators operate on class type objects, containing at least one virtual member function.

The dynamic_cast operator

The dynamic_cast<>() operator is used to convert a base class pointer or reference to, respectively, a derived class pointer or reference.

A dynamic cast is performed run-time. A prerequisiste for the use of the dynamic cast operator is the existence of at least one virtual member function in the base class.

In the following example a pointer to the class Derived is obtained from the Base class pointer bp:

class Base { public: virtual ~Base(); }; class Derived: public Base { public: char const *toString() { return ("Derived object"); } }; int main() { Base *bp; Derived *dp, d; bp = &d; dp = dynamic_cast<Derived *>(bp); if (dp) cout << dp->toString() << endl; else cout << "dynamic cast conversion failed\n"; } Note the test: in the if condition the success of the dynamic cast is checked. This must be done run-time, as the compiler can't do this all by itself. If a base class pointer is provided the dynamic cast operator returns 0 on failure, and a pointer to the requested derived class on success. Consequently, if there are multiple derived classes, a series of checks could be performed to find the actual derived class to which the pointer points (In de next example derived classes are declared only): class Base { public: virtual ~Base(); };

Page 110: Object Oriented Programing using C++

Object Oriented Programming using C++

110

class Derived1: public Base; class Derived2: public Base; int main() { Base *bp; Derived1 *d1, d; Derived2 *d2; bp = &d; if ((d1 = dynamic_cast<Derived1 *>(bp))) cout << *d1 << endl; else if ((d2 = dynamic_cast<Derived2 *>(bp))) cout << *d2 << endl; } Alternatively, a reference to a base class object may be available. In this case the dynamic_cast<>() operator will throw an exception if it fails. For example: #include <iostream> #include <typeinfo> class Base { public: virtual ~Base() {} virtual char const *toString() {} }; class Derived1: public Base {}; class Derived2: public Base {}; void process(Base &b) { try { cout << dynamic_cast<Derived1 &>(b).toString() << endl; return; } catch (std::bad_cast) {} try { cout << dynamic_cast<Derived2 &>(b).toString() << endl; return; } catch (std::bad_cast) {} } int main() { Derived1 d; process(d); }

Page 111: Object Oriented Programing using C++

Object Oriented Programming using C++

111

In this example the value std::bad_cast is introduced. The std::bad_cast is thrown as an exception if the dynamic cast of a reference to a base class object fails. Apparently bad_cast is the name of a type ( ). In section EMPTYENUM the construction of such a type is discussed.

The dynamic cast operator is a handy tool when an existing base class cannot or should not be modified (e.g., when the sources are not available), and a derived class may be modified instead. Code receiving a base class pointer or reference may then perform a dynamic cast to the derived class to be able to use the derived class' functionality.

Casts from a base class reference or pointer to a derived class reference or pointer are called downcasts.

The typeid operator

As with the dynamic_cast<>() operator, the typeid is usually applied to base class objects, that are actually derived class objects. Similarly, the base class should contain one or more virtual functions.

In order to use the typeid operator, source files must

#include <typeinfo> Actually, the typeid operator returns an object of type type_info, which may, e.g., be compared to other type_info objects.

The class type_info may be implemented differently by different implementations, but at the very least it has the following interface:

class type_info { public: virtual ~type_info(); int operator==(const type_info &other) const; int operator!=(const type_info &other) const; char const *name() const; private: type_info(type_info const &other); type_info &operator=(type_info const &other); }; Note that this class has a private copy constructor and overloaded assignment operator. This prevents the normal construction or assignment of a type_info object. Type_info objects are constructed and returned by the typeid operator. Implementations, however, may choose to extend or elaborate the type_info class and provide, e.g., lists of functions that can be called with a certain class.

If the type_id operator is given a base class reference (where the base class contains at least one virtual function), it will indicate that the type of its operand is the derived class. For example:

class Base; // contains >= 1 virtual functions class Derived: public Base; Derived d; Base

Page 112: Object Oriented Programing using C++

Object Oriented Programming using C++

112

&br = d; cout << typeid(br).name() << endl; In this example the typeid operator is given a base class reference. It will print the text ``Derived'', being the class name of the class br actually refers to. If Base does not contain virtual functions, the text ``Base'' would have been printed.

The typeid operator can be used to determine the name of the actual type of expressions, not just of class type objects. For example:

cout << typeid(12).name() << endl; // prints: int cout << typeid(12.23).name() << endl; // prints: double Note, however, that the above example is suggestive at most of the type that is printed. It may be int and double, but this is not necessarily the case. If portability is required, make sure no tests against static, built-in strings are required. Check out what your compiler produces in case of doubt.

In situations where the typeid operator is applied to determine the type of a derived class, it is important to realize that a base class reference is used as the argument of the typeid operator. Consider the following example:

class Base; // contains at least one virtual function class Derived: public Base; Base *bp = new Derived; // base class pointer to derived object if (typeid(bp) == typeid(Derived *)) // 1: false ... if (typeid(bp) == typeid(Base *)) // 2: true ... if (typeid(bp) == typeid(Derived)) // 3: false ... if (typeid(bp) == typeid(Base)) // 4: false ... Here, (1) returns false as a Base * is not a Derived *. (2) returns true, as the two pointer types are the same, (3) and (4) return false as pointers to objects are not the objects themselves.

On the other hand, if *bp is used in the above expressions, then (1) and (2) return false as an object (or reference to an object) is not a pointer to an object, whereas with

if (typeid(*bp) == typeid(Derived)) // 3: true ... if (typeid(*bp) == typeid(Base)) // 4: false ... we see that (3) now returns true: *bp actually refers to a Derived class object, and typeid(*bp) will return typeid(Derived).

A similar result is obtained if a base class reference is used:

Base &br = *bp;

Page 113: Object Oriented Programing using C++

Object Oriented Programming using C++

113

if (typeid(br) == typeid(Derived)) // 3: true ... if (typeid(br) == typeid(Base)) // 4: false ...

A polymorphic exception class

Earlier we hinted at the possibility of designing a class Exception whose process() member would behave differently, depending on the kind of exception that was thrown. Now that we've introduced polymorphism, we can further develop this example.

It will now probably be clear that our class Exception should be a virtual base class, from which special exception handling classes can be derived. It could even be argued that Exception can be an abstract base class declaring only pure virtual member functions. The (abstract) base class Exception is designed as follows:

#ifndef _EXCEPTION_H_ #define _EXCEPTION_H_ #include <iostream> #include <string> class Exception { friend ostream &operator<<(ostream &str, Exception const &e) { return str << e.operator string(); } public: virtual ~Exception() {} virtual void process() const = 0; virtual operator string() const { return reason; } protected: Exception(char const *reason) : reason(reason) {} string reason; }; #endif The operator string() member function of course replaces the toString() member. The friend operator<<() function is using the (virtual) operator string() member so that we're able to insert an Exception object into an ostream. Apart from that, notice the use of a virtual destructor, doing nothing.

A derived class FatalException: public Exception could now be defined as follows (using a very basic process() implementation indeed):

#ifndef _FATALEXCEPTION_H_ #define _FATALEXCEPTION_H_ #include "exception.h" class FatalException: public Exception { public: FatalException(char const *reason)

Page 114: Object Oriented Programing using C++

Object Oriented Programming using C++

114

: Exception(reason) {} void process() const { exit(1); } }; #endif

(using derived classes WarningException and MessageException), constructed like FatalException:

#include <iostream> #include "message.h" #include "warning.h" void initialExceptionHandler(Exception const *e) { cout << *e << endl; // show the plain-text information if ( dynamic_cast<MessageException const *>(e) || dynamic_cast<WarningException const *>(e) ) { e->process(); // Process a message or a warning delete e; } else throw; // Pass on other types of Exceptions }

How polymorphism is implemented

This section briefly describes how polymorphism is implemented in C++. It is not necessary to understand how polymorphism is implemented if using this feature is the only intention. However, we think it's nice to know how polymorphism is at all possible. Besides, the following discussion does explain why there is a cost of polymorphism in terms of memory usage.

The fundamental idea behind polymorphism is that the compiler does not know which function to call compile-time; the appropriate function will be selected run-time. That means that the address of the function must be stored somewhere, to be looked up prior to the actual call. This `somewhere' place must be accessible from the object in question. E.g., when a Vehicle *vp points to a Truck object, then vp->getweight() calls a member function of Truck; the address of this function is determined from the actual object which vp points to.

A common implementation is the following: An object containing virtual member functions holds as its first data member a hidden field, pointing to an array of pointers containing the addresses of the virtual member functions. The hidden data member is usually called the vpointer, the array of virtual member function addresses the vtable. Note that the discussed implementation is compiler-dependent, and is by no means dictated by the C++ ANSI/ISO standard.

The table of addresses of virtual functions is shared by all objects of the class. Multiple classes may even share the same table. The overhead in terms of memory consumption is therefore:

� One extra pointer field per object, which points to:

Page 115: Object Oriented Programing using C++

Object Oriented Programming using C++

115

� One table of pointers per (derived) class to address the virtual functions. Consequently, a statement like vp->getweight() first inspects the hidden data member of the object pointed to by vp. In the case of the vehicle classification system, this data member points to a table of two addresses: one pointer for the function getweight() and one pointer for the function setweight(). The actual function which is called is determined from this table.

The internal organization of the objects having virtual functions is further illustrated in figure 15.

figure 15: Internal organization objects when virtual functions are defined.

As can be seen from figure 15, all objects which use virtual functions must have one (hidden) data member to address a table of function pointers. The objects of the classes Vehicle and Auto both address the same table. The class Truck, however, introduces its own version of getweight(): therefore, this class needs its own table of function pointers.

Templates Function templates

Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones:

template <class indetifier> function_declaration; template <typename indetifier> function_declaration;

the only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

For example, to create a template function that returns the greater one of two objects we could use:

template <class GenericType> GenericType GetMax (GenericType a, GenericType b) { return (a>b?a:b); }

Page 116: Object Oriented Programing using C++

Object Oriented Programming using C++

116

As specifies the first line, we have created a template for a generic data type that we have called GenericType. Therefore in the function that follows, GenericType becomes a valid data type and it is used as type for its two parameters a and b and as return value for the function GetMax.

GenericType still does not represent any concrete data type; when the function GetMax will be called we will be able to call it with any valid data type. This data type will serve as pattern and will replace GenericType in the function. The way to call a template class with a type pattern is the following:

function <pattern> (parameters);

Thus, for example, to call GetMax and to compare two integer values of type int we can write:

int x,y; GetMax <int> (x,y);

so GetMax will be called as if each appearance of GenericType was replaced by an int expression.

Ok, here is the complete example:

// function template #include <iostream.h> template <class T> T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int>(i,j); n=GetMax<long>(l,m); cout << k << endl; cout << n << endl; return 0; }

6 10

(In this case we have called the generic type T instead of GenericType because it is shorter and in addition is one of the most usual identifiers used for templates, although it is valid to use any valid identifier).

In the example above we used the same function GetMax() with arguments of type int and long having written a single implementation of the function. That is to say, we have written a function template and called it with two different patterns.

As you can see, within our GetMax() template function the type T can be used to declare new objects:

T result;

result is an object of type T, like a and b, that is to say, of the type that we enclose between angle-brackets <> when calling our template function.

Page 117: Object Oriented Programing using C++

Object Oriented Programming using C++

117

In this concrete case where the generic T type is used as parameter for function GetMax the compiler can find out automatically which data type is passed to it without having you to specify it with patterns <int> or <long>. So we could have written:

int i,j; GetMax (i,j);

since being both i and j of type int the compiler would assume automatically that the wished function is for type int. This implicit method is more usual and would produce the same result:

// function template II #include <iostream.h> template <class T> T GetMax (T a, T b) { return (a>b?a:b); } int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax(i,j); n=GetMax(l,m); cout << z << endl; cout << n << endl; return 0; }

6 10

Notice how in this case, within function main() we called our template function GetMax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determines what type is needed on each call.

Because our template function includes only one data type (class T) and both arguments it admits are both of that same type, we cannot call to our template function with two objects of different types as parameters:

int i; long l; k = GetMax (i,l);

It would be incorrect, since our function waits for two arguments of the same type (or class).

We can also make template-functions that admit more than one generic class or data type. For example:

template <class T, class U> T GetMin (T a, U b) { return (a<b?a:b); }

In this case, our template function GetMin() admits two parameters of different types and returns an object of the same type as the first parameter (T) that is passed. For example, after that declaration we could call the function writing:

int i,j; long l; i = GetMin<int,long> (j,l);

or even i = GetMin (j,l);

even although j and l are of different types.

Page 118: Object Oriented Programing using C++

Object Oriented Programming using C++

118

Class templates

We also have the possibility to write class templates, so that a class can have members based on generic types that do not need to be defined at the moment of creating the class or whose members use these generic types. For example:

template <class T> class pair { T values [2]; public: pair (T first, T second) { values[0]=first; values[1]=second; } };

The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:

pair<int> myobject (115, 36);

this same class would serve also to create an object to store any other type:

pair<float> myfloats (3.0, 2.18);

The only member function has been defined inline within the class declaration, nevertheless if this is not thus and we define a function member outside the declaration we always must also precede the definition with the prefix template <... >.

// class templates #include <iostream.h> template <class T> class pair { T value1, value2; public: pair (T first, T second) {value1=first; value2=second;} T getmax (); }; template <class T> T pair<T>::getmax () { T retval; retval = value1>value2? value1 : value2; return retval; } int main () { pair <int> myobject (100, 75); cout << myobject.getmax(); return 0; }

100

notice how the definition of member function getmax begins:

template <class T> T pair<T>::getmax ()

Page 119: Object Oriented Programing using C++

Object Oriented Programming using C++

119

All Ts that appear are necessary, reason why whenever you declare member functions you will have to follow a format similar to this (the second T makes reference to the type returned by the function, so this may vary).

Template specialization

A template specialization allows a template to make specific implementations for when the pattern is of a determined type. For example, suppose that our class template pair included a function to return the result of the module operation between the objects contained in it, but we only want that it works when the contained type is int and for the rest of types we want that this function always returns 0. This can be done this way:

// Template specialization #include <iostream.h> template <class T> class pair { T value1, value2; public: pair (T first, T second) {value1=first; value2=second;} T module () {return 0;} }; template <> class pair <int> { int value1, value2; public: pair (int first, int second) {value1=first; value2=second;} int module (); }; template <> int pair<int>::module() { return value1%value2; } int main () { pair <int> myints (100,75); pair <float> myfloats (100.0,75.0); cout << myints.module() << '\n'; cout << myfloats.module() << '\n'; return 0; }

25 0

As you can see in the code the specialization is defined this way:

template <> class class_name <type>

The specialization is part of a template, for that reason we must begin the declaration with template <>. And indeed because it is a specialization for a concrete type the generic type cannot be used in it, as well as the first angle-brackets <> must appear empty. After the class name we must include the type that is being specialized enclosed between angle-brackets <>.

When we specialize a type of a template we must also define all the members adequating them to the specialization (if one pays attention, in the example above we have had to include its own constructor, although it is identic to the one in the generic template). The reason is that no member is "inherited" from the generic template to the specialized one.

Page 120: Object Oriented Programing using C++

Object Oriented Programming using C++

120

Parameter values for templates

Besides the template arguments preceded by class or typename keyword that represent a type, functions templates and class templates can include other parameters that are not types whenever they are also constant values, like for example values of fundamental types. As an example see this class template that serves to store arrays:

// array template #include <iostream.h> template <class T, int N> class array { T memblock [N]; public: setmember (int x, T value); T getmember (int x); }; template <class T, int N> array<T,N>::setmember (int x, T value) { memblock[x]=value; } template <class T, int N> T array<T,N>::getmember (int x) { return memblock[x]; } int main () { array <int,5> myints; array <float,5> myfloats; myints.setmember (0,100); myfloats.setmember (3.0,3.1416); cout << myints.getmember(0) << '\n'; cout << myfloats.getmember(3) << '\n'; return 0; }

100 3.1416

It is also possible to set default values for any template parameter just as that is done with function parameters.

Some possible template examples seen above:

template <class T> // The most usual: one class parameter. template <class T, class U> // Two class parameters. template <class T, int N> // A class and an integer. template <class T = char> // With a default value. template <int Tfunc (int)> // A function as parameter.

Templates and multiple-file projects

From the point of view of the compiler, templates are not normal function or classes. They are compiled on demand. Meaning that the code of a template function is not compiled until an instantiation is required. At that moment, when an instantiation is required, the compiler generates from the template a function specifically for that type.

When projects grow it is usual to split the code of a program in different source files. In these cases, generally the interface and implementation are separated. Taking as example a library of functions, the interface generally consists on the prototypes of all the functions that can be called,

Page 121: Object Oriented Programing using C++

Object Oriented Programming using C++

121

these are generally declared in a "header file" with .h extension, and the implementation (the definition of these functions) is in an independent file of c++ code.

The macro-like functionality of templates, forces us to a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as the declaration. That means we cannot separate the interface in a separate header file and we must include both interface and implementation in any file that uses the templates.

Going back to the library of functions, if we wanted to make a library of function templates, instead of creating a header file (.h) we should create a "template file" with both the interface and implementation of the function templates (there is no convention on the extension for these type of file other that no extension at all or to keep the .h). The inclusion more than once of the same template file with both declarations and definitions in a project doesn't generate linkage errors, since they are compiled on demand and compilers that allow templates should be prepared to not generate duplicate code in these cases.

Namespaces Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

The form to use namespaces is:

namespace identifier { namespace-body }

Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:

namespace general { int a, b; }

In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:

general::a general::b

The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error. For example:

// namespaces #include <iostream.h> namespace first { int var = 5; } namespace second { double var = 3.1416;

5 3.1416

Page 122: Object Oriented Programing using C++

Object Oriented Programming using C++

122

} int main () { cout << first::var << endl; cout << second::var << endl; return 0; }

In this case two global variables with the var name exist, one defined within namespace first and another one in second. No redefinition errors thanks to namespaces.

using namespace

The using directive followed by namespace serves to associate the present nesting level with a certain namespace so that the objects and functions of that namespace can be accesible directly as if they were defined in the global scope. Its utilization follows this prototype:

using namespace identifier;

Thus, for example:

// using namespace example #include <iostream.h> namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { using namespace second; cout << var << endl; cout << (var*2) << endl; return 0; }

3.1416 6.2832

In this case we have been able to use var without having to precede it with any scope operator.

You have to consider that the sentence using namespace has validity only in the block in which it is declared (understanding as a block the group of instructions within key brackets {}) or in all the code if it is used in the global scope. Thus, for example, if we had intention to first use the objects of a namespace and then those of another one we could do something similar to:

// using namespace example #include <iostream.h> namespace first { int var = 5; } namespace second { double var = 3.1416;

5 3.1416

Page 123: Object Oriented Programing using C++

Object Oriented Programming using C++

123

} int main () { { using namespace first; cout << var << endl; } { using namespace second; cout << var << endl; } return 0; }

alias definition

We have the possibility to define alternative names for namespaces that already exist. The form to do it is:

namespace new_name = current_name ;

Namespace std

One of the best examples that we can find about namespaces is the standard C++ library itself. According to ANSI C++ standard, the definition all the classes, objects and functions of the standard C++ library are defined within namespace std.

You may have noticed that we have ignored this rule all along this tutorial. I've decided to do so since this rule is almost as recent as the ANSI standard itself (1997) and many older compilers do not comply with this rule.

Almost all compilers, even those complying with ANSI standard, allow the use of the traditional header files (like iostream.h, stdlib.h, etc), the ones we have used througout this tutorial. Nevertheless, the ANSI standard has completely redesigned this libraries taking advantage of the templates feature and following the rule to declare all the functions and variables under the namespace std.

The standard has specified new names for these "header" files, basically using the same name for C++ specific files, but without the ending .h. For example, iostream.h becomes iostream.

If we use the ANSI-C++ compliant include files we have to bear in mind that all the functions, classes and objects will be declared under the std namespace. For example:

// ANSI-C++ compliant hello world #include <iostream> int main () { std::cout << "Hello world in ANSI-C++\n"; return 0; }

Hello world in ANSI-C++

Although it is more usual to use using namespace and save us to have to use the scope operator :: before all the references to standard objects:

Page 124: Object Oriented Programing using C++

Object Oriented Programming using C++

124

// ANSI-C++ compliant hello world (II) #include <iostream> using namespace std; int main () { cout << "Hello world in ANSI-C++\n"; return 0; }

Hello world in ANSI-C++

The name for the C files has also suffered some changes. The use of the ANSI-compliant way to include the standard libraries, apart for the ANSI-compliance itself, is highly recommended for STL users.

Exception handling During the development of a program, it may be some cases where we do not have the certainty that a piece of the code is going to work right, either because it accesses to resources that do not exist or because it gets out of an expected range, etc...

This type of anomalous situations are included in what we consider exceptions and C++ has recently incorporated three new operators to help us to handle these situations: try, throw and catch.

Its form of use is the following:

try { // code to be tried throw exception; } catch (type exception) { // code to be executed in case of exception }

And its operation: - The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type. - If an exception has taken place, that is to say, if it has been executed a throw instruction within the try block, the catch block is executed receiving as parameter the exception passed by throw.

For example:

// exceptions #include <iostream.h> int main () { char myarray[10]; try { for (int n=0; n<=10; n++) { if (n>9) throw "Out of range"; myarray[n]='z'; } } catch (char * str)

Exception: Out of range

Page 125: Object Oriented Programing using C++

Object Oriented Programming using C++

125

{ cout << "Exception: " << str << endl; } return 0; }

In this example, if within the n loop, n gets to be more than 9 an exception is thrown, since myarray[n] would in that case point to a non-trustworthy memory address. When throw is executed, the try block finalizes right away and every object created within the try block is destroyed. After that, the control is passed to the corresponding catch block (that is only executed in these cases). Finally the program continues right after the catch block, in this case: return 0;.

The syntax used by throw is similar to the one of return: Only one parameter that is not needed to be enclosed between parenthesis.

The catch block must go right after the try block without including any code line between them. The parameter that catch accepts can be of any valid type. Even more, catch can be overloaded so that it can accept different types as parameters. In that case the catch block executed is the one that matches with the type of the exception sent (the parameter of throw):

// exceptions: multiple catch blocks #include <iostream.h> int main () { try { char * mystring; mystring = new char [10]; if (mystring == NULL) throw "Allocation failure"; for (int n=0; n<=100; n++) { if (n>9) throw n; mystring[n]='z'; } } catch (int i) { cout << "Exception: "; cout << "index " << i << " is out of range" << endl; } catch (char * str) { cout << "Exception: " << str << endl; } return 0; }

Exception: index 10 is out of range

In this case there is a possibility that happen, at least, two different exceptions:

1. That the required block of 10 characters cannot be assigned (something rare, but possible): in this case an exception is thrown that will be caught by catch (to char * str).

2. That the maximum index for mystring is exceeded: in this case the exception thrown will be caught by catch (int i), since parameter is an integer number.

We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch:

Page 126: Object Oriented Programing using C++

Object Oriented Programming using C++

126

try { // code here } catch (...) { cout << "Exception occurred"; }

It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception received to the external level, for that it is used the expression throw; with no arguments. For example:

try { try { // code here } catch (int n) { throw; } } catch (...) { cout << "Exception occurred"; }

Exceptions not caught

If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called.

This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is:

void terminate();

Standard exceptions

Some functions of the standard C++ language library send exceptions that can be captured if we include them within a try block. These exceptions are sent with a class derived from std::exception as type. This class (std::exception) is defined in the C++ standard header file <exception> and serves as pattern for the standard hierarchy of exceptions:

exception bad_alloc (thrown by new) bad_cast (thrown by dynamic_cast when fails with a referenced type) bad_exception (thrown when an exception doesn't match any catch) bad_typeid (thrown by typeid) logic_error

domain_error invalid_argument length_error out_of_range

runtime_error overflow_error range_error underflow_error

ios_base::failure (thrown by ios::clear) Because this is a class hierarchy, if you include a catch block to capture any of the exceptions of this hierarchy using the argument by reference (i.e. adding an ampersand & after the type) you will also capture all the derived ones (rules of inheritance in C++).

Page 127: Object Oriented Programing using C++

Object Oriented Programming using C++

127

The following example catches an exception of type bad_typeid (derived from exception) that is generated when requesting information about the type pointed by a null pointer:

// standard exceptions #include <iostream.h> #include <exception> #include <typeinfo> class A {virtual f() {}; }; int main () { try { A * a = NULL; typeid (*a); } catch (std::exception& e) { cout << "Exception: " << e.what(); } return 0; }

Exception: Attempted typeid of NULL pointer

You can use the classes of standard hierarchy of exceptions to throw your exceptions or derive new classes from them.

Advanced Class Type-casting Until now, in order to type-cast a simple object to another we have used the traditional type casting operator. For example, to cast a floating point number of type double to an integer of type int we have used:

int i; double d; i = (int) d;

or also

i = int (d);

This is quite good for basic types that have standard defined conversions, however this operators can also been indiscriminately applied on classes and pointers to classes. So, it is perfectly valid to write things like:

// class type-casting #include <iostream.h> class CDummy { int i; }; class CAddition { int x,y; public: CAddition (int a, int b) { x=a; y=b; } int result() { return x+y;} }; int main () {

Page 128: Object Oriented Programing using C++

Object Oriented Programming using C++

128

CDummy d; CAddition * padd; padd = (CAddition*) &d; cout << padd->result(); return 0; }

Although the previous program in sintactically correct in C++ (in fact it will compile with no warnings on most compilers) it is a code with not much sense since we use function result, that is a member of CAddition, without having been declared any object of that class: padd is not an object, it is only a pointer which we have assigned the address of a non related object. When accessing its result member it will produce a run-time error or, at best, just an unexpected result.

In order to control these types of conversions between classes, ANSI-C++ standard has defined four new casting operators: reinterpret_cast, static_cast, dynamic_cast and const_cast. All of them have the same format when used:

reinterpret_cast <new_type> (expression) dynamic_cast <new_type> (expression) static_cast <new_type> (expression) const_cast <new_type> (expression)

Where new_type is the destination casting type that expression has to be casted to. To make an easily understandable parallelism with traditional type-casting operators these expression mean:

(new_type) expression new_type (expression)

but with their own special characteristics.

reinterpret_cast

reinterpret_cast casts a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vice versa.

This operator can cast pointers between non-related classed. The operation results is a simple binary copy of the value from a pointer to the other. The content pointed does not pass any kind of check nor transformation between types.

In the case that the copy is performed from a pointer to an integer, the interpretation of its content is system dependent and therefore any implementation is non portable. A pointer casted to an integer enough large to fully contain it can be casted back to a valid pointer.

class A {}; class B {}; A * a = new A; B * b = reinterpret_cast<B*>(a);

reinterpret_cast treats all pointers exactly as traditional type-casting operators do.

static_cast

static_cast allows to perform any casting that can be implicitly performed as well as also the inverse cast (even if this is not allowed implicitly).

Page 129: Object Oriented Programing using C++

Object Oriented Programming using C++

129

Applied to pointers to classes, that is to say that it allows to cast a pointer of a derived class to its base class (this is a valid conversion that can be implicitly performed) and can also perform the inverse: cast a base class to its derivated class.

In this last case the base class that is being casted is not checked to determine wether this is a complete class of the destination type or not.

class Base {}; class Derived: public Base {}; Base * a = new Base; Derived * b = static_cast<Derived*>(a);

static_cast, aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:

double d=3.14159265; int i = static_cast<int>(d);

dynamic_cast

dynamic_cast is exclusively used with pointers and references to objects. It allows any type-casting that can be implicitly performed as well as the inverse one when used with polymorphic classes, however, unlike static_cast, dynamic_cast checks, in this last case, if the operation is valid. That is to say, it checks if the casting is going to return a valid complete object of the requested type.

Checking is performed during run-time execution. If the pointer being casted is not a pointer to a valid complete object of the requested type, the value returned is a NULL pointer.

class Base { virtual dummy(){}; }; class Derived : public Base { }; Base* b1 = new Derived; Base* b2 = new Base; Derived* d1 = dynamic_cast<Derived*>(b1); // succeeds Derived* d2 = dynamic_cast<Derived*>(b2); // fails: returns NULL

If the type-casting is performed to a reference type and this casting is not possible an exception of type bad_cast is thrown:

class Base { virtual dummy(){}; }; class Derived : public Base { }; Base* b1 = new Derived; Base* b2 = new Base; Derived d1 = dynamic_cast<Derived&*>(b1); // succeeds Derived d2 = dynamic_cast<Derived&*>(b2); // fails: exception thrown

const_cast

This type of casting manipulates the const attribute of the passed object, either to be set or removed:

class C {}; const C * a = new C; C * b = const_cast<C*> (a);

Page 130: Object Oriented Programing using C++

Object Oriented Programming using C++

130

Neither of the other three new cast operators can modify the constness of an object.

typeid

ANSI-C++ also defines a new operator called typeid that allows to check the type of an expression:

typeid (expression)

this operator returns a refernece to a constant object of type type_info that is defined in standard header file <typeinfo>. This returned value can be compared with another using operators == and != or can serve to obtain a string of characters representing the data type or class name by using its name() method.

// typeid, typeinfo #include <iostream.h> #include <typeinfo> class CDummy { }; int main () { CDummy* a,b; if (typeid(a) != typeid(b)) { cout << "a and b are of different types:\n"; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; } return 0; }

a and b are of different types: a is: class CDummy * b is: class

Preprocessor directives Preprocessor directives are orders that we include within the code of our programs that are not instructions for the program itself but for the preprocessor. The preprocessor is executed automatically by the compiler when we compile a program in C++ and is the one in charge to make the first verifications and digestions of the program's code.

All these directives must be specified in a single line of code and they do not have to include an ending semicolon ;.

#define

At the beginning of this tutorial we already spoken about a preprocessor directive: #define, that serves to generate what we called defined constantants or macros and whose form is the following:

#define name value

Its function is to define a macro called name that whenever it is found in some point of the code is replaced by value. For example:

Page 131: Object Oriented Programing using C++

Object Oriented Programming using C++

131

#defines MAX_WIDTH 100 char str1[MAX_WIDTH]; char str2[MAX_WIDTH];

It defines two strings to store up to 100 characters.

#define can also be used to generate macro functions:

#define getmax(a,b) a>b?a:b int x=5, y; y = getmax(x,2);

after the execution of this code y would contain 5.

#undef

#undef fulfills the inverse functionality than #define. What it does is to eliminate from the list of defined constants the one that has the name passed as parameter to #undef:

#define MAX_WIDTH 100 char str1[MAX_WIDTH]; #undef MAX_WIDTH #define MAX_WIDTH 200 char str2[MAX_WIDTH];

#ifdef, #ifndef, #if, #endif, #else and #elif

These directives allow to discard part of the code of a program if a certain condition is not fulfilled.

#ifdef allows that a section of a program is compiled only if the defined constant that is specified as parameter has been defined, independently of its value. Its operation is:

#ifdef name // code here #endif

For example:

#ifdef MAX_WIDTH char str[MAX_WIDTH]; #endif

In this case, the line char str[MAX_WIDTH]; is only considered by the compiler if the defined constant MAX_WIDTH has been previously defined, independently of its value. If it has not been defined, that line will not be included in the program.

#ifndef serves for the opposite: the code between the #ifndef directive and the #endif directive is only compiled if the constant name that is specified has not been defined previously. For example:

#ifndef MAX_WIDTH #define MAX_WIDTH 100 #endif char str[MAX_WIDTH];

Page 132: Object Oriented Programing using C++

Object Oriented Programming using C++

132

In this case, if when arriving to this piece of code the defined constant MAX_WIDTH has not yet been defined it would be defined with a value of 100. If it already existed it would maintain the value that it had (because the #define statement won't be executed).

The #if, #else and #elif (elif = else if) directives serve for that the portion of code that follows is compiled only if the specified condition is met. The condition can only serve to evaluate constant expressions. For example:

#if MAX_WIDTH>200 #undef MAX_WIDTH #define MAX_WIDTH 200 #elsif MAX_WIDTH<50 #undef MAX_WIDTH #defines MAX_WIDTH 50 #else #undef MAX_WIDTH #defines MAX_WIDTH 100 #endif char str[MAX_WIDTH];

Notice how the structure of chained directives #if, #elsif and #else finishes with #endif.

#line

When we compile a program and there happens any errors during the compiling process, the compiler shows the error that have happened preceded by the name of the file and the line within the file where it has taken place.

The #line directive allows us to control both things, the line numbers within the code files as well as the file name that we want that it appears when an error takes place. Its form is the following one:

#line number "filename"

Where number is the new line number that will be assigned to the next code line. The line number of successive lines will be increased one by one from this.

filename is an optional parameter that serves to replace the file name that will be shown in case of error from this directive until other one changes it again or the end of the file is reached. For example:

#line 1 "assigning variable" int a?;

This code will generate an error that will be shown as error in file "assigning variable", line 1.

#error

This directive aborts the compilation process when it is found returning the error that is specified as parameter:

Page 133: Object Oriented Programing using C++

Object Oriented Programming using C++

133

#ifndef __cplusplus #error A C++ compiler is required #endif

This example aborts the compilation process if the defined constant __cplusplus is not defined.

#include

This directive has also been used assiduously in other sections of this tutorial. When the preprocessor finds an #include directive it replaces it by the whole content of the specified file. There are two ways to specify a file to be included:

#include "file" #include <file>

The only difference between both expressions is in the directories in which the compiler is going to look for the file. In the first case in that the file is specified between quotes the file is looked for from the same directory in which the file that includes the directive is, and only in case that it is not there the compiler looks for in the default directories where it is configured to look for the standard header files.

In case that the file name is included enclosed between angle-brackets <> the file is directly looked for in the default directories where the compiler is configured to look for the standard header files.

#pragma

This directive is used to specify diverse options to the compiler. These options are specific for the platform and the compiler you use. Consult the manual or the reference of your compiler for more information on the possible parameters that you can define with #pragma.

Input/Output with files As an extension to the standard stream ( FILE) approach well known from the C programming language, C++ offers an I/O library based on class concepts.

Earlier we've already seen examples of the use of the C++ I/O library, especially the use of the insertion operator (<<) and the extraction operator (>>). In this chapter we'll cover the library in more detail.

The discussion of input and output facilities provided by the C++ programming language heavily uses the class concept, and the notion of member functions. we think it is well possible to introduce input and output (I/O) facilities well before discussing the technical background of these topics.

The use of the C++ I/O library offers the additional advantage of type safety in all kinds of standard situations. Objects (or plain values) are inserted into streams. Compare this to the situation commonly encountered in C where the fprintf() function is used to indicate by a format string what kind of value to expect where. Compared to this latter situation C++'s iostream approach immediately uses the objects where their values should appear, as in

Page 134: Object Oriented Programing using C++

Object Oriented Programming using C++

134

cout << "There were " << nMaidens << " virgins present\n";

The compiler notices the type of the nMaidens variable, inserting its proper value at the appropriate place in the sentence inserted into the cout iostream.

Compare this to the situation encountered in C. Although C compilers are getting smarter and smarter over the years, and although a well-designed C compiler may warn you for a mismatch between a format specifier and the type of a variable encountered in the corresponding position of the argument list of a printf() statement, it can't do much more than warn you. The type safety seen in C++ prevents you from making type mismatches, as there are no types to match.

Apart from this, the iostreams offer more or less the same set of possibilities as the standard streams of C: files can be opened, closed, positioned, read, written, etc..

figure 3: Central I/O Classes

This chapter is organized as follows see also figure figure 3:

� First, the ios class is introduced. The ios class forms the foundation of all I/O operations, and defines, among other things, the facilities for inspecting the state of I/O streams and output formatting. Every class of the I/O library doing input or output is derived from this ios class, and inherits its capabilities. This means that a member function like good(), which is defined in the ios

Page 135: Object Oriented Programing using C++

Object Oriented Programming using C++

135

class, is also available in classes that were derived from ios, like istream and ofstream. The reader is urged to keep this feature in mind while reading this chapter. The concept of inheritance is not discussed further here, but rather in chapter 13. � Next, basic C++ output facilities are discussed. The basic class used for output is ostream, defining the insertion operator as well as other facilities for writing information to streams. Apart from inserting information in files it is possible to insert information in memory buffers, for which the ostringstream class is available. Formatting of the output is to a great extent possible using the facilities defined in the ios class, but it is also possible to insert formatting commands directly in streams, using manipulators. This aspect of C++ output is discussed as well. � Basic C++ input facilities are available in the istream class. This class defines the insertion operator and related facilities for input. Analogous to the ostringstream a class istringstream class is available for extracting information from memory buffers. � Finally, several advanced I/O-related topics are discussed: Among other topics, combined reading and writing using streams; C and C++ I/O> mixing C and C++ I/O using stdiobuf ojects; communicating with external processes using procbuf objects and marking positions in streams using streammarker objects are discussed. The basic I/O facilities are declared in the iostream header file: the preprocessor directive #include <iostream> is required to use the ostream and istream classes, e.g., cout and cin.

The foundation: ios

The ios class forms the foundation of all I/O operations, and defines, among other things, the facilities for inspecting the state of I/O streams and output formatting. Every class of the I/O library doing input or output is derived from this ios class, and inherits its capabilities.

The discussion of the class ios precedes the introduction of members that can be used for actual reading from and writing to streams. But as the ios class is the basis on which all I/O in C++ was built, we start with discussing this part of the C++ I/O library.

Note, however, that as in C, I/O in C++ is not part of the language (although it is part of the ANSI/ISO standard on C++): although it is technically possible to ignore all predefined I/O facilities, nobody actually does so, and the I/O library represents therefore a de facto I/O standard in C++.

For the sake of completeness and not so much because it is necessary to understand the ongoing discussion, it is noted here that it is not possible to construct an ios object directly. classes that are derived from ios (like ostream) may construct ios objects indirectly using the ios::ios(streambuf *) constructor.

The ios object internally uses a streambuf in which the state of the stream that is managed by the ios object is maintained. In several situations it is desirable to access the streambuf layer of the ios object. For this, the ios class defines the member streambuf *ios::rdbuf()

Condition states

Operations on streams may succeed and they may fail for several reasons. Whenever an operation fails, further read and write operations on the stream are suspended. It is possible to inspect (and possibly: clear) the condition state of streams, so that a program can repair the problem, instead of having to abort.

Conditions are represented by the following condition flags:

� ios::badbit:

Page 136: Object Oriented Programing using C++

Object Oriented Programming using C++

136

if this flag has been raised an illegal operation has been performed, like an attempt to read beyond end-of-file.

� ios::eofbit:

if this flag has been raised, the ios object has sensed end of file.

� ios::failbit:

if this flag has been raised, an operation has failed, like an attempt to extract an int when no numeric characters are available on input.

� ios::goodbit:

this flag indicates that no failure has been sensed for the ios object. The ios::failbit and ios::goodbit are each other's complements.

Several condition member functions are available to manipulate or determine the states of ios objects:

� ios::bad():

this member function returns a non-zero value when an invalid operation has been requested (i.e., when ios::goodbit has been set).

� ios::eof():

this member function returns a non-zero value when end of file (EOF) has been sensed (i.e., ios::eofbit has been set).

� ios::fail():

this member function returns a non-zero value when ios::eof() or ios::bad() returns a non-zero value.

� ios::good():

this member function returns a non-zero value when ios::fail() returns a zero value and vice versa.

Ios objects can also be interrogated using boolean operators: true is returned in boolean expressions for ios objects if ios::good() would have returned a non-zero value. So, a construction like

cin >> x; // cin is also an ios object if (cin) cout << "Good extraction of `x'\n"; is possible. Also, the complementary test is possible: cin >> x; // cin is also an ios object

Page 137: Object Oriented Programing using C++

Object Oriented Programming using C++

137

if (!cin) cout << "Extraction of `x' failed\n"; Once an error condition has been raised (ios::goodbit has not been set), further processing of the ios object is suspended.

The following members are available for the mangement of error states:

� ios::clear():

When an error condition has occurred, and the condition can be repaired, then clear() can be called to clear the error status of the file.

� ios::rdstate():

This member function returns the current set of flags that are set for an ios object. To test for a particular flag, use the bitwise and operator:

if (iosObject.rdstate() & ios::good) { // state is good }

� ios::setState(int flags):

This member is used to set a particular set of flags. The member ios::clear() is a shortcut to clear all error flags. Of course, clearing the flags doesn't automatically mean the errorcondition has been cleared too. The strategy should be:

� An error condition is detected,

� The error is repaired

� The member ios::clear() is called.

Formatting output and input

The way information is written to ios objects (or, occasionally, read from ios objects) may be controlled by formatting flags.

Formatting may involve the control of the width of an output field or an input buffer or the form (e.g., the radix) in which a value is displayed. Most of the format control is realized in the context of the ios class, although most formatting is actually found with output streams, like the upcoming ostream class. Since the formatting is controlled by flags, which are defined in the ios class, it was considered best to discuss formatting with the ios class itself, rather than with a selected derived class, where the choice of the derived class would always be somewhat arbitrarily.

The flags control the formatting, but the flags can be altered basically in two ways: using specialized member functions or using manipulators, which are directly inserted in streams. Manipulators are not applied directly to the ios class, as they require the use of the insertion operator.

Formatting flags

Page 138: Object Oriented Programing using C++

Object Oriented Programming using C++

138

Most formatting flags are related to outputting information. Information can be written to output streams in basically two ways: binary output will write information directly to the output stream, without conversion to some human-readable format. E.g., an int value is written as a set of four bytes. Alternatively, formatted output will convert the values that are stored in bytes in the computer's memory to ASCII-characters, in order to create a human-readable form.

Formatting flags can be used to define the way this conversion takes place, to control, e.g., the number of characters that are written to the output stream.

The following formatting flags are available

� ios::basefield:

used in combination with a flag setting the radix of integral values to output (ios::dec, ios::hex or ios::oct, see below).

� ios::dec:

to display integral values as decimal (i.e., radix 10) values. This is the default.

� ios::fixed:

to display real values in a fixed notation (e.g., 12.25), as opposed to displaying values in a scientific notation (see below).

� ios::floatfield:

used in combination with a flag setting the way real numbers are displayed ios::fixed or ios::scientific,

� ios::hex:

to display integral values as hexadecimal values (i.e., radix 16) values.

� ios::internal:

to add fill characters (blanks by default) between the minus sign of negative numbers and the value itself.

� ios::left:

to left-adjust (integral) values in fields that are wider than needed to display the values. By default values are right-adjusted (see below).

� ios::oct:

to display integral values as octal values (i.e., radix 8) values.

� ios::right:

to right-adjust (integral) values in fields that are wider than needed to display the values. This is the default adjustment.

Page 139: Object Oriented Programing using C++

Object Oriented Programming using C++

139

� ios::scientific:

to display real values in scientific notation (e.g., 1.24e+03).

� ios::showbase:

to display the numeric base of integral values. With hexadecimal values the 0x prefix is used, with octal values the prefix 0. For the (default) decimal value no particular prefix is used.

� ios::showpoint:

display a trailing decimal point and trailing decimal zeros when real numbers are displayed. When this flag is set, an insertion like:

cout << 16.0 << ", " << 16.1 << ", " << 16 << endl;

could result in:

16.0000, 16.1000, 16

Note that the last 16 is an integral rather than a real number, and is not given a decimal point: ios::showpoint has no effect here. If ios::showpoint is not used, then trailing zeros are discarded. If the decimal part is zero, then the decimal point is discarded as well.

� ios::showpos:

display a + character with positive values.

� ios::skipws:

used for extracting information from streams. When this flag is is (which is the default) leading white space characters (blanks, tabs, newlines, etc.) are skipped when a value is extracted from a stream. If the flag is not set, leading white space characters are not skipped.

� ios::stdio:

flush the standard C streams stdout and stderr after each output operation.

� ios::unitbuf:

flush the stream after each output operation.

� ios::uppercase:

use capital letters in the representation of (hexadecimal or scientifically formatted) values.

Format modifying member functions

Page 140: Object Oriented Programing using C++

Object Oriented Programming using C++

140

Several member functions are available for I/O formatting. They are:

� ios::fill() const:

returns (as char) the current padding character. By default, this is the blank space.

� ios::fill(char padding):

redefines the padding character. Returns (as char) the previous padding character.

� ios::flags() const:

returns the current collection of flags controlling the format state of the stream for which the member function is called. To inspect a particular flag, use the binairy and operator, e.g.,

if (cout.flags() & ios::hex) { // hexadecimal output of integral values }

� ios::flags(fmtflags flagset):

returns the previous set of flags, and defines the current set of flags as flagset, defined by a combination of formatting flags, combined by the binary or operator.

� ios::precision() const:

returns (as int) the number of significant digits used for outputting real values (default: 6).

� ios::precision(int signif):

redefines the number of significant digits used for outputting real values, returns (as int) the previously used number of significant digits.

� ios::setf(fmtflags flag):

returns the previous set of all flags, and sets one formatting flag (leaving the remaining flags unaltered).

� ios::setf(fmtflags flag, fmtflags fieldtype):

returns the previous set of all flags, and sets the way values are represented:

� setf(ios::hex, ios::basefield) is used to activate the hexadecimal representation of integral values (alternatively, ios::dec and ios::oct can be used). � setf(ios::fixed, ios::floatfield) is used to activate the fixed value representation of real values (alternatively, ios::scientific can be used). � ios::unsetf(fmtflags flag):

Page 141: Object Oriented Programing using C++

Object Oriented Programming using C++

141

returns the previous set of all flags, and clears one formatting flag (leaving the remaining flags unaltered). The unsetting of a default flag (e.g., cout.unsetf(ios::dec)) has no effect.

� ios::width() const:

returns (as int) the current output field width (the number of characters to write for numerical values on the next insertion operation). Default: 0, meaning `as many characters as needed to write the value'.

� ios::width(int nchars):

returns (as int) the previously used output field width, redefines the value to nchars for the next insertion operation. Note that the field width is reset to 0 after every insertion operation, and that width() currently has no effect on text-values like char * or string values.

Output

Output in C++ evolves around the ostream class. The ostream class defines the basic operators and members for inserting information into streams: the insertion operator ( operator<<()), and special members like ostream::write() for writing unformatted information from streams.

From the class ostream several other classes are derived, all having the functionality of the ostream class, and adding their own specialties. In the next sections we will introduce:

� The class ostream, offering the basic facilities for doing output; � The class ifstream, allowing us to open files for writing (comparable to C's fopen(filename, "w")); � The class ostringstream, allowing us to write informatioon to memory rather than to files (streams) (comparable to C's sprintf() function). � Manipulators, allowing us, among other things, to format output and to change the radix used for displaying integral numbers.

Basic output: the class `ostream'

The class ostream is the class defining basic output facilities. The cout, clog and cerr objects are all ostream objects. Note that all facilities defined in the ios class, as far as output is concerned, is available in the ostream class as well, due to the inheritance mechanism

Ostream objects can be constructed by the following ostream constructors:

� ostream object:

this constructor basically creates a new ios object. It is seldomly used.

� ostream object(streambuf *sb):

this constructor can be used to construct a wrapper around an existing open stream. The wrapper can be used, e.g., to write to an external process

In order to use the ostream class in C++ sources, the #include <iostream> preprocessor directive must be given.

Page 142: Object Oriented Programing using C++

Object Oriented Programming using C++

142

Writing to `ostream' objects

The ostream class supports both formatted and binary output.

The insertion operator ( <<) may be used to insert values in a type safe way into ostream objects. This is called formatted output, as binary values which are stored in the computer's memory are converted to human-readable ASCII characters according to certain formatting rules.

Note that the insertion operator points to the ostream object wherein the information must be inserted. The normal associativity of the <<-operator remains unaltered, so when a statement like

cout << "hello " << "world";

is encountered, the leftmost two operands are evaluated first (cout << "hello "), and an ostream & object, which is actually the same cout object, is returned. Now, the statement is reduced to

cout << "world"

and the second string is inserted into cout.

The << operator has a lot of (overloaded) variants, so many types of variables can be inserted into ostream objects. There is an overloaded <<-operator expecting an int, a double, a pointer, etc. etc.. For every part of the information that is inserted into the stream the operator returns the ostream object into which the information so far was inserted, and the next part of the information to be inserted is devoured.

As an extension to the ANSI/ISO standard, the Gnu compiler provides the following additional member functions for formatted output:

� : ostream& ostream::form(char const *format, ...):

This member function returns a reference to an ostream object. Therefore, it can be used in combination with, e.g., the insertion operator:

cout.form("Hello %s", "world") << endl;

The member function form() is the analogue of C's printf() and fprintf() functions.

� ostream& ostream::vform(char const *format, va_list list):

When variadic functions are constructed in which information must be inserted into a stream, the member function vform() can be used, being the analogue of C's vprintf() and vfprintf() functions.

When binary files must be written, the information should normally not be formatted: an int value should be written as a series of unaltered bytes, not as a series of ASCII numeric characters 0 to 9. The following member functions are defined for ostream objects: � ostream& ostream::put(char c):

This member function

writes a single character to the output stream. Since a character is a byte, this member function could also be used for writing a single character to a text-file.

Page 143: Object Oriented Programing using C++

Object Oriented Programming using C++

143

� ostream& ostream::write(void const *buffer, int length):

This member function writes at most len bytes, stored in the void const *buffer to the ostream object. The bytes are written as they are stored in the buffer, no formatting is done whatsoever.

`ostream' positioning

Although not every ostream object supports repositioning, some do. This means that it is possible to rewrite a section of the stream which was written earlier. Repositioning is frequently used in database applications where it must be possible to access the information in the database randomly.

The following members are available:

� streampos ostream::tellp():

this function returns the current position where the next write-operation to the stream will take place. For all practical purposes a streampos can be considered to be an unsigned long.

� ostream &ostream::seekp(streamoff step, ios::seekdir org):

This member function can be used to reposition the stream. The function expects a streamoff step, the stepsize in bytes to go from org. For all practical purposes a streampos can be considered to be a long. The origin of the step, org is a ios::seekdir value. Possible values are:

� ios::beg:

org is interpreted as the stepsize relative to the beginning of the stream. If org is not specified, ios::beg is used.

� ios::cur:

org is interpreted as the stepsize relative to the current position (as returned by tellp() of the stream).

� ios::end:

org is interpreted as the stepsize relative to the current end position of the the stream.

It is allowed to seek beyond end of file . Writing bytes to a location beyond EOF will pad the intermediate bytes with ASCII-Z values: null-bytes. It is not allowed to seek before begin of file. Seeking before ios::beg will cause the ios::fail flag to be set.

`ostream' flushing

Page 144: Object Oriented Programing using C++

Object Oriented Programming using C++

144

Unless the ios::unitbuf flag has been set, information written to an ostream object is not immediately written to the physical stream. Rather, an internal buffer is filled up during the write-operations, and when full it is flushed.

The internal buffer can be flushed under program control:

� ostream& ostream::flush():

this member function writes any buffered information to the ostream object. The call to flush() is implied when:

� The ostream object ceases to exist,

� The endl or flush manipulators are inserted into the ostream object,

� A stream derived from ostream is closed.

Output to files: the class `ofstream'

The ofstream class is derived from the ostream class: it has the same capabilities as the ostream class, but can be used to access files or create files for writing.

In order to use the ofstream class in C++ sources, the preprocessor directive #include <fstream> must be given. If this directive is used, it is not necessary anymore to include the header file iostream.

The following constructors are available for ofstream objects:

� ofstream object:

This is the basic constructor. It creates an ofstream object which may be associated with an actual file later, using the open() member (see below).

� ofstream object(char const *name, int mode, int perm):

This constructor can be used to associate an ofstream object with the file named name, using output mode mode, and permission bits perm.

The output mode is by default ios::out.

The file permission bits are the standard Unix permission values. The default value is 0644, meaning owner: read/write access, group members and others: read-only access.

In the following example an ofstream object, associated with the newly created file /tmp/scratch, is constructed:

ofstream out("/tmp/scratch");

� ofstream object(int fd):

Page 145: Object Oriented Programing using C++

Object Oriented Programming using C++

145

construct an ofstream wrapper around an existing open file whose file descriptor is known. Using the well-known file descriptor for the standard output stream, STDOUT_FILENO, as defined in, e.g., the unistd.h header file.

Instead of directly associating an ofstream object with a file, the object can be constructed first, and opened later.

� tt(void ofstream::open(char const *name, int mode, int perm)): quote( Having constructed an tt(ofstream) object, the member function tt(open()) can be used to associate the tt(ofstream) object with an actual file. This member function can only be used with a file whose name is known. It is not possible to open a tt(ofstream) object with the tt(open()) member function when a file descriptor is available.) � ofstream::close():

Conversely, it is possible to close an ofstream object explicitly using the close() member function. The function sets the ios::fail flag of the closed object. Closing the file will flush any buffered information to the associated file. A file is automatically closed when the associated ofstream object ceases to exist.

A subtlety is the following: Assume a stream is constructed, but it is not actually attached to a file. E.g., the statement ofstream ostr was executed. When we now check its status through good(), a non-zero (i.e., ok) value will be returned. The `good' status here indicates that the stream object has been properly constructed. It doesn't mean the file is also open. To test whether a stream is actually open, In order to write information to memory, using the stream facilities, ostringstream objects can be used. These objects are derived from ostream objects. The following constructors and members are available: � ostringstream ostr:

The constructor will construct an empty ostringstream object. Any information inserted into the object is stored in dynamically allocated memory which is deleted when the ostringstream object goes out of scope.

� string ostringstream::str() const:

This member function will return the string that is stored inside the ostringstream object.

Before the stringstream class was available the class ostrstream was commonly used for doing output to memory. This latter class suffered from the fact that, once its contents were retrieved using its str() member function, these contents were `frozen', meaning that its dynamically allocated memory was not released when the object went out of scope. Although this situation could be prevented (using the ostrstream member call freeze(0)), this implementation could easily lead to memory leaks. The stringstream class does not suffer from these risks. Therefore, the use of the class ostrstream is now deprecated in favor of ostringstream.

The following example illustrates the use of the ostringstream class: several values are inserted into the object. Then, the stored text is stored in a string, whose length and contents are thereupon printed. Ostringstream objects are most often used for doing `type to string' conversions, like converting int to string. Formatting commands can be used with stringstreams as well, as they are available in ostream objects.

#include <string> #include <sstream> int main() {

Page 146: Object Oriented Programing using C++

Object Oriented Programming using C++

146

ostringstream ostr; ostr.setf(ios::showbase); ostr.setf(ios::hex, ios::basefield); ostr << 12345; cout << ostr.str() << endl; ostr << " -- "; ostr.unsetf(ios::hex); ostr << 12; cout << ostr.str() << endl; /* Output from this program: 0x3039 0x3039 -- 12 */ }

Manipulators

Iostream objects define a set of format flags that are used for determining the way values are inserted. The format flags can be controlled by member functions, but also by manipulators. Manipulators are inserted into the stream, instead of being activated through the member selection operator (`.').

New manipulators can be constructed as well. In this section the manipulators that are available in the default implementation of the ostream class are discussed.

� dec, hex, oct:

These manipulators enforce the display of integral numbers in, respectively, decimal, hexadecimal and octal format. The default conversion is decimal. The conversion is applied to values inserted into the stream after processing the manipulators. For example,

cout << 16 << ", " << hex << 16 << ", " << oct << 16; // produce the output: 16, 10, 20

� setbase(int b):

This manipulator can be used to display integral values using the base 8, 10 or 16. It can be used as an alternative to oct, dec, hex in situations where the base of integral values is parameterized.

� setfill(int ch):

This manipulator can be used when the header file iomanip.h is included. It can be used when the header file iomanip.h is included. It defines the filling character in situations where the values of numbers are too small to fill the width that is used to display these values. By default the blank space is used.

� setprecision(int width):

Page 147: Object Oriented Programing using C++

Object Oriented Programming using C++

147

This manipulator can be used when the header file iomanip.h is included. It will set the precision in which a float or double is displayed.

� setw(int width):

This manipulator can be used when the header file iomanip.h is included. It expects as its argument the width of the field that is inserted or extracted next. It can be used as manipulator for insertion, where it defines the maximum number of characters that are displayed for the field, but it can also be used during extraction, where it defines the maximum number of characters that are inserted into an array of characters. To prevent array bounds overflow when extracting from cin, setw() can be used as well:

cin >> setw(sizeof(array)) >> array;

A nice feature is that a long string appearing at cin is split into substrings of at most sizeof(array) - 1 characters, and that an ASCII-Z character is automatically appended. Notes:

� setw() is valid only for the next field. It does not act like e.g., hex which changes the general state of the output stream for displaying numbers.

� When setw(sizeof(someArray)) is used, make sure that someArray really is an array, and not a pointer to an array: the size of a pointer, being, e.g., four bytes, is usually not the size of the array that it points to....

� For reasons unknown (to me), setw() doesn't work when a string is inserted into an ostream. If a string must be inserted using a particular field width, the string::c_str() member can be used.

Input

Input in C++ evolves around the istream class. The istream class defines the basic operators and members for extracting information from streams: the extration operator ( operator>>()), and special members like istream::read() for reading unformatted information from streams.

From the class istream several other classes are derived, all having the functionality of the istream class, and adding their own specialties. In the next sections we will introduce:

� The class istream, offering the basic facilities for doing input; � The class ifstream, allowing us to open files for reading (comparable to C's fopen(filename, "r")); � The class istringstream, allowing us to read informatioon from text that is not stored on files (streams) but in memory (comparable to C's sscanf() function).

Basic input: the class `istream'

The class istream is the class defining basic output facilities. The cin object is an istream object. Note that all facilities defined in the ios class, as far as input is concerned, is available in the istream class as well, due to the inheritance mechanism

Istream objects can be constructed by the following istream constructors:

Page 148: Object Oriented Programing using C++

Object Oriented Programming using C++

148

� istream object:

this constructor basically creates a new ios object. It is seldomly used.

� istream object(streambuf *sb):

this constructor can be used to construct a wrapper around an existing open stream. The wrapper can be used, e.g., to read from an external process .

In order to use the istream class in C++ sources, the #include <iostream> preprocessor directive must be given.

Reading from `istream' objects

The istream class supports both formatted and unformatted binary input.

The extraction operator ( >>) may be used to extract values in a type safe way from istream objects. This is called formatted input, whereby human-readable ASCII characters are converted, according to certain formatting rules, to binary values which are stored in the computer's memory.

Note that the extraction operator points to the objects or variables which must receive new values. The normal associativity of the >>-operator remains unaltered, so when a statement like

cin >> x >> y;

is encountered, the leftmost two operands are evaluated first (cin >> x), and an istream & object, which is actually the same cin object, is returned. Now, the statement is reduced to

cin >> y

and the y variable is extracted from cin.

The >> operator has a lot of (overloaded) variants, so many types of variables can be extracted from istream objects. There is an overloaded >>-operator available for the extraction of an int, a double, a string, an array of characters, possibly to a pointer, etc. etc.. String or character array extraction will (by default) skip all white space characters, and will then extract all consecutive non-white space characters. After processing an extraction operator, the istream object into which the information so far was inserted is returned, which will thereupon be used as the lvalue for the remaining part of the statement.

As an extension to the ANSI/ISO standard, the Gnu compiler provides the following additional member functions for formatted input:

� : istream& istream::scan(char const *format, ...):

This member function returns a reference to an istream object. Therefore, it can be used in combination with, e.g., the extraction operator:

cin.scan("%s", charptr) >> x;

The member function scan() is the analogue of C's scanf() and fscanf() functions.

Page 149: Object Oriented Programing using C++

Object Oriented Programming using C++

149

� istream& istream::vscan(char const *format, va_list list):

When variadic functions are to be used to extract information from streams, the member function vscan() can be used, being the analogue of C's vscanf() and vfscanf() functions.

When binary files must be read, the information should normally not be formatted: an int value should be read as a series of unaltered bytes, not as a series of ASCII numeric characters 0 to 9. The following member functions for reading information from istream objects are available:

� int istream::gcount():

this function does not actually read from the input stream, but returns the number of characters that were read from the input stream during the last unformated input operation.

� int istream::get():

this function returns EOF or reads and returns the next available single character as an int value.

� istream &istream::get(char &c):

this function reads the next single character from the input stream into c. As its return value is the stream itself, its return value can be queried to determine whether the extraction succeeded or not.

� istream& istream::get(char *buffer, int len [, char delim]):

This function reads a series of len - 1 characters from the input stream into the array starting at buffer, which should be at least len bytes long. At most len - 1 characters are read into the buffer. By default, the delimiter is a newline ('\n') character. The delimiter itself is not removed from the input stream.

After reading the series of characters into buffer, an ASCII-Z character is written beyond the last character that was written to buffer. The functions eof() and fail()return 0 (false) if the delimiter was not encountered before len - 1 characters were read. Furthermore, an ASCII-Z can be used for the delimiter: this way strings terminating in ASCII-Z characters may be read from a (binary) file. The program using this get() member function should know in advance the maximum number of characters that are going to be read.

� istream& istream::getline(char *buffer, int len [, char delim]):

This function operates analogously to the previous get() member function, but delim is removed from the stream if it is actually encountered. At most len - 1 bytes are written into the buffer, and a trailing ASCII-Z character is appended to the string that was read. The delimiter itself is not stored in the buffer. If delim was not found (before reading len - 1 characters) the fail() member function, and possibly also eof() will return true.

� istream& istream::ignore(int n , int delim):

Page 150: Object Oriented Programing using C++

Object Oriented Programming using C++

150

This member function has two (optional) arguments. When called without arguments, one character is skipped from the input stream. When called with one argument, n characters are skipped. The optional second argument specifies a delimiter: after skipping n or the delim character (whichever comes first) the function returns.

� int istream::peek():

this function returns the next available input character, but does not actually remove the character from the input stream.

� istream& istream::putback (char c):

The character c is `pushed back' into the input stream, to be read again as the next character. EOF is returned if this is not allowed. One character may always be put back.

� istream& istream::read(void *buffer, int len):

This function reads at most len bytes from the input stream into the buffer. If EOF is encountered first, fewer bytes are read, and the member function eof() will return true. This function will normally be used for reading binary files.

� istream& istream::unget():

an attempt is made to push back the last character that was read into the stream. Normally, this succeeds, as with putback()

`istream' positioning

Although not every istream object supports repositioning, some do. This means that it is possible to rewrite a section of the stream which was written earlier. Repositioning is frequently used in database applications where it must be possible to access the information in the database randomly.

The following members are available:

� streampos istream::tellg():

this function returns the current position where the next write-operation to the stream will take place. For all practical purposes a streampos can be considered to be an unsigned long.

� istream &istream::seekg(streamoff step, ios::seekdir org):

This member function can be used to reposition the stream. The function expects a streamoff step, the stepsize in bytes to go from org. For all practical purposes a streampos can be considered to be a long. The origin of the step, org is a ios::seekdir value. Possible values are:

� ios::beg:

Page 151: Object Oriented Programing using C++

Object Oriented Programming using C++

151

org is interpreted as the stepsize relative to the beginning of the stream. If org is not specified, ios::beg is used.

� ios::cur:

org is interpreted as the stepsize relative to the current position (as returned by tellg() of the stream).

� ios::end:

org is interpreted as the stepsize relative to the current end position of the the stream.

While it is allowed to seek beyond end of file, reading at that point will of course fail. It is not allowed to seek before begin of file. Seeking before ios::beg will cause the ios::fail flag to be set.

Input from streams: the class `ifstream'

The ifstream class is derived from the istream class: it has the same capabilities as the istream class, but can be used to access files for reading. Such files must exist.

In order to use the ifstream class in C++ sources, the preprocessor directive #include <fstream> must be given. If this directive is used, it is not necessary anymore to include the header file iostream.

The following constructors are available for ifstream objects:

� ifstream object:

This is the basic constructor. It creates an ifstream object which may be associated with an actual file later, using the open() member .

� ifstream object(char const *name, int mode, int perm):

This constructor can be used to associate an ifstream object with the file named name, using input mode mode, and permission bits perm.

The input mode is by default ios::in.

The file permission bits are the standard Unix permission values. The default value is 0644, meaning owner: read/write access, group members and others: read-only access.

In the following example an ifstream object is opened for reading. The file must exist:

ifstream in("/tmp/scratch");

� ifstream object(int fd):

Page 152: Object Oriented Programing using C++

Object Oriented Programming using C++

152

construct an ifstream wrapper around an existing open file whose file descriptor is known. Using the well-known file descriptor for the standard input stream, STDIN_FILENO, as defined in, e.g., the unistd.h header file.

Instead of directly associating an ifstream object with a file, the object can be constructed first, and opened later.

� tt(void ifstream::open(char const *name, int mode, int perm)):

Having constructed an ifstream object, the member function open() can be used to associate the ifstream object with an actual file. This member function can only be used with a file whose name is known. It is not possible to open a ifstream object with the open() member function when a file descriptor is available.

� ifstream::close():

Conversely, it is possible to close an ifstream object explicitly using the close() member function. The function sets the ios::fail flag of the closed object. A file is automatically closed when the associated ifstream object ceases to exist.

A subtlety is the following: Assume a stream is constructed, but it is not actually attached to a file. E.g., the statement ifstream istr was executed. When we now check its status through good(), a non-zero (i.e., ok) value will be returned. The `good' status here indicates that the stream object has been properly constructed. It doesn't mean the file is also open. To test whether a stream is actually open, In order to read information from memory, using the stream facilities, istringstream objects can be used. These objects are derived from istream objects. The following constructors and members are available: � istringstream istr:

The constructor will construct an empty istringstream object. The object may be filled with information to be retrieved later.

� istringstream istr(string const &text):

The constructor will construct an istringstream object initialized with the contents of the string text.

� void istringstream::str(string const &text):

This member function will store the contents of the string text into the istringstream object, overwriting its current contents.

The istringstream object is commonly used for converting ASCII text to its binary equivalent, like the C function atoi. The following example illustrates the use of the istringstream class:

#include <string> #include <sstream> int main() { istringstream istr("123 345"); // store some text. int x;

Page 153: Object Oriented Programing using C++

Object Oriented Programming using C++

153

istr >> x; // extract int cout << x << endl; // write it out istr.str("666"); // store another text istr >> x; // extract it cout << x << endl; // write it out /* output of this program: 123 666 */ }

Advanced topics

Copying streams

Usually, files are copied either reading a source file character by character or line by line. The basic mold for processing files is as follows: � In an eternal loop:

1. read a character 2. if reading did not succeed (i.e., fail() returns true), break from the loop 3. process the character

It is important to note that the reading must precede the testing, as it is only possible to know after the actual attempt to read from a file whether the reading succeeded or not. Of course, variations are possible: getline(istream &, string &)returns an istream & itself, so here reading and testing may be realized in one expression. Nevertheless, the above mold represents the general case. So, the following program could be used to copy cin to cout: #include <iostream> int main() { while (true) { char c; cin.get(c); if (cin.fail()) break; cout << c; } return 0; }

By combining the get() with the if-statement a construction comparable to getline() could be used:

if (!cin.get(c)) break; Note, however, that this would still follow the basic rule: ` read first, test later'.

Page 154: Object Oriented Programing using C++

Object Oriented Programming using C++

154

This simple copying of a file, however, isn't required very often. More often, a situation is encountered where a file is processed up to a certain point, whereafter the remainder of the file can be copied unaltered. The following program illustrates this situation: the ignore() call is used to skip the first line (for the sake of the example it is assumed that the first line is at most 80 characters long), the second statement used a special overloaded version of the <<-operator, in which a streambuf pointer is inserted into another stream. The member rdbuf() returns a streambuf *, which is thereupon inserted into cout, thus copying the remainder of cin to cout:

#include <iostream> int main() { cin.ignore(80, '\n'); // skip the first line cout << cin.rdbuf(); // copy the rest by inserting a streambuf * return 0; }

Using C's FILE* in C++: the class `stdiobuf'

I/O in C++ and C can be mixed. A stdiobuf object is a streambuf object that is constructed using a C FILE *. Consequently, all C++ I/O operations are available with C FILE pointers after `wrapping' a stdiobuf around a FILE *.

Normally, cin, cout and cerr are constructed as stdiobuf objects.

Conversely (although not standard ANSI/ISO, but available in the Gnu compiler), a streambuf may be regarded as a FILE, so a streambuf * can be used with all C functions expecting FILE * arguments. Since the rdbuf() member of the class ios returns a streambuf *, all iostream objects can be used as FILE pointers.

In order to use the C I/O facilities, and the stdiobuf class, the header file stdiostream.h must be included (using the preprocessor directive #include <stdiostream.h>).

An example showing the wrapping of a stdiobuf around a FILE * and the use of a iostream object in a C output function is given in the next example:

#include <stdiostream.h> int main() { stdiobuf f = fopen("stdio.cc", "r"); // open this source to read cout << &f; // copy it to cout fprintf(cout.rdbuf(), "hello world\n"); // append `hello world', using // a C function. return 0; /* output produced: the contents of this file, and an extra line containing the text `hello world' */ }

Page 155: Object Oriented Programing using C++

Object Oriented Programming using C++

155

Reading AND Writing to one stream

In order to be able to read and write to a stream an fstream object must be created. As with ifstream and ofstream objects, the constructor receives the name of the file to be opened: fstream inout("iofile", ios::in | ios::out);

Note the use of the ios constants ios::in and ios::out, indicating that the file must be opened for both reading and writing. Multiple mode indicators may be used, concatenated by the binary or operator '|'. Alternatively, instead of ios::out, ios::app could have been used, in which case writing will always be done at the end of the file.

Under DOS-like operating systems, which use the multiple character \r\n sentinels to separate lines in text files the flag ios::bin is required for processing binary files to ensure that \r\n combinations are processed as two characters.

With fstream objects, the ios::out will result in the creation of the file, if the file doesn't exist, and if ios::out is the only mode specification of the file. If the mode ios::in is given as well, then the file is created only if it doesn't yet exist. So, we have the following combinations:

------------------------------------------------------------- Specified Filemode --------------------------------------------- File: ios::out ios::in | ios::out ------------------------------------------------------------- exists File is rewritten File is used as found doesn't exist File is created File is created ------------------------------------------------------------- Once a file has been opened in read and write mode, the << operator can be used to insert information to the file, while the >> operator may be used to extract information from the file. These operations may be performed in random order. The following fragment will read a blank-delimited word from the file, and will then write a string to the file, just beyond the point where the string just read terminated, followed by the reading of yet another string just beyond the location where the string just written ended: fstream f("filename", ios::in | ios::out); string str; f >> str; // read the first word // write a well known text f << "hello world"; f >> str; // and read again Since the operators << and >> can apparently be used with fstream objects, you might wonder whether a series of << and >> operators in one statement might be possible. After all, f >> str should produce a fstream &, shouldn't it?

The answer is: it doesn't. The compiler casts the fstream object into an ifstream object in combination with the extraction operator, and into an ofstream object in combination with the insertion operator. Consequently, a statement like

f >> str << "grandpa" >> str;

results in a compiler error like

Page 156: Object Oriented Programing using C++

Object Oriented Programming using C++

156

no match for `operator <<(class istream, char[8])'

Since the compiler complains about the istream class, the fstream object is apparently considered an ifstream object in combination with the extraction operator.

Of course, random insertions and extractions are hardly used. Generally, insertions and extractions take place at specific locations in the file. In those cases, the position where the insertion or extraction must take place can be controlled and monitored by the seekg() and tellg() member functions Error conditions occurring due to, e.g., reading beyond end of file, reaching end of file, or positioning before begin of file, can be cleared using the clear() member function. Following clear() processing may continue. E.g.,

fstream f("filename", ios::in | ios::out); string str; f.seekg(-10); // this fails, but... f.clear(); // processing f continues f >> str; // read the first word

A common situation in which files are both read and written occurs in data base applications, where files consists of records of fixed size, or where the location and size of pieces of information is well known. For example, the following program may be used to add lines of text to a (possibly existing) file, and to retrieve a certain line, based on its order-numer from the file. Note the use of the binary file index to retrieve the location of the first byte of a line.

#include <fstream> #include <string> void err(char const *msg) { cout << msg << endl; return; } void err(char const *msg, long value) { cout << msg << value << endl; return; } void read(fstream &index, fstream &strings) { int idx; if (!(cin >> idx)) // read index return err("line number expected"); index.seekg(idx * sizeof(long)); // go to index-offset long offset; if (!(index.read(&offset, sizeof(long)))) // read the line-offset return err("no offset for line", idx); if (!strings.seekg(offset)) // go to the line's offset return err("can't get string offet ", offset);

Page 157: Object Oriented Programing using C++

Object Oriented Programming using C++

157

string line; if (!getline(strings, line)) // read the line return err("no line at ", offset); cout << "Got line: " << line << endl; // show the line } void write(fstream &index, fstream &strings) { string line; if (!getline(cin, line)) // read the line return err("line missing"); strings.seekp(0, ios::end); // to strings index.seekp(0, ios::end); // to index long offset = strings.tellp(); if (!index.write(&offset, sizeof(long))) // write the offset to index err("Writing failed to index: ", offset); if (!(strings << line << endl)) // write the line itself err("Writing to `strings' failed"); // confirm writing the line cout << "Write at offset " << offset << " line: " << line << endl; } int main() { fstream index("index", ios::in | ios::out), // maybe add ios::bin strings("strings", ios::in | ios::out); // maybe add ios::bin cout << "enter `r <number>' to read line <number> or " "w <line>' to write a line\n" "or enter `q' to quit.\n"; while (true) { cout << "r <nr>, w <line>, q ? "; // show prompt string cmd; cin >> cmd; // read cmd if (cmd == "q") // process the cmd. return 0; if (cmd == "r") read(index, strings); else if (cmd == "w") write(index, strings); else cout << "Unknown command: " << cmd << endl; } }

As another example of reading and writing files, consider the following program, which also serves as an illustration of reading an ASCII-Z delimited string:

Page 158: Object Oriented Programing using C++

Object Oriented Programming using C++

158

#include <fstream> int main() { fstream f("hello", ios::in | ios::out); // r/w the file f.write("hello", 6); // write 2 ascii-z f.write("hello", 6); f.seekg(0, ios::beg); // reset to begin of file char buffer[20], c; // read the first `hello' cout << f.get(buffer, 100, 0).tellg() << endl;; f >> c; // read the ascii-z delim // and read the second `hello' cout << f.get(buffer + 6, 100, 0).tellg() << endl; buffer[5] = ' '; // change asciiz to ' ' cout << buffer << endl; // show 2 times `hello' }

External processes: `procbuf' and `pfstream'

Streams can also be used to write to or read from separate processes started by a program. A program may read from processes or write to processes, and in most operating systems it is possible to both read and write from processes. Although the last case is in fact a standard a C problem, it might be worth while to examine this case in the context of the C++ programming language.

Reading from or writing from a process is accomplished through a procbuf, which represents an object which has the capability to either read from or write to a process. Around a procbuf a pfstream may be constructed, but this class is currently ( Gnu g++, version 3.0) not completely generally implemented. Even so, acknowledging its restrictions, the pfstream is a handy class, and we present it in several examples as an extension to the procbuf class.

When both reading an wrinting to external processes is at stake, the procbuf cannot be used. Instead, a fork() system call must be invoked using pipe() system calls to set up the communication between the main process (called the parent process) and the process it created (called the child process). This subject is not really covered by the iostream library.

The `procuf' class

The procbuf class is not standard C++ but a Gnu extension. The procbuf class has the following members:

� procbuf():

this constructor creates a procbuf in a closed state. It can be opened, though (see below).

� :

This constructor creates a subprocess. The /bin/sh shell is used to start the command subprocess. The mode argument could be ios::in, in which case the

Page 159: Object Oriented Programing using C++

Object Oriented Programming using C++

159

standard output from the process is read by the process creating the procbuf. Alternatively, it could be ios::out, in which case information could be written to the subprocess, which receives the information at its standard input.

� procbuf *procbuf::open(char const *command, int mode):

this member function can be used to start command using the specified mode, if the associated procbuf is in its closed state. 0 is returned if starting the subprocess fails.

� procbuf *procbuf::close():

this member function terminates the child process. 0 is returned if closing fails.

� procbuf::~procbuf():

the destructor terminates the subprocess (if open), using the close() member function.

The `pfstream' class

Like procbuf the pfstream represents a Gnu extension. Actually, there is no class pfstream, but rather the classes ipfstream for reading from a subprocess and opfstream for writing to a subprocess are available.

In order to use the ipfstream or opfstream class, the header file pfstream.h must be included. Both classes use the procbuf class to set up the communication with the external process. Two constructors are available:

� ipfstream::ipfstream(char const *command, int mode=ios::in, int prot=0664):

this constructor defines an object from which information may be read. The class ipfstream is derived from ifstream, so the functionality that is available with the ifstream class should also be available for the ipfstream. The parameter command defines the command which is started as a subprocess, and whose standard output is read from the ipfstream object. In order to start a subprocess, the last character in command must be the pipe symbol (|).

� opfstream::opfstream(char const *command, int mode=ios::out, int prot=0664):

this class defines an object to which information may be written. The class opfstream is derived from ofstream, so the functionality that is available with the ofstream class should also be available for the opfstream. The parameter command defines the command which is started as a subprocess, and to whose standard input information is written by the opfstream object. In order to start a subprocess, the first character in command must be the pipe symbol (|).

Reading from a separate process

By using a procbuf is is possible to read the standard output from an external process. Both the procbuf and an associated wrapper class, ipfstream are Gnu extensions, and may not be available for other compilers.

Page 160: Object Oriented Programing using C++

Object Oriented Programming using C++

160

In order to use a procbuf, sources must

#include <procbuf.h> A procbuf object may be given as argument to an istream object, thus creating a process whose standard output can be read by a program. When the istream object goes out of scope, the associated procbu will not be destroyed. This is no problem, as the procbuf will be destroyed automatically when its own scope ends. Of course, with dynamically allocated procbuf objects, it is the responsibility of the programmer to ensure that the allocated memory will be properly deleted.

If an ipfstream object is used, all memory management associated with the ipfstream is properly handled by the ipfstream object, without requiring the programmer to be alert. In order to use an ipfstream object, sources must

#include <pfstream.h>

In the following example a procbuf is used to allow the program to process a long-format directory listing: it lists the directories and other entries separately:

#include <iostream> #include <string> #include <procbuf.h> int main() { procbuf pb; pb.open("ls -Fla", ios::in); istream ipb(&pb); string line, dirs; cout << "Files:\n"; while (getline(ipb, line)) { if (line[0] == 'd') dirs += line + '\n'; else cout << line << endl; } cout << "Directories:\n" << dirs; } Procbuf objects inherit the PATH environment variable from their calling process: it will usually contain the ` standard PATH' entries. The following program uses an ipfstream and shows the value of the PATH environment variable as seen by the program: #include <pfstream.h> #include <string> int main() { ipfstream ipb("|echo $PATH"); string

Page 161: Object Oriented Programing using C++

Object Oriented Programming using C++

161

line; while (getline(ipb, line)) // `cout << ipb.rdbuf()' doesn't work cout << line << endl; } Note that, maybe contrary to what is expected, the pipe symbol ( |) must be given as the first character of the ipfstream command string.

Although an ipfstream can be considered a istream, the current implementation appears to have its flaws. One of the unexpected characteristics of the ipfstream is that its rdbuf() member does not appear to work properly. While it should be possible to insert a streambuf pointer into an ostream, the statement cout << ipstr.rdbuf() produces no output. This is confusing, especially as it is not difficult to construct a wrapper class around procbuf that does allow the insertion of its streambuf pointer into an ostream. But allowing for these small inconveniences, the ipfstream object seems to be a useful wrapper class for the procbuf object.

Writing to a separate process

Apart from using a procbuf for reading the standard output from external processes, it is also possible to use procbuf to write information to the standard input of external processes. In order to use a procbuf, sources must

#include <procbuf.h> A procbuf object may be given as argument to an ostream object, thus creating a process that will receive information wriiten to the ostream object at its standard input. When the ostream object goes out of scope, the associated procbuf will not be destroyed. This is no problem, as the procbuf will be destroyed automatically when its own scope ends.

If an opfstream object is used, all memory management associated with the opfstream is properly handled by the opfstream object.

In the following program the program sort is used to sort lines of input that are fed to it. The lines of imput are originally read via the standard input of our program. Our little program will filters out empty lines and lines having a hash mark ( #) as its first non-blank character. The program uses a procbuf to start the external sort command, which is called with a -f flag, to, as its manual page explains, `fold lower case to upper case characters in keys'. A complex way to tell us that the sorting will be done case insensitively:

#include <iostream> #include <string> #include <procbuf.h> int main() { procbuf pb; pb.open("sort -f", ios::out); ostream opb(&pb); string line; while (getline(cin, line)) {

Page 162: Object Oriented Programing using C++

Object Oriented Programming using C++

162

unsigned pos; if ( line.length() == 0 || (pos = line.find_first_not_of(" \t")) != string::npos && line[pos] == '#' ) continue; opb << line << endl; } } The following program does the same filtering , using an opfstream object: #include <pfstream.h> #include <string> int main() { opfstream opb("|sort -f"); string line; while (getline(cin, line)) { int pos; if ( line.length() == 0 || (pos = line.find_first_not_of(" \t")) != string::npos && line[pos] == '#' ) continue; opb << line << endl; } }

Remembering positions: the class `streammarker'

Objects of the class streammarker can be used to remember a position in a stream, an to reset the stream to the remembered position quickly. Streammarkers are convenient tools in situations where input is parsed and one of several alternate inputs can be expected. One input route may eventually fail, in which case it is desirable to backtrack to an initial point, from where an alternate route is taken. Much like the way we tend to traverse a maze, but then a bit quicker.

The streammarker class has the following constructor and methods:

� Constructor:

A streammarker can be constructed using an existing streambuf pointer. The thus constructed streammarker object stores the current position of the stream for which it is constructed. For example:

streammarker mark(cin.rdbuf());

� Member functions:

Page 163: Object Oriented Programing using C++

Object Oriented Programming using C++

163

� streammarker::delta():

This member returns the difference in positions between the current position in the stream and another position in the stream stored in the streammarker object. The returned difference is positive if the stored location is farther into the stream than the current position of the stream.

� int streammarker::delta(streammarker &other):

This member returns the difference in positions between the current streammarker object and another streammarker object. The returned difference is positive if the current object stores a position that is located farther into the stream than the position that is stored by the other streammarker object.

� int streambuf::seekmark(streammarker &mark):

This member sets the postion of the stream to the position where it was when the used streammarker was constructed. The value 0 is returned if resetting succeeds.

Here is a short example showing the use of the various streammarker members: #include <iostream> #include <string> int main() { string line; getline(cin, line); streammarker mark(cin.rdbuf()); getline(cin, line); streammarker mark2(cin.rdbuf()); cout << "The beginning of the second line is at offset " << cin.tellg() + mark.delta() << endl << "The length of the 2nd line, using 2 streammarker objects: " << mark2.delta(mark) << endl; cout << cin.rdbuf()->seekmark(mark) << endl; getline(cin, line); cout << "rereading: " << line << endl; } /* Using input: one two three Generated output: The beginning of the second line is at offset 4 The length of the 2nd line, using 2 streammarker objects: 10 0 rereading: two three */