OOPS PARADIGM Programming language, Object-Oriented Programming, Object-Oriented Languages, Object-based programming languages, Object-oriented programming languages. 2. INTRODUCTION TO C++ Basic concept of oops-Objects, Classes, Encapsulation, Data Abstraction, Inheritance, Polymorphism, Dynamic Binding, Message Passing, Brief History of C++ 3. Data Types & Variables Structure of a C++ program., Comments, Variables,Identifiers,Data types. Declaration of variables, Initialization of variables, Scope of variables, Constants 4. Operator and control structures Types of Operators. Priority of Operators. Control 5. Array and pointer Arrays. Initializing arrays, Strings, Pointers. Pointers and arrays ,Dynamic Memory. 6. structures and union Structures.,User defined data types. 7. Functions Functions . Default values in arguments 8. classes and objects Introduction to class, Class Definition, Classes and Objects, Access specifiers – Private, Public and Protected. Member functions of the class. 9. Constructor and destructor Constructors, Overloading Constructors, Destructor 10. Function Overloading Function overloading, Precautions to be taken while overloading functions. Static Class Members, Static Member Functions, Friend Functions 11. Operator Overloading Introduction to Operator Overloading. ,Operator Overloading Fundamentals. Implementing the operator functions. 12. Inheritance Reusability.,Inheritance concept-singleinheritance.Using the derived class,Constructor and destructor in derived class, Object initialization and conversion., Nested classes (Container classes).\,Multilevel inheritance., Multiple inheritance., Hybrid Inheritance. Virtual base class. 13. Abstract and virtual function Abstract class, Virtual function. Pure virtual function 14. Templates and exception handling Templates. Exception handling, Advanced 15. File Input Output Input/Output with files. Open a file, closing a file



1. Herbert Schildt, “C++ the Complete Reference “, III edition, TMH 1999 2. Balagurusamy, Entrepreneurial “Object Oriented programming with C++”, TMH 3. Barkakatin “objects oriented programming in C++” PHI 1995



Introduction to OOPS,Data types and variables, Operators and control structures,Array and pointer,Structures and union,Functions,Classes and Objects

Constructor and Destructor Functions,Function Overloading,Operator

Overloading,Inheritance,Abstract and virtual function,Templates and exception handling,File handling



Unit1 Introduction to OOPS Programming language Object-oriented programming paradigm Object-Oriented Programming Object-Oriented Languages Object-based programming languages Object-oriented programming languages. Basic concept of oops Objects Classes Encapsulation Data Abstraction Inheritance Polymorphism Dynamic Binding Message Passing Fits of OOP Application of OOPS Brief History of C++ Unit 2-Data Types & Variables 2.1 Structure of a C++ program. 2.2 Comments 2.3 Variables 2.4 Identifiers 2.5 Data types. 2.6 Declaration of variables 2.7 Initialization of variables 2.8 Scope of variables 2.9 Constants Unit3-Operator and control structures 3.1 Types of Operators. 3.2 Priority of Operators. 3.3 Communication through console. 3.3.1 Output 3.3.2 input 3.4 Control Structures. 3.4.1 Conditional structure 3.4.2 Repetitive structures or loops 3.4.3 Bifurcation of control and jumps 3.4.4 The selective Structure: switch Unit4-Array and pointer 4.1 Arrays.


4.1.1 Initializing arrays 4.1.2 Access to the values of an Array 4.1.3 Multidimensional Arrays 4.1.4 Arrays as parameters 4.2 Strings 4.2.1 1Initialization of strings 4.2.2 Assigning values to strings 4.2.3 Converting strings to other types 4.2.4 Functions to manipulate strings 4.3 Pointers. 4.4 Pointers and arrays 4.5 Dynamic Memory. Unit 5-structures and union 5.1 Structures. 5.1.1 Poniters to structures 5.1.2 Nesting structures 5.2 User defined data types. 5.2.1 Typedef 5.2.2 Union 5.2.3 Num Unit-6-Functions 6.1 Functions . 6.2 Default values in arguments 6.3 Void Functions 6.4 Call by value and reference 6.5 Passing Reference to Functions. 6.6 Returning References from Functions 6.7 Inline function 6.8 Recursive function 6.9 Prototyping function Unit7-classes and objects 7.1 Introduction to class. 7.2 Class Definition 7.3 Classes and Objects 7.4 Access specifiers – Private, Public and Protected. 7.5 Member functions of the class. 7.6 Passing and returning objects. 7.7 Pointers to objects. 7.8 Array of objects. 7.9 The special ‘this’ pointer 7.10 self test Unit8 : Constructor and destructor 8.1 Constructors 8.1.1 Syntax rules for writing constructor functions 8.1.2 Different ways of calling contructor 8.2 Overloading Constructors 8.3 Destructor 8.4 Self test


Unit9- Function Overloading 9.1 Function overloading 9.2 Precautions to be taken while overloading functions. 9.3 Static Class Members 9.4 Static Member Functions 9.5 Friend Functions 9.6 Friend for Overloading Operators 9.7 Granting friendship to another class 9.8 Granting friendship to a member function of another class Unit10-. Operator Overloading 10.1 Introduction to Operator Overloading. 10.2 Operator Overloading Fundamentals. 10.3 Implementing the operator functions. 10.4 Rules for overloading the operators. 10.5 Pointer oddities (assignment) and Operator Overloading. 10.6 Overloading the Extraction and Insertion Operators 10.7 Conversion functions. 10.7.1 Conversion from basic to user-defined variable. 10.7.2 Conversion from User-Defined to Basic data type 10.7.3 Conversion Between Objects of Different Classes 10.7.4 Conversion function in the Destination Class 10.8 Table for Type Conversions 10.9 Self Test Unit 11. Inheritance 11.1 Reusability. 11.2 Inheritance concept- single inheritance. 11.2.1 Private derivation 11.2.2 Public derivation 11.2.3 The Protected Access 11.2.4 Summary of derivation 11.2.5 Table of derivation and access specifiers 11.3 Using the derived class 11.4 Constructor and destructor in derived class. 11.5 Object initialization and conversion. 11.6 Nested classes (Container classes). 11.7 Multilevel inheritance. 11.8 Multiple inheritance. 11.9 Hybrid Inheritance. 11.10 Virtual base class. Unit 12- Abstract and virtual function 12.1 Abstract class. 12.2 Virtual function. 12.3 Pure virtual function 12.4 self test Unit-13 Templates and exception handling 13.1 Templates. 13.1.1 Function template 13.1.2 Class templates 13.1.3 Template specialization


13.1.4 Parameter values for templates 13.1.5 Templates and multiple -file project 13.2 Exception handling 13.2.1 Exception not caught 13.2.2 Standard exception 13.3 Advanced class type-casting. 13.3.1 Reinterpret cast 13.3.2 Static cast 13.3.3 Dynamic cast 13.3.4 Tonst_cast 13.3.5 Typeid 13.4 Preprocessor directives. Unit 14- File Input Output 14.1 Input/Output with files. 14.2 Open a file 14.3 Closing a file 14.4 Methods of Input and Output Classes 14.5 Text mode files 14.6 State flags 14.7 Binary files 14.8 Buffers and Synchronization 14.9 I/O Manipulation



Contents 1.1 Programming language 1.2 Object-oriented programming paradigm 1.2.1 Object-Oriented Programming 1.2.2 Object-Oriented Languages 1.2.3 Object-based programming languages 1.2.4 Object-oriented programming languages. 1.3 Basic concept of oops 1.3.1 Objects 1.3.2 Classes 1.3.3 Encapsulation 1.3.4 Data Abstraction 1.3.5 Inheritance 1.3.6 Polymorphism 1.3.7 Dynamic Binding 1.3.8 Message Passing 1.4 Benefits of OOP 1.5 Application of OOPS 1.6 Brief History of C++

1.1 Programming Languages So, what exactly is a programming language? As a loose definition, a programming language is a tool used by a programmer to give the computer very specific instructions in order to serve some purpose for the user. A program is like a recipe. It outlines exactly the steps needed to create something or perform a certain task. Computers do exactly what they are told, no more, no less. When writing a program, a programmer must outline every possible step and scenario that could occur. The first programming languages that emerged, were assembly languages. These languages are exactly the instruction set of a specific processor. These languages are very low-level and hard to understand. For example, say we wanted to add two numbers, 3 and 4 and get a result: in assembly: ldl 3, R1 ldl 4, R2 addl R1, R2, R3

in C++:

Int a = 3 + 4;


The version in C++ is easier to understand and simpler to write. Programmers write their code in a high level language and then use a compiler to translate their code into an assembly language and then into a machine language that will run on the machine they are using. Programs consist of algorithms. An algorithm is just a well-outlined method for completing a task. • • • • ask the user for the first number ask the user for the second number add the two numbers display the result on the screen

This high-level abstraction is not actual code. However, it does express the ideas of a program, and is called pseudo-code. Often, programmers will design their programs in pseudo-code, and then use this to write their actual code. So, why is there more than one programming language? It may seem that a standard language should be agreed on, since all languages are translated using a compiler anyways. However, languages are often designed with a specific use in mind, and some are better than others for dealing with certain problems. So if a programmer is capable of writing a compiler (which is a very complex piece of software) then they can design and create a language. The most important thing to remember about programming languages is that they are only an abstraction! Programming languages were created so developers could express their ideas on a higher level than a computer can understand. Once a user has a good concept of how computers work, and has learned a few computer languages, it becomes much easier to pick up new languages. A programming language is a tool used by programmers in order to specifically outline a series of steps that a computer is to take in a certain instance. High-level programming languages allow a programmer to express ideas on an abstract level, and forces the compiler to worry about the lowlevel implementation details. This allows for faster development of applications, since applications are easier to write. There are even fourth generation languages emerging as viable programming languages. Recall that machine code is considered first generation, assembly languages are second generation, compiled languages are third generation. Fourth generation languages are actually code-generating environments, such as Microsoft's Visual Basic. These fourth generation languages allow programmers to express their ideas visually, and the environment then writes the code to implement these ideas.

1.2 OBJECT-ORIENED PROGRAMMING PARADIGNM The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach. OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called object and then builds data and functions around these objects. The organization of data and functions in object-oriented programs is shown in fig. The data of an object can access the functions of other objects.


Object A Data

Object B Data



Object C



Some of the striking features of object-oriented programming are: • • • • • • • • Emphasis is on data rather than procedure. Programs are divided into what are known as objects Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions. New data and functions can be easily added whenever necessary. Follows bottom-up approach in program design.

Object-oriented programming is the most recent concept among programming paradigms and still means different things to different people. It is therefore important to have a working definition of object-oriented programming before we proceed further. We define “object-oriented programming as an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as template for creating copies of such modules on demand.” Thus, an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access that data. Since the memory partitions are independent, the objects can be used in a variety of different programs without modifications.


Object-Oriented Programming

Since object –oriented programming (OOP) drove the creation of ++, it is necessary to understand its foundational principles. OOP is a powerful way to approach the job of programming.


Programming methodologies have changed grammatically since the invention of the computer, primarily to accommodate the increasing complexity of programs. For example, when computers were first invented, programming was done by toggling in the binary machine instructions using the computer’s front panel. As long as programs grew, assembly language was invented so that a programmer could deal with larger, increasingly complex programs, using symbolic representations of the machine instructions. As program continued to grow, high-level languages were introduced that gave the programmer more tools with which to handle complexity. The first widespread language was, of course, FORTRN. Although FORTRON was a very impressive first step, it is hardly a language that encourages clear, easy-to-understand programs. The 1960s gave birth to structured programming. This is the method encouraged by languages such as and Pascal. The use of structured languages made it possible to write moderately complex programs fairly easily. Structured languages are characterized by their support for stand-alone subroutines, local variables, rich control constructs, and their lack of reliance upon the GOTO. Although structured languages are a powerful tool, even they reach their limit when a project becomes too large. Consider this: At each milestone in the development of programming, techniques and tools were created to allow the programmer to deal with increasingly greater complexity. Each step of the way, the new approach took the best elements of the previous methods and moved forward. Prior to the invention of OOP, many projects were nearing (or exceeding) the point where the structured approach no longer worked. Object-oriente4d methods were created to help programmers break through these barriers. Object-oriented programming took the best ideas of structured programming and combined them with several new concepts. The result was a different way of organizing a program. In the most general sense, a program can be organized in of two ways: around its code (what is happening) or around its data (who is being affected). Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as “code acting on data”. For example, a program written in a structured language such as is defined by its functions, any of which may operate on any type of data used by the program. Object-oriented programs work the other way around. They are organized around data, with the key principle being “data controlling access to code.” In an object-oriented language, you define the data and the routines that are permitted to act on that data. Thus, a data type defines precisely what sort of operations can be applied to that data. To support the principles of object-oriented programming, all OOP languages have three traits in common: encapsulation, polymorphism, and inheritance. 1.2.2 Object-Oriented Languages Object-oriented programming is not the right way of any particular language. Like structured programming, OOP concepts can be implemented using languages such as C and Pascal . However, programming becomes clumsy and may generate confusion when the programs grow large. A language that is specially designed to support the OOP concepts makes it easier to implement them.


The languages should support several of the OOP concepts to claim that they are object-oriented. Depending upon the features they support, they can be classified into the following two categories:

1.2.3 Object-based programming languages, Object-based programming is the style of programming that primarily supports encapsulation and object identity. Major features that are required for object-based programming are: • • • • Data encapsulation Data hiding and access mechanism Automatic initialization and clear-up of objects Operator overloading

Languages that support programming with objects are said to be object-based programming languages. They do not support inheritance and dynamic binding. Ad is a typical object-based programming language.

1.2.4 Object-oriented programming languages. Object-oriented programming incorporates all of object-based programming features along with two additional features, namely, inheritance and dynamic binding. Object-oriented programming can therefore be characterized by the following statement: Object based features + inheritance + dynamic binding Languages that support these features include C++, Smalltalk, Object Pascal and Java . There are a large number of object-oriented programming languages. Table lists some popular general purpose OOP languages and their characteristics. Characteristi cs Binding (early or late) Polymorphis m Data Hiding Concurrency Poor Poor Poor Diffi cult No No No No --------No No No Pro mise d Simul a * Both Smalltal k * Late Objectiv e c Both C++ Ada ** Arly Object Pascal Late Turbo Pasca l Early Ffecl * Earl y Java * Both


Inheritance Multiple Inheritance Garbage Collection




Promise d



Like 3 GL



Som e supp ort No

Genericity Object Libraies



No Not muc h



As seen from Table, all languages provide for polymorphism and data hiding. However , many of them do not provide facilities for concurrency, persistence and generosity. Eiffel, Ad and C++ provide generic facility which is an important construct for supporting reuse. However, persistence(a process of storing objects) is not full supported by any of them. In Smalltalk, though the entire current execution state can be saved to disk, yet the individual objects cannot be saved to an external file. Commercially, C++ is only 10 years old, Smalltalk and Objective C13 years old, and Java only 5 years old. Although Similar has existed for more than two decades, it has spent most of its life I n a re search environment. The field is so new, however ,that it should not be judged too harshly. Use of a particular language depends on characteristics and requirements of an application, organizational impact of the choice, and reuse of the existing programs. C++ has now become the most successful , practical, general purpose OOP language, and is widely used in industry today.

1.3 Basic concept of Object-oriented programming It is necessary to understand some of the concepts used extensively in object-oriented programming. These include:

1.3.1. Objects: Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists. Programming problem is analyzed in term of objects and the nature of communication between them. Program objects should be chosen such that they match closely with real-world objects. Objects take up space in memory and have an associated address like a record in Pascal, or a structure in . When a program is executed, the objects interact by sending messages to one another. Foe example, if” customer” and “account” are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance. Ach object contains data and code to manipulate the data. Objects can interact without having to know details of each other’s data or code. It is sufficient to know the type of message accepted, and the type of response return by the objects. Although different authors represent them differently, fig. below shows two notations that are popularly used in object-oriented analysis and design.


Object:STUDEN T DATA Name Date-of-birth Marks FUNCTIONS Total Average Display

STUDENT Total Average Display

1.3.2. Classes: We just mentioned that objects contained that objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of a class. In fact, objects are variables of that class type. Once a class has been defined , we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects of similar type. Foe example, mango, apple and orange are member of the class fruit. Classes are user-defined data types and behave like the built-in types of a programming language. The syntax is used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement: Fruit mango; Will create an object mango belonging to the class fruit.

1.3.3 Encapsulation: The wrapping of data and functions into a single unit Data encapsulation is the most striking feature of a outside world, and those functions, which are wrapped provide the interface between the object’s data and the direct access by the program is called data hiding . 1.3.4 Data Abstraction: Abstraction refers to the act of representing essential features with out including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes. Sometimes, these are called data members because they hold information. The functions that operate on these data are called methods or member functions. (called class) is known as encapsulation. class. The data is not accessible to the in the class, can access it. These function program. This insulation of the data from

1.3.5 Inheritance


Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, the bird ‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’. The principle behind this sort of division is that each derived class shares common characteristics with the class from which it is derived as illustrated in figure. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both of the classes. The real appeal and the power of the inheritance mechanism is that it a allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any

Bird Attributes Feathers Lay eggs

Flying Bird Attributes ………… …………

Non-flying Bird Attributes ……………… ………………..

Robin Attributes ………… ………… …………

Swallow Attributes ………… ………… .

Penguin Attributes ………. ……….

Kiwi Attributes ……….. …………

Undesirable side-effects into the rest of the classes. Note that each sub-class defines only those features that are unique to it. Without the use of classification, each class would have to explicitly include all its features.

1.3.6 Polymorphism: Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the type of data used in the operation. For example, consider the


operation of addition. For two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Figure below illustrates that a single function name can be used to handle different number and different types of arguments. This is something similar to a particular word having several different meanings depending on the context. Using a single function name to perform different types of tasks is known as function overloading.

Shape Draw ()

Circle object Draw(circle)

Box object Draw(box)

Triangle object Draw(triangle)

Polymorphism plays an in important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance.

1.3.7 Dynamic Binding: Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance. A function call associated with a polymorphism reference depends on the dynamic type of that reference. Consider the procedure “draw” in the above figure. By inheritance, every object will have this procedure. Its algorithm is, however, unique to each object and so the draw procedure will be refined in each class that defines the object. At run-time, the code matching the object under current reference will be called.

1.3.8 Message Passing: An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, therefore, involves the following basic steps: 1. Creating classes that define objects and their behavior,


2. Creating objects from class definitions, and 3. Establishing communication among objects. Objects communicate with one another by sending and receiving information much the same way as people pass message to one another. The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterparts.

A message for an object as a request for execution of a procedure, and therefore will invoke a function (procedure) in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function(message) and the information to be sent. Example:

Employee. salary(name);

object message


Objects have a life cycle. They can be created and destroyed. Communication with an object is feasible as long as it is alive.

1.4 Benefits of OOP OOP offers several benefits to both the program designer and the user. Object-orientation contributes to the solution of many problems associated with the development and quality of software products. A new technology promises greater programmer productivity, better quality of software and lesser maintenance cost. The principal advantages are: • • • Through inheritance, we can eliminate redundant code and extend the use of existing Classes. We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to co-exist without any interference. It is possible to map objects I the problem domain to those in the program. It is easy to partition the work in a project based on objects. The data-centered design approach enables us to capture more details of a model in implemental form Object-oriented systems can be easily upgraded from small to large systems.

• • • • • •


• •

Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler. Software complexity can be easily managed.

While it is possible to incorporate all these features in an object-oriented system, their importance depends on the type of the project and the preference of the programmer. There are a number of issues that need to be tackled to reap some of the benefits stated above. For instance, object


libraries must be available for reuse. The technology is still developing and current products may be superseded quickly. Strict controls and protocols need to be developed if reuse is not to be compromised. Developing software that is easy to use makes it hard to build. It is hoped that the object-oriented programming tools would help manage this problem.

1.5 Application of OOPS OOP has become one of the programming buzzwords today. There appears to be a great deal of excitement and interest among software engineers in using OOPS . Applications of OOP are beginning to gain importance in many areas. The most popular application of object –oriented programming, up to now, has been in the area of user interface design such as windows. Hundreds of windowing systems have been developed, using the OOP techniques. Real –business systems are often much more complex and contain many more objects with complicated attributes and methods. OOP is useful in these types of applications because can simplify a complex problem. The promising areas for application of OOP includes. • • • • • • • • Real-time systems Simulation and modeling Object-oriented databases Hypertext, hypermedia and expert text AI and expert systems Neural networks and parallel programming Decision support and office automation systems CIM/CAM/CAD systems

The object-oriented paradigm sprang from the language, has matured into design, and has recently moved into analysis. It is believed that the richness of OOP environment will enable the software industry to improve not only the quality of software systems but also its productivity. Object-oriented technology is certainly going to change the way the software engineers think, analyze, design and implement future systems.

1.6 Brief History of C++ The C++ Programming Language is basically an extension of the C Programming Language. The C Programming language was developed from 1969-1973 at Bell labs, at the same time the UNIX operating system was being developed there. C was a direct descendant of the language B, which was developed by Ken Thompson as a systems programming language for the fledgling UNIX operating system. B, in turn, descended from the language BCPL which was designed in the 1960s by Martin Richards while at MIT. In 1971 Dennis Ritchie at Bell Labs extended the B language (by adding types) into what he called NB, for "New B". Ritchie credits some of his changes to language constructs found in Algol68, although he states "although it [the type scheme], perhaps, did not emerge in a form that Algol's


adherents would approve of" After restructuring the language and rewriting the compiler for B, Ritchie gave his new language a name: "C". In 1983, with various versions of C floating around the computer world, ANSI established a committee that eventually published a standard for C in 1989. In 1983 Bjarne Stroustrup at Bell Labs created C++. C++ was designed for the UNIX system environment, it represents an enhancement of the C programming language and enables programmers to improve the quality of code produced, thus making reusable code easier to write. Why Program in C++? So what is so special about C++? Why should you use C++ to develop your applications? First, C++ is not the best language to use in every instance. C++ is a great choice in most instances, but some special circumstances would be better suited to another language. There are a few major advantages to using C++: 1. C++ allows expression of abstract ideas C++ is a third generation language that allows a programmer to express their ideas at a high level as compared to assembly languages. 2. C++ still allows a programmer to keep low-level control Even though C++ is a third generation language, it has some of the "feel" of an assembly language. It allows a programmmer to get down into the low-level workings and tune as necessary. C++ allows programmers strict control over memory management. 3. C++ has national standards (ANSI) C++ is a language with national standards. This is good for many reasons. Code written in C++ that conforms to the national standards can be easily integrated with preexisting code. Also, this allows programmers to reuse certain common libraries, so certain common functions do not need to be written more than once, and these functions behave the same anywhere they are used. 4. C++ is reusable and object-oriented C++ is an object-oriented language. This makes programming conceptually easier (once the object paradigm has been learned) and allows easy reuse of code, or parts of code through inheritance. 5. C++ is widely used and taught C++ is a very widely used programming language. Because of this, there are many tools available for C++ programming, and there is a broad base of programmers contributing to the C++ "community".



Contents 2.1 Structure of a C++ program. 2.2 Comments 2.3 Variables 2.4 Identifiers 2.5 Data types. 2.6 Declaration of variables 2.7 Initialization of variables 2.8 Scope of variables 2.9 Constants

2.1 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; } The above code shows the source code for our first program, which we can name, for example, hiworld.cpp. The way to edit and compile a program depends on the compiler you are using. Depending on whether 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, 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 on 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 of whether it is at the beginning, at the end or in 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 is 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 immediately follows its formal declaration and it is 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 signifies the end of the instruction and must be included after every instruction 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 causes the main() function 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 humans that may read it. Here is a program with some more instructions: // 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 have been perfectly defined thus: int main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; } We were also free to divide the code into 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 and do not produce any code. These must be specified in their own line and do not require the include a semicolon (;) at the end.

2.2 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 of including several lines. We are going to add comments to our second program: /* my second program in C++ with more comments */ #include <iostream.h> int main () { cout << "Hello World! "; // says Hello World! cout << "I'm a C++ program"; // says I'm a C++ program return 0; } Hello World! I'm a C++ program

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

2.3 Variables The usefulness of the "Hello World" programs shown in the previous section are something more than questionable. We had to write several lines of code, compile them, and then execute the resulting program just to obtain a sentence on the screen as result. It is true that it would have


been much faster to simply write the output sentence by ourselves, but programming is not limited only to printing texts on screen. 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 the variable. Let's think that I ask you to retain the number 5 in your mental memory, and then I ask you to also memorize the 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 the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now subtract and obtain 4 as the result. All this process that you have made is a simile 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 millions 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 any names we wanted to invent, as long as they were valid identifiers.

2.4. 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 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 any key word of the C++ language 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 16 bit code (like some compilers for DOS) also include far, huge and near as key words. Very important: The C++ language is "case sensitive", that means that an 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 as the variable result nor the variable Result.

2.5.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 storing a simple number, a letter or a large number is not going to occupy the same space in memory. Our computer's memory is organized in bytes. A byte is the minimum amount of memory that 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, such as 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 char short Bytes* Description 1 2 character or integer 8 bits length. integer 16 bits length. Range* signed: -128 to 127 unsigned: 0 to 255 signed: -32768 to 32767 unsigned: 0 to 65535 signed:-2147483648 to 2147483647 unsigned: 0 to 4294967295



integer 32 bits length.



Integer. Its length traditionally depends on the length of the system's Word type, thus in MSDOS it is 16 bits long, whereas in 32 bit systems (like Windows See short, long 9x/2000/NT and systems that work under protected mode in x86 systems) it is 32 bits long (4 bytes). floating point number. double precision floating point number. long double precision floating point number. 3.4e + / - 38 (7 digits) 1.7e + / - 308 (15 digits) 1.2e + / - 4932 (19 digits)

float double long double

4 8 10




Boolean value. It can take one of two values: true or false NOTE: this is a type recently added by the ANSI-C++ true or false standard. Not all compilers support it. Consult section bool type for compatibility information. Wide character. It is designed as a type to store international characters of a two-byte character set. NOTE: this is a wide characters type recently added by the ANSI-C++ standard. Not all compilers support it.

wchar_t 2

* 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.

2.6. 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; 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, therefore in the second declaration we could have written: int MyAccountBalance; with exactly the same meaning and since this is the most usual way, 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 different 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 respectively. The following two declarations are equivalent: unsigned MyBirthYear; unsigned int MyBirthYear; To see what variable declaration looks like in action in a program, we are going to show 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; } Do not worry if something about the variable declarations looks a bit strange to you. You will see the rest in detail in coming sections.

2.7. Initialization of variables When declaring a local variable, its value is undetermined by default. But you may want a variable to store a concrete value the moment that 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 at 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++.

2.8. 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 is recommended under some circumstances to follow the indications of the C language when declaring variables, since it can be useful when 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).


Global variables can be referred to 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) their scope is the whole main function. In the example above, this means that if another function existed in addition to main(), 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 there exists external scope, that causes a variable to be visible not only in the same source file but in all other files that will be linked together.

2.9. 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: // decimal 75 // octal 0113 0x4b // hexadecimal All of them represent the same number: 75 (seventy five) expressed as a radix-10 number, octal and hexdecimal, respectively.


Floating Point Numbers:-They express numbers with decimals and/or exponents. 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 source code 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 \r \t \v \b \f \a \' \" \? \\ For example: Newline carriage return Tabulation vertical tabulation Backspace page feed alert (beep) single quotes (') double quotes (") question (?) inverted slash (\)


'\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 immediately follow 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). Constants 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 resort 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. The #define directive is not a code instruction, it is a directive for the preprocessor, therefore 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.



Contents 3.1 3.2 3.3 Types of Operators. Priority of Operators. Communication through console. 3.3.1 Output 3.3.2 input Control Structures. 3.4.1 Conditional structure 3.4.2 Repetitive structures or loops 3.4.3 Bifurcation of control and jumps 3.4.4 The selective Structure: switch


Introduction 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.

3.1 Different types of operators • 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 int a, b; a = 10; b = 4; a = b; b = 7; this code (with the evolution of the variables' content in green color): // a:? b:? // a:10 b:? // a:10 b:4 // a:4 b:4 // a:4 b:7

Will give us the 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 C++ has over other programming languages is that the assignation operation can be used as the 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), leaving 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 should 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 remainder of a division of two integer values. For example,


if we write a = 11 % 3;, the variable a will contain 2 as the result since 2 is the remainder 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.

Another example of saving language 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 because 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++). Although in 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 B=3; A=++B; // A is 4, B is 4 Example 2 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. (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) (a*b >= c) would return false. 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 the result of the operation is true. 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 saying 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 !false returns 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 Second result result Operand Operand a && b a || b a b true true false false true false true false true false false false true true true 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.

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

Bitwise Operators ( &, |, ^, ~, <<, >> ).


Bitwise operators modify variables considering the bits that represent the values that 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 For more information about binary numbers and bitwise operations, consult Boolean logic. • Explicit type casting operators

Type casting operators allows you to convert a datum of a given type to another. There are several 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 enclosing the expression between parentheses: 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 a few more operators, like the ones referring to pointers or the specifics for object-oriented programming. Each one is treated in its respective section.


3.2 Priority of operators When making complex expressions with several operands, 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 1 2 :: () [ ] -> . sizeof ++ -~ ! 3 &* (type) +4 5 6 7 8 9 10 11 12 13 */% +<< >> < <= > >= == != &^| && || ?: Reference and Dereference (pointers) Type casting Unary less sign arithmetical operations arithmetical operations bit shifting (bitwise) Relational operators Relational operators Bitwise operators Logic operators Conditional Left Left Left Left Left Left Left Right Right Left increment/decrement Complement to one (bitwise) Unary NOT Right Description Scope Associativity Left Left

= += -= *= /= %= Assignation >>= <<= &= ^= |= , Comma, Separator

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


All these precedence levels for operators can be manipulated or become more legible using parenthesis signs ( and ), as in this example: a = 5 + 7 % 2; might be written as: a = 5 + (7 % 2); or a = (5 + 7) % 2; 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, always include parenthesis. It will probably also be more legible code.

3.3 Communication Through Console The console is the basic interface of computers, normally it is the set composed of 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, cerr and clog have also been implemented - 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 on the screen and receive his/her input from the keyboard. 3.3.1 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 // prints number 120 on screen cout << 120; // prints the content of variable x on screen cout << x; 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 is 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 suppose 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."; 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 by 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.

3.3.2 Input (cin). Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. This must be 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. Therefore, 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 the type of the variable that you are using as a 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 #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; }

Please enter an integer value: 702 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 even 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 a name (which is a string of characters), the result may cause 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. Farther ahead, when we will see how to use strings of characters we will see possible solutions for the errors that can be caused by 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.

3.4 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 has to be done 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 a 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. 3.4.1 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 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 more than a single instruction to be executed, we must group them in a block of instructions by using curly brackets { }.

3.4.2 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: 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; Enter the starting number > 8 8, 7, 6, 5, 4, 3, 2, 1, FIRE!


cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!"; return 0; } 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 an indefinite number of times while the condition (n>0) remains 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 such a 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); 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> Enter number (0 to end): 12345 You entered: 12345


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): 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 an 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. For example we could write: for (;n<10;) if we want to specify no initialization and no 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 by one, the loop's condition will become false after the 50th loop, when both n and i will be equal to 50.

3.4.3 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; } The continue instruction. 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!


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: // 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; } The goto instruction. It allows making 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 an 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 for 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; } 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. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE! 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!


4.4.4 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: 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, then 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 of the following code fragments are equivalent: switch example Switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; } if-else equivalent 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 continue executing the rest of the blocks of instructions until the first appearance of the break instruction or the end of the switch selective block. This makes it unnecessary to include curly brackets { } in each of the cases, and it can also be useful to execute the 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. Therefore we cannot put variables (case (n*2):) or ranges (case (1..3):) because they are not valid constants.



Contents 4.1 Arrays. 4.1.1 Initializing arrays 4.1.2 Access to the values of an Array 4.1.3 Multidimensional Arrays 4.1.4 Arrays as parameters 4.2 Strings 4.2.1 1Initialization of strings 4.2.2 Assigning values to strings 4.2.3 Converting strings to other types 4.2.4 Functions to manipulate strings 4.3 Pointers. 4.4 Pointers and arrays 4.5 Dynamic Memory.

4.1. 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 with a different identifier. Instead, using an array we can store 5 different values of the same type, int for example, with a 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 is enclosed within brackets [], specifies how many of these elements the array contains. Therefore, to declare billy as shown above it is 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 it must assign to the array before any instruction is considered. 4.1.1 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 useless repetition, C++ includes the possibility of leaving the brackets empty [ ] and the size of the Array will be defined by the number of values included between curly brackets { }: int billy [] = { 16, 2, 77, 40, 12071 };

4.1.2 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 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:

For example, to store the value 75 in the third element of billy a suitable sentence would be: 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 purposes, the expression billy[2] is like any other variable of type int. Notice that the third element of billy is specified billy[2], since first is billy[0], the second is 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 create 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 farther 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 is to specify indices for a concrete array element when referring to it. We must simply take care not to confuse these two possible uses of brackets [ ] with arrays: int billy[5]; // declaration of a new Array (begins with a type name) // access to an element of the Array. billy[2] = 75; 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]; } 12206


cout << result; return 0; }

4.1.3 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.

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 as 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! This would consume about 3000 megabytes of RAM memory if we could declare it. Multidimensional arrays are nothing more than an abstraction, since we can obtain the same results with a simple array just 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 () { for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n][m]=(n+1)*(m+1); } return 0; } // pseudo-multidimensional array #include <iostream.h> #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT * WIDTH]; int n,m; int main () { 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 could be done by changing the line: #define HEIGHT 3 to #define HEIGHT 4 with no need to make any other modifications to the program. 4.1.4 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 to a function, even if it is ordered as an array, but it is allowed to pass its address. This has almost the same practical effect and it 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, 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 to write a call like this: procedure (myarray); Here you have a complete example: // 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; } 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 tells the function the length of each array that we pass to it as the first parameter. This allows the for loop that prints out the array to know the range to check in the passed array. In a function declaration is also possible to include multidimensional arrays. The format for a tridimensional array 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 are 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. 5 10 15 2 4 6 8 10


Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for less experienced programmers.



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): 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 always be 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, a convention has been reached 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 a null character ('\0') it is included in order to indicate the end of the string. The panels in gray color represent indeterminate values. 4.2.1 Initialization of strings Because strings of characters are ordinary arrays they fulfill all their same rules. For example, if we want to initialize a string of characters with predetermined values we can do it just like 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, strings of characters have an additional way to initialize their values: using constant strings. In the expressions we have used in examples in previous chapters constants that represented entire strings of characters have already appeared several times. These are specified enclosed between double quotes ("), for example: "the result is: " is a constant string that we have probably used on some occasion. Unlike single quotes (') which specify single character constants, double quotes (") are constants that specify a succession of characters. Strings enclosed between double quotes always have a null character ('\0') automatically appended at the end. Therefore we could initialize the string mystring with values by either 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, notice 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. 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, always have the rule previously underlined present. 4.2.2 Assigning values to strings Since the lvalue of an assignation can only be an element of an array and not the entire array, it would be valid to assign a string of characters to an array of char using a method like this: mystring[0] mystring[1] mystring[2] mystring[3] = = = = 'H'; 'e'; 'l'; '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); 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; } Notice that we 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 operation as 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 J. Soulie


Another frequently used method to assign values to an array is by directly using 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 of 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'). The following example repeats whatever you type on your keyboard. It is quite simple but serves as an example of 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 with the new one that is introduced. If you remember the section about communication through the 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 a 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. That makes your program unstable in case the user input is longer than the array that will host it.

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


4.2.3 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: // 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; } 4.2.4 Functions to manipulate strings Enter price: 2.75 Enter quantity: 21 Total price: 57.75

The cstring library (string.h) defines many functions to perform manipulation operations with Clike strings (like already explained strcpy). Here you have a brief look at 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[]

4.3 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 trouble, since there must be only one house with that number and, in addition, we know that the 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 it 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 placed fortunately that is something automatically done by the compiler and the operating system at 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 is 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 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. Farther ahead we will see how this type of variable is declared. Reference operator (*) Using a pointer we can directly access 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". Therefore, 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.

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 of including or not including 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 a pointer to directly reference the value that it point to, it becomes necessary to specify which data type a pointer points to when declaring it. It is not the same to point to a char as it is to point to an int or a float type. Therefore, the declaration of pointers follows 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 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 amount of space nor are of the same type, one is int, another one is char and the other one float. I emphasize that the asterisk (*) that we use when declaring a pointer means only that it is a pointer, and should not be confused with the reference operator that we have seen a bit earlier which 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; value1==10 / value2==20


int * mypointer; mypointer = &value1; *mypointer = 10; mypointer = &value2; *mypointer = 20; cout << "value1==" << value1 << "/ value2==" << value2; return 0; } 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 (&). Then we have assigned 10 to the value pointed by mypointer, which 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; p2 = &value2; *p1 = 10; *p2 = *p1; p1 = p2; *p1 = 20; // p1 = address of value1 // p2 = address of value2 // value pointed by p1 = 10 // value pointed by p2 = value pointed by p1 // p1 = p2 (value of pointer copied) // value pointed by p1 = 20 value1==10 / value2==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 the asterisk. The meaning of using or not using 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 of where it is pointing. 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 as the declaration of types, therefore, because they are associative operators from the right, the asterisks are evaluated first than the type. We have talked about this in, although it is enough that you know clearly that -unless you include parenthesis- you will have to put an asterisk (*) before each pointer that you declare.

4.4 Pointers and arrays The concept of array is very much 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, the only difference is 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 an array name is 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 the character of variables all the expressions that include pointers in the following example are perfectly valid: // 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] << ", "; 10, 20, 30, 40, 50,


return 0; } In chapter "Arrays" we used bracket signs [] several times in order to specify the index of the element of the Array to which we wanted to refer. Well, the bracket signs operator [] are known as offset operators and they are equivalent to adding 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 them to point, 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, it in no case indicates the reference operator (*). Remember, they are two different operators, although they are written with the same sign. Thus, we must take care not to confuse the previous with: int number; int *tommy; *tommy = &number; That anyway would not have much sense in this case. As 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 as declaring the variable pointer: char * terry = "hello"; 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 the value 1702 and not 'h' nor "hello", although 1702 points to these characters. 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 either of those two expressions something like this would happen:

Arithmetic of pointers To conduct arithmetical operations on pointers is a little different than to conduct them on other integer data types. To begin with, 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. 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. 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 is 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). In 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 both q and p are increased by one. It is equivalent to: *p = *q; p++; q++; Like always, I recommend you use parenthesis () in order to avoid unexpected results. Pointers to pointers C++ allows the use of pointers that point to pointers, that these, in 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 to which 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)); increase (&c,sizeof(c)); cout << (int) a << ", " << b << ", " << c; return 0; 6, 10, 13


} 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 operations with pointers to functions. The greatest use of this 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 except the name of the function is enclosed between parenthesis () and a pointer asterisk (*) is inserted before the name. It might not be a very handsome syntax, but that 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; } In the example, minus is a global pointer to a function that has two parameters of type int, it is immediately assigned to point to the function subtraction, all in a single line: int (* minus)(int,int) = subtraction; 8

4.5. 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


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. Operators new and delete are exclusive of C++. Farther ahead in this section are shown the C equivalents for these operators. Operators new and new[ ] In order to request dynamic memory, the operator new exists. new is 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 a 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 assigning 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 multitask interfaces it 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 it is recommended to always check to see if the returned pointer is null after a call to new. 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 it is no longer needed it should be freed so that it becomes available for future requests of dynamic memory. The operator delete exists for this purpose, whose form is: 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; 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 us requesting to the system to provide as much space as 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 rarely 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 the 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: 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*, which is the 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 the 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 so 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.



Contents 5.1

5.2 5.2.1 5.2.2 5.2.3

Structures. 5.1.1 Poniters to structures 5.1.2 Nesting structures User defined data types. Typedef Union Enum

5.1 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 we are 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, we can also declare the structure objects apple, orange and melon 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 is 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 any of these elements as if they were standard variables of their respective types: apple.price orange.price melon.price each one being of its corresponding data type:, and 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"); Enter title: Alien Enter year: 1979 My favourite movie is: 2001 A Space Odyssey (1968) And yours: Alien (1979)


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"; } The example shows how we can use the elements of a structure and the structure itself as normal variables. For example, yours.year is a valid variable of type int, and mine.title is a valid array of 50 chars. Notice that mine and yours are also treated as valid variables of type movies_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 the entire 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. // 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]; Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter title: Alien year: 1979 title: Blade Runner year: 1982 title: Matrix year: 1999 title: Rear Window year: 1954 title: Taxi Driver year: 1975

You have entered these movies: Alien (1979) Blade Runner (1982) Matrix (1999) Rear Window (1954)


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"; }

Taxi Driver (1975)

5.1.1 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 to introduce a new operator: // pointers to structures #include <iostream.h> #include <stdlib.h> struct movies_t { Enter title: Matrix Enter year: 1999 You have entered: Matrix (1999)


char title [50]; int year; };

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 an 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: pmovie->title that could be translated to: (*pmovie).title both expressions pmovie->title and (*pmovie).title are valid and mean that we are evaluating the element title of the structure pointed by pmovie. You must distinguish it clearly from: *pmovie.title that is equivalent to *(pmovie.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 pmovie.title pmovie->title *pmovie.title

Description Element title of structure pmovie Element title of structure pointed by pmovie Value pointed by element title of structure pmovie

Equivalent (*pmovie).title *(pmovie.title)

5.1.2 Nesting structures Structures can also be nested so that a valid element of a structure can also be another structure. 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; Therefore, after the previous declaration we could use the following expressions: 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 as 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. 5.2 User Defined Data Types We have already seen a data type that is defined by the user (programmer): the structures. But in addition to these there are other kinds of user defined data types:

5.2.1 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 typedef typedef typedef

char C; unsigned int WORD; char * string_t; 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 will need to change it in a later version, or if a type you want to use has too long a name and you want it to be shorter.

5.2.2 Unions Unions allow a portion of memory to be accessed as different data types, since all of them are in fact 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.f mytypes.i

each one of a different data type. Since all of them are referring to a same location in memory, the modification of one of the elements will afect 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 that we can access 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 we will be able to access the elements directly by its name. For example, look at the difference between these two declarations: Union struct { char title[50]; char author[50]; union { float dollars; int yens; } price; } book; anonymous union 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 we did not. The difference is when accessing 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 to either numerical or character constants nor to the 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 another way, we have created a new data type without it being based on any 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 in the following expressions will be valid: colors_t mycolor; mycolor = blue; if (mycolor == green) mycolor = red; In fact our enumerated data type is compiled as an integer and its possible values are any type of integer constant specified. If it 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, black 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 equal to 1.



Contents 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9

Functions Default values in arguments Void Functions Call by value and reference Passing Reference to Functions. Returning References from Functions Inline function Recursive function Prototyping function

6.1.Functions Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming in C++ can offer 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 passing 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; The result is 8


return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } 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 with the main function. So we will begin 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 in the code lines above:

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, control is lost by main 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 called it (main) following the program from the same point at which it was interrupted by the call to addition. But additionally, return was called with the content of variable r (return (r);), which 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 another way, 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. Scope of variables [re] You must consider that the scope of variables declared within a function or any other block of instructions is only their own function or their 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 function addition. Also, it had been impossible to use the variable z directly within 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 The The The first result is 5 second result is 5 third result is 2 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. In order to understand well these examples you must consider once again that a call to a function could be perfectly 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 as 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 introduced 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 note that instead of: z = 4 + subtraction (x,y); we could 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;

6.2. 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; } As we can see in the body of the program there are two calls to the function divide. In the first one: divide (12) 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 is lacking (notice the function declaration, which finishes with 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 stepped on by the passed parameter, that is 4, making the result equal to 5 (20/4). 6 5

6.3. Void function If you remember the syntax of a function declaration: type name ( argument1, argument2 ...) statement 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 it to return any value, moreover, we do not need it to receive any parameters. For these cases, the void type was devised in the 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; } Although in C++ it is not necessary to specify void, its use is considered suitable to signify 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 specifing its name and enclosing the arguments between parenthesis. The non-existence of arguments does not exempt us from the obligation to use parenthesis. For that reason the call to dummyfunction is dummyfunction (); This clearly indicates that it is a call to a function and not the name of a variable or anything else. I'm a function!

6.4 Call by value and 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 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 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.

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 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; } The first thing that should call your attention is that in the declaration of duplicate the type of each argument was followed by an ampersand sign (&), that 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. x=2, y=6, z=14

To express it another way, 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, shows the values of the three variables of main doubled. 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) 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++. 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

6.5. Passing Reference to Functions. Reference variables are particularly useful when passing to functions. The changes made in the called functions are reflected back to the calling function . The program uses the classic problem in programming, swapping the values of two variables. e.g. void val_swap(int x, int y) // Call by Value { int t; t = x; x = y; y = t; } void add_swap(int *x, int *y) // Call by Address { int t; t = *x; *x = *y; *y = t; } void val_swap(int &x, int &y) // Call by Reference {


int t; t = x; x = y; y = t; }

void main() { int n1 = 25, n2 = 50; cout << “Before call by value : “; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; val_swap( n1, n2 ); cout << “ After call by value : “; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; cout << “Before call by address : “; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; val_swap( &n1, &n2 ); cout << “ After call by address : “; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; cout << “Before call by reference: “; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; val_swap( n1, n2 ); cout << “ After call by value : “ ; cout << “ n1 = “ << n1 << “ n2 = “ << n2 << endl; } Output : Before call by value : n1 = 25 n2 = 50 After call by value : n1 = 25 n2 = 50 // x = 50, y = 25 Before call by address : n1 = 25 n2 = 50 After call by address : n1 = 50 n2 = 25 //x = 50, y = 25 Before call by reference: n1 = 50 n2 = 25 After call by reference : n1 = 25 n2 = 50 //x = 25, y = 50 You can see that the only difference in writing the functions in call by value and call by reference is while receiving the parameters where as in pass by address the function body has some changes, i.e. they use (*) indirection operator to manipulate the variables.

6.6. Returning References from Functions


Just as in passing the parameters by reference, returning a reference also doesn’t return back a copy of the variable , instead an alias is returned. e.g. int &func(int &num) { : : return(num); } void main() { int n1,n2; : : n1 = fn( n2); } Notice that the function header contains an ampersand (&) before the function name. This is how a function is made to return reference variable. In this case, it takes a reference to an integer as its argument and returns a reference to an integer. This facility can be very useful for returning objects and even structure variables.

6.7. Inline functions The inline directive can be included before a function declaration to specify that the function must be compiled as code at the same point where it is called. This is equivalent to declaring a macro. 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. 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.

6.8. Recursive function Recursivity is the property that functions have to be called by themselves. It is useful for tasks such as 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; } 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 at 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 storing factorials greater than 12!. Type a number: 9 !9 = 362880

6.9 Prototyping functions Until now, we have defined the all of the functions before the first appearance of calls to them, that generally was in main, leaving the function main for the end. If you try to repeat some of the examples of functions described so far, 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 a function it must have been declared previously (it must be known), like we have done in all our examples. But there is an alternative way to avoid writing 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, but quite significant, declaration of the complete definition 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 recommended. 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); } 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 half of the 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); 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. Type a number (0 Number is odd. Type a number (0 Number is even. Type a number (0 Number is even. Type a number (0 Number is even. to exit): 9 to exit): 6 to exit): 1030 to exit): 0


Nevertheless, the specific reason why this program needs at least one of the functions prototyped is because in odd there is a call to even and in even there is a call to odd. 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 case that they are very long. Having the prototype of all the functions in the same place can spare us some time when determining how to call it or even ease the creation of a header file.



Contents 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10

Introduction to class. Class Definition Classes and Objects Access specifiers – Private, Public and Protected. Member functions of the class. Passing and returning objects. Pointers to objects. Array of objects. The special ‘this’ pointer self test

7.1 Introduction to Classes Object-oriented programming (OOP) is a conceptual approach to design programs. It can be implemented in many languages, whether they directly support OOP concepts or not. The C language also can be used to implement many of the object-oriented principles. However, C++ supports the object-oriented features directly. All these features like Data abstraction, Data encapsulation, Information hiding etc have one thing in common – the vehicle that is used to implement them. The vehicle is “ class.” Class is a user defined data type just like structures, but with a difference. It also has three sections namely private, public and protected. Using these, access to member variables of a class can be strictly controlled.

7.2. Class Definition The following is the general format of defining a class template:

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


class tag_name { public : type member_variable_name; : type member_function_name(); : private: type member_variable_name; : type member_function_name(); : };

// Must

// Optional

The keyword class is used to define a class template. The private and public sections of a class are given by the keywords ‘private’ and ‘public’ respectively. They determine the accessibility of the members. All the variables declared in the class, whether in the private or the public section, are the members of the class. Since the class scope is private by default, you can also omit the keyword private. In such cases you must declare the variables before public, as writing public overrides the private scope of the class. e.g. class tag_name { type member_variable_name; // private : type member_function_name(); // private :

public : type member_variable_name; : type member_function_name(); : };

// Must

The variables and functions from the public section are accessible to any function of the program. However, a program can access the private members of a class only by using the public member functions of the class. This insulation of data members from direct access in a program is called information hiding. e.g.


class player { public : void getstats(void); void showstats(void); int no_player; private : char name[40]; int age; int runs; int tests; float average; float calcaverage(void); }; The above example models a cricket player. The variables in the private section – name, age, runs, highest, tests, and average – can be accessed only by member functions of the class calcaverage(), getstats() and showstats(). The functions in the public section - getstats() and showstats() can be called from the program directly , but function calcaverage() can be called only from the member functions of the class – getstats() and showstats(). With information hiding one need not know how actually the data is represented or functions implemented. The program need not know about the changes in the private data and functions. The interface(public) functions take care of this. The OOP methodology is to hide the implementation specific details, thus reducing the complexities involved.

7.3. Classes and Objects As seen earlier, a class is a vehicle to implement the OOP features in the C++ language. Once a class is declared, an object of that type can be defined. An object is said to be a specific instance of a class just like Maruti car is an instance of a vehicle or pigeon is the instance of a bird. Once a class has been defined several objects of that type can be declared. For instance, an object of the class defined above can be declared with the following statement: player Sachin, Dravid, Mohan ; [Or] class player Sachin , Dravid, Mohan ; where Sachin and Dravid are two objects of the class player. Both the objects have their own set of member variables. Once the object is declared, its public members can be accessed using the dot operator with the name of the object. We can also use the variable no_player in the public section with a dot operator in functions other than the functions declared in the public section of the class.


e.g. Sachin.getstats(); Dravid.showstats(); Mohan.no_player = 10;

7.4. Access specifis- Private Public and Protected members Class members can either be declared in public’,’protected’ or in the ‘private’ sections of the class. But as one of the features of OOP is to prevent data from unrestricted access, the data members of the class are normally declared in the private section. The member functions that form the interface between the object and the program are declared in public section ( otherwise these functions can not be called from the program ). The member functions which may have been broken down further or those, which do not form a part of the interface, are declared in the private section of the class. By default all the members of the class are private. The third access specifier ‘protected’ that is not used in the above example, pertains to the member functions of some new class that will be inherited from the base class. As far as non-member functions are concerned, private and protected are one and the same. Summary of Access specifiers • • • private members of a class are accessible only from other members of their same class or from their "friend" classes. protected members are accessible from members of their same class and friend classes, and also from members of their derived classes. Finally, public members are accessible from anywhere the class is visible.

7.5. Member Functions of a Class A member function of the class is same as an ordinary function. Its declaration in a class template must define its return value as well as the list of its arguments. You can declare or define the function in the class specifier itself, in which case it is just like a normal function. But since the functions within the class specifier is considered inline by the compiler we should not define large functions and functions with control structures, iterative statements etc should not be written inside the class specifier. However, the definition of a member function differs from that of an ordinary function if written outside the class specifier. The header of a member function uses the scope operator (::) to specify the class to which it belongs. The syntax is: return_type class_name :: function_name (parameter list) { : } e.g. void player :: getstats (void) { : }


void player :: showstats (void) { : : } This notation indicates that the functions getstats () and showstats() belong to the class player. COMPLETE EXAMPLE OF CLASS: Find the area of the rectangle // 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; } ------------------------------------------------------------OUTPUT rect area: 12 rectb area: 30 7.6. Passing and Returning Objects Objects can be passed to a function and returned back just like normal variables. When an object is passed by content , the compiler creates another object as a formal variable in the called function and copies all the data members from the actual variable to it. Objects can also be passed by address, which will be discussed later.


e.g. class check { public : check add(check); void get() { cin >> a; } void put() { cout << a; } private : int a; }; void main() { check c1,c2,c3; c1.get(); c2.get(); c3 = c1.add(c2); c3.put(); } check check :: add ( check c2) { check temp; temp.a = a + c2.a; return ( temp); } The above example creates three objects of class check. It adds the member variable of two classes, the invoking class c1 and the object that is passed to the function , c2 and returns the result to another object c3. You can also notice that in the class add() the variable of the object c1 is just referred as ‘a’ where as the member of the object passed .i.e. c2 is referred as ‘c2.a’ . This is because the member function will be pointed by the pointer named this in the compiler where as what we pass should be accessed by the extraction operator ‘.’. we may pass more than one object and also normal variable. we can return an object or a normal variable from the function. We have made use of a temporary object in the function add() in order to facilitate return of the object.


7.7. Pointers to Objects Passing and returning of objects is, however, not very efficient since it involves passing and returning a copy of the data members. This problem can be eliminated using pointers. Like other variables, objects of class can also have pointers. Declaring a pointer to an object of a particular class is same as declaring a pointer to a variable of any other data type. A pointer variable containing the address of an object is said to be pointing to that object. Pointers to objects can be used to make a call by address or for dynamic memory allocation. Just like structure pointer, a pointer to an object also uses the arrow operator to access its members. Like pointers to other data types, a pointer to an object also has only one word of memory. It has to be made to point to an already existing object or allocated memory using the keyword ‘new’. e.g. string str; string *sp; // Object // Pointer to an object

sp = &str; // Assigns address of an existing object sp = new string // Allocates memory with new.

It is perfectly valid to create pointers pointing to objects, 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. 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 objects 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]; a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56


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; } 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

7.8. Array of Objects As seen earlier, a class is a template, which can contain data items as well as member functions to operate on the data items. Several objects of the class can also be declared and used. Also, an array of objects can be declared and used just like an array of any other data type. An example will demonstrate the use of array of objects. e.g. class student { public : void getdetails(); void printdetails(); private : int rollno; char name[25]; int marks[6]; float percent; }; void student :: getdetails()


{ int ctr,total; cout << ”enter rollno”; cin >> rollno ;

cout << ”enter name”; cin >> name; cout << ” enter 6 marks “ ; for( ctr = 1 ;ctr <= 6 ; ctr++ ) { cin >> marks[ctr]; total = total + marks[ctr]; } percent = total / 6; } void student :: printdetails () { cout << rollno << name << percent ; } void main() { student records[50]; int x=0; cout << “ How many students “; cin >> x; for ( int i =1; i<= x; i++) { records[i].getdeatils(); } for ( int i =1; i<= x; i++) { records[i].printdeatils(); } } As can be seen above, an array of objects is declared just like any other array. Members of the class are accessed, using the array name qualified by a subscript. The statement,


records[y].printdetails(); Invokes the member funs printdetails() for the object given by the subscript y. For different values of subscript, it invokes the same member function, but for different objects.

7.9 The Special Pointer ‘this’ When several instances of a class come into existence, it naturally follows that each instance has its own copy of member variables. If this were not the case, then for obvious reasons it would be impossible to create more than one instance of the class. On the other hand, even though the class member functions are encapsulated with the data members inside the class definition, it would be very inefficient in terms of memory usage to replicate all these member functions and store the code for them within each instance. Consequently, only one copy of each member function per class is stored in memory, and must be shared by all of the instances of the class. But this poses a big problem for the compiler: How can any given member function of a class knows which instance it is supposed to be working on ? In other words, up to now in a class member function you have simply been referring to the members directly without regard to the fact that when the instantiations occur each data member will have a different memory address. In other words, all the compiler knows is the offset of each data member from the start of the class. The solution to this dilemma is that, in point of fact, each member function does have access to a pointer variable that points to the instance being manipulated. Fortunately this pointer is supplied to each member function automatically when the function is called, so that this burden is not placed upon the programmer. This pointer variable has a special name ‘this’ (reserved word). Even though the this pointer is implicitly declared, you always have access to it and may use the variable name anywhere you seem appropriate. e.g. class try_this { public : void print(); try_this add(int); private : int ivar; };


void print() { cout << ivar; cout << this -> ivar ; } The function print refers to the member variable ivar directly. Also, an explicit reference is made using the this pointer. This special pointer is generally used to return the object, which invoked the member function. For example, void main() { try_this t1,t2; t2 = t1.add(3); t2.print(); } try_this try_this :: add(int v) { ivar = ivar + v; return ( *this); } In the above example if ivar for t1 is 10 and value in v is 2, then the function add() adds them and ivar for t1 becomes 12 . We want to store this in another object t2, which can be done by returning the object t1 using *this to t2. The result of t2.print() now will be 12.

Dereferencing the Pointer this Sometimes a member function needs to make a copy of the invoking instance so that it can modify the copy without affecting the original instance. This can be done as follows : try_this temp(*this); try_this temp = *this ;

In OOP emphasis is on how the program represents data. It is a design concept with less emphasis on operational aspects of the program. The primary concepts of OOP is implemented using class and objects. A class contains data members as well as function members. The access specifiers control the access of data members. Only the public members of the class can access the data members declared in private section. Once class has been defined, many objects of that class can be declared. Data members of different objects of the same class occupy different memory area but function members of different objects of the same class share the same set of functions. This is possible because of the internal pointer ‘*this’ which keeps track of which function is invoked by which object.


7.10 Self test Exercises: 1. Define a class to model a banking system. The function members should allow initializing the data members, a query to facilitate for account and a facility to deposit and withdraw from the account. WAP to implement the same. Create a class called Time that has separate int member data for hours, minutes and seconds. Write functions for accepting time and displaying time.




Contents 8.1

8.2 8.3 8.4

Constructors 8.1.1 Syntax rules for writing constructor functions 8.1.2 Different ways of calling contructor Overloading Constructors Destructor Self test

Since C++ supports the concept of user-defined classes and the subsequent initiations of these classes, it is important that initialization of these instantiations be performed so that the state of any object does not reflect “ garbage”. One of the principles of C++ is that objects know how to initialize and cleanup after themselves. This automatic initialization and clean up is accomplished by two member functions – the constructor and the destructor.

8.1 Constructors By definition, a constructor function of some class is a member function that automatically gets executed whenever an instance of the class to which the constructor belongs comes into existence. The execution of such a function guarantees that the instance variables of the class will be initialized properly. A constructor function is unique from all other functions in a class because it is not called using some instance of the class, but is invoked whenever we create an object of that class. A constructor may be overloaded to accommodate many different forms of initialization for instances of the class. i.e. for a single class many constructors can be written with different argument lists .

8.1.1 Syntax rules for writing constructor functions • • • • Its name must be same as that of the class to which it belongs. It is declared with no return type (not even void). However, it will implicitly return a temporary copy of the instance itself that is being created. It cannot be declared static (a function which does not belong to a particular instance), const( in which you can not make changes). It should have public or protected access within the class. Only in very rare circumstances the programmers declare it in private section.



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; } . 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); rect area: 12 rectb area: 30

8.1.2 Different ways of calling contructor

There are basically three ways of calling constructor. The first way to call the constructor is explicitly as : CRectangle rect = CRectangle (3,4); This statement creates an object with the name bigbox and initializes the data members with the parameters passed to the constructor function. The above object can also be created with an implicit call to the constructor : CRectangle rect (3,4); Both the statements given above are equivalent. Yet, another way of creating and initializing an object is by direct assignment of the data item to the object name. But, this approach works if there is only one data item in the class. This is obvious because we cannot assign more than one value at a time to a variable.


e.g. class counter { public : counter ( int c) // constructor. { count = c; }; private : int count; };

we can now create an object as, counter cnt = 0; In the above example , object cnt is initialized by a value zero at the time of declaration. This value is actually assigned to its data member count. This is the third way to initialize an object’s data member. Thus, all the following statements to initialize the objects of the class counter are equivalent: counter c1(20); counter c1 = counter(30); counter c1= 10;

8.2 Overloading Constructors Like any other function, a constructor can also be overloaded with several functions that have the same name but different types or numbers 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 non static 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; } It is important to realize 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 must 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; } 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. rect area: 12 rectb area: 25


Notice that if we declare a new object and we do not want to pass parameters to it we do not include parentheses (): CRectangle rectb; // right CRectangle rectb(); // wrong!

8.3 Destructors The Destructor fulfills the opposite functionality. It 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 it is released using operator delete. A destructor function gets executed whenever an instance of the class to which it belongs goes out of existence. The primary usage of a destructor function is to release memory space that the instance currently has reserved. Syntax rules for writing a destructor function • • • • • Its name is the same as that of the class to which it belongs, except that the first character of the name is the symbol tilde ( ~ ). It is declared with no return type ( not even void ) since it cannot ever return a value. It cannot be static, const or volatile. It takes no input arguments , and therefore cannot be overloaded. It should have public access in class declaration.

Generally the destructor cannot be called explicitly (directly) from the program. The compiler generates a class to destructor when the object expires. Class destructor is normally used to clean up the mess from an object. Class destructors become extremely necessary when class constructor use the new operator, otherwise it can be given as an empty function. However, the destructor function may be called explicitly allowing you to release the memory not required and allocate this memory to new resources, in Borland C++ version 3.1. Eg class employee { public : employee() { } ~employee(); { } }; // example on constructors and destructors


#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; } 8.4 1. Self test Create a class called Time that has a separate data members for day, month and year. A constructor should be used to initialize these members. Then write a function to add these dates and store the result in a third object and display it. WAP to add co-ordinates of the plane. The class contains x and y co-ordinates. Create three objects. Use a constructor to pass one pair of co-ordinates and a function to accept the second pair. Add these variables of two objects and store the result in the third.




Contents 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8

Function overloading Precautions to be taken while overloading functions. Static Class Members Static Member Functions Friend Functions Friend for Overloading Operators Granting friendship to another class Granting friendship to a member function of another class

9.1 Function Overloading Function overloading is a form of polymorphism. Function overloading facilitates defining one function having many forms. In other words it facilitates defining several functions with the same name, thus overloading the function names. Like in operator overloading, even here, the compiler uses context to determine which definition of an overloaded function is to be invoked. Function overloading is used to define a set of functions that essentially, do the same thing, but use different argument lists. The argument list of the function is also called as the function’s signature. You can overload the function only if their signatures are different. The differences can be 1. In number of arguments, 2. Data types of the arguments, 3. Order of arguments, if the number and data types of the arguments are same. e.g. int add( int, int ); int add( int, int, int ); float add( float, float ); float add( int, float, int ); float add(int,int,float);

The compiler cannot distinguish if the signature is same and only return type is different. Hence, it is a must, that their signature is different. The following functions therefore raise a compilation error. e.g.


float add( int, float, int ); int add( int, float, int );

Consider the following 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); cout << "\n"; return 0; } 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 one to call in each case by 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 functions with the same name but with completely different behaviors. 2 2.5

9.2 Precautions to be taken while overloading function Function overloading is a boon to designers, since different names for similar functions need not be thought of, which often is a cumbersome process given that many times people run out of names. But, this facility should not be overused, lest it becomes an overhead in terms of readability and maintenance. Only those functions, which basically do the same task, on different sets of data, should be overloaded.


We have already seen the powerful features of OOP that make C++ such a strong language. In this session we will continue exploring some other features, which make this language more powerful.

9.3 Static Class Members As we already know all the objects of the class have different data members but invoke the same member functions. However, there is an exception to this rule. If the data member is declared with the keyword static, then only one such data item is created for the entire class, no matter how many objects it has. Static data members are useful, if all objects of a class must share a common data item. Whereas, the visibility of this data item remains same, the duration of this variable is for entire lifetime of the program. For example, such a variable can be particularly useful if an object requires to know how many objects of its kind exist. class counter { public : counter (); int getcount(); private: static int count; }; counter::counter () { count++; } int counter::getcount() { return ( count ); } int counter :: count = 0;// INITIALIZATION OF STATIC MEMBER. void main() { counter c1,c2; cout << “ Count = “ << c1.getcount() << endl; cout << “ Count = “ << c2.getcount() << endl; counter c3; cout << “ Count = “ << c3.getcount() << endl; counter c4,c5;


cout << “ Count = “ << c4.getcount() << endl; cout << “ Count = “ << c5.getcount() << endl; }

Output: Count Count Count Count Count = = = = = 2 2 3 5 5 // not 1 because 2 objects are already created.

In the above example, the class counter demonstrates the use of static data members. It contains just one data member count. Notice the initialization of this static class member. For some compilers it is mandatory. Even though the data member is in the private section it can be accessed in this case as a global variable directly, but has to be preceded by the name of the class and the scope resolution operator. This example contains a constructor to increment this variable. Similarly, there can be a destructor to decrement it. You can use these to generate register numbers for student objects from a student class. Whenever an object is created, he will be automatically assigned a register number if the register number is a static variable and a constructor is used to write an equation to generate separate register numbers in some order. You could initialize the variable to give the first register number and then use this in the constructor for further operations.

9.4 Static Member Functions:All the objects of the class share static data members of a class. The example above demonstrates how to keep track of all the objects of a class which are4 in existence. However, this function uses existing objects to invoke a member function getcount(), which returns the value of the static data member. What if the programme does not want to use objects to invoke this function and still the programme would like to know how many objects have been created? If there is no object how the member function is invoked? Further, as can be seen from the previous output, the number of objects (count) remains same at a given instance no matter which object is used to invoke the member function. In fact, the use of existing objects, like in the above example, is not an effective way to access the value of the static data member. A specific object should not be used to refer to this member, since it does not belong to that object; it belongs to the entire class. C++ gives a facility to define static function members, for the same. That is, to invoke such a function, an object is not required. It can be invoked with the name of the class. The programme given below illustrates its use. class counter { public :


counter (); static int getcount(); private: static int count;

}; counter::counter () { count++; } int counter::getcount() { } int counter :: count = 0; // INITIALIZATION OF STATIC MEMBER. void main() { counter c1,c2; cout << “ Count = “ << counter :: getcount() << endl; cout << “ Count = “ << counter :: getcount() << endl; counter c3; cout << “ Count = “ << counter :: getcount() << endl; counter c4,c5; cout << “ Count = “ << counter :: getcount() << endl; cout << “ Count = “ << counter :: getcount() << endl; } Output: Count Count Count Count Count = = = = = 2 2 3 5 5


9.5 Friend Functions One of the main features of OOP is information hiding. A class encapsulates data and methods to operate on that data in a single unit . The data from the class can be accessed only through member functions of the class. This restricted access not only hides the implementation details of the methods and the data structure, it also saves the data from any possible misuse, accidental or otherwise. However, the concept of data encapsulation sometimes takes information hiding too far. There are situations where a rigid and controlled access leads to inconvenience and hardships. For instance, consider a case where a function is required to operate on object of two different classes. This function cannot be made a member of both the classes. What can be done is that a function can be defined outside both the classes and made to operate on both. That is, a function not belonging to either, but able to access both. Such a function is called as a friend function. In other words, a friend function is a nonmember function, which has access to a class’s private members. It is like allowing access to one’s personal belongings to a friend. Using a friend function is quite simple. The following example defines a friend function to access members of two classes. class Bclass; class Aclass { public : Aclass(int v) { Avar = v; } friend int addup(Aclass &ac, Bclass &bc); private : int Avar; }; class Bclass { public : Bclass(int v) { Bvar = v; } friend int addup(Aclass &ac, Bclass &bc); private : int Bvar; }; // Forward Declaration


int addup(Aclass &ac, Bclass &bc) { return( ac.Avar + bc.Bvar); }

void main() { Aclass aobj; Bclass bobj; int total; total = addup(aobj,bobj); }

The program defines two classes- Aclass and Bclass. It also has constructors for these classes. A friend function, addup(), is declared in the definition of both the classes, although it does not belong to either. This friend function returns the sum of the two private members of the classes. Notice, that this function can directly access the private members of the classes. To access the private members, the name of the member has to be prefixed with the name of the object , along with the dot operator. The first line in the program is called forward declaration. This is required to inform the compiler that the definition for class Bclass comes after class Aclass and therefore it will not show any error on encountering the object of Bclass in the declaration of the friend function. Since it does not belong to both the classes , while defining it outside we do not give any scope resolution and we do not invoke it with the help of any object. Also , the keyword friend is just written before the declaration and not used while defining. Sometimes friend functions can be avoided using inheritance and they are preferred. Excessive use of friend over many classes suggests a poorly designed program structure. But sometimes they are unavoidable. 9.6 Friend for Overloading Operators Some times friend functions cannot be avoided. For instance with the operator overloading. Consider the following class that contains data members to simulate a matrix. Several operations can be performed on the matrices. One of them is to multiply the given matrix by a number( constant literal). There are two ways in which we can do this. The two ways are : Matrix * num; [or] num * Matrix;


In the first case we can overload * to perform the operation and an object invokes this as the statement gets converted to : Mobj.operator*(num); Where Mobj is an object of Matrix and num is a normal integer variable. What happens to the second one ? It gets converted by the compiler as : num.operator*(Mobj); Let us see this program in detail. class Matrix { public: : : Matrix &operator*(int num); friend Matrix &operator*(int n, Matrix &m); private: int mat[20][20]; int rows, cols; } Matrix Matrix::operator*(int num) { Matrix temp; temp.rows=rows; temp.cols=cols; for(int i=1; i<=rows; i++) for(int j=1; j<=cols; j++) temp.mat[i][j]=mat[i][j]*num; return (temp); } Matrix operator*(int n, Matrix &m) { Matrix temp; temp= m*n; return temp; } void main() { Matrix M1, M2, M3; int num; : : // accept matrix one and num


M2=M1*num; // calls member operator function. M3=num*M1; // calls friend function. } Here when the compiler comes across the multiplication of an object by a number it invokes the operator member function. When is encountered before multiplication symbol as in the second call in the program, the compiler calls friend function . in friend function we have just reversed the arguments, which causes the member function to be invoked . Intelligent use of friend functions makes the code more readable. 9.7 Granting friendship to another class:To grant friendship to another class, write the keyword followed by the class name. The keyword class is optional. Note that this declaration also implies a forward declaration of the class to which the friendship is being granted. The implication of this declaration is that all off the member functions of the friend class are friend functions of the class that bestows the friendship. e.g. class Aclass { public : : : : friend class Bclass; // Friend declaration. private : int Avar; }; class Bclass { public : : : void fn1(Aclass ac) { Bvar = ac. Avar; // Avar can be accessed. } private : int Bvar; }; void main() { Aclass aobj;


Bclass bobj; Bobj,fn1(aobj); } The program declares class Bclass to be a friend of Aclass. It means that all member functions of Bclass have been granted direct access to all member functions of Aclass. 9.8 Granting friendship to a member function of another class If you want class A to grant friendship to one or more individual member functions of class B, then you must code the classes and their member functions in this manner: • • • Forward declare class A; Define class B and declare (not define) the member functions: Define class A in which you declare the friendship for the member functions of class B. Of course, you must qualify the names of these functions using the class name B and the scope resolution operator. Define the member functions of class B;

• e.g.

class Aclass class Bclass { public : : : void fn1(); // Can’t define here

void fn3() { : : } private : int Bvar; }; class Aclass { public : :



void fn1() { : } friend classB:: fn1(); friend classB:: fn2(); private : int Avar; }; void classB:: fn1() { Bvar = Avar; } void classB:: fn2() { Bvar = variable +25; }



Contents 10.1 10.2 10.3 10.4 10.5 10.6 10.7

10.8 10.9

Introduction to Operator Overloading. Operator Overloading Fundamentals. Implementing the operator functions. Rules for overloading the operators. Pointer oddities (assignment) and Operator Overloading. Overloading the Extraction and Insertion Operators Conversion functions. 10.7.1 Conversion from basic to user-defined variable. 10.7.2 Conversion from User-Defined to Basic data type 10.7.3 Conversion Between Objects of Different Classes 10.7.4 Conversion function in the Destination Class Table for Type Conversions Self Test

10.1 Introduction to Operator Overloading All computer languages have built in types like integers, real numbers, characters and so on. Some languages allow us to create our own data types like dates, complex numbers, co-ordinates of a point. Operations like addition, comparisons can be done only on basic data types and not on derived (user-defined) data types. If we want to operate on them we must write functions like compare (), add (). e.g. if (compare (v1, v2) = = 0) : : Where v1 and v2 are variables of the new data type and compare () is a function that will contain actual comparison instructions for comparing their member variables. However, the concept of Operator Overloading, in C++, allows a statement like if (v1 = = v2) : : Where the operation of comparing them is defined in a member function and associated with comparison operator(==).


The ability to create new data types, on which direct operations can be performed is called as extensibility and the ability to associate an existing operator with a member function and use it with the objects of its class, as its operands, is called as Operator Overloading. Operator Overloading is one form of Polymorphism ,an important feature of object-oriented programming .Polymorphism means one thing having many forms, i.e. here an operator can be overloaded to perform different operations on different data types on different contexts. Operator Overloading is also called operational polymorphism. Another form of polymorphism is function overloading.

10.2 Operator Overloading Fundamentals The C language uses the concept of Operator Overloading Discreetly. The asterisk (*) is used as multiplication operator as well as indirection (pointer) operator. The ampersand (&) is used as address operator and also as the bitwise logical ‘AND’ operator. The compiler decides what operation is to be performed by the context in which the operator is used. Thus, the C language has been using Operator Overloading internally. Now, C++ has made this facility public. C++ can overload existing operators with some other operations. If the operator is not used in the context as defined by the language, then the overloaded operation, if defined will be carried out. For example, in the statement x = y + z; If x, y and z are integer variables, then the compiler knows the operation to be performed. But, if they are objects of some class, then the compiler will carry out the instructions, which will be written for that operator in the class.

10.3 Implementing Operator Functions The general format of the Operator function is: return_type operator op ( argument list ); Where op is the symbol for the operator being overloaded. Op has to be a valid C++ operator, a new symbol cannot be used. e.g. Let us consider an example where we overload unary arithmetic operator ‘++’. class Counter { public : Counter();


void operator++(void); private : int Count; };

Counter::Counter() { Count = 0; } void Counter::operator++(void) { ++ Count ; } void main() { Counter c1; c1++; ++c1; } In main() the increment operator is applied to a specific object. The function itself does not take any arguments. It increments the data member Count. Similarly, to decrement the Counter object can also be coded in the class definition as: void operator--(void) { -- Count ; } and invoked with the statement --c1; or c1--; In the above example , the compiler checks if the operator is overloaded and if an operator function is found in the class description of the object, then the statement to increment gets converted, by the compiler, to the following: c1.operator++(); This is just like a normal function call qualified by the object’s name. It has some special characters ( ++) in it. Once this conversion takes place, the compiler treats it just like any other member function from the class. Hence, it can be seen that such a facility is not a very big overhead on the compiler. // increments Count to 1 // increments Count to 2


However, the operator function in the above example has a potential glitch. On overloading , it does not work exactly like it does for the basic data types. With the increment and decrement operators overloaded, the operator function is executed first, regardless of whether the operator is postfix or prefix. If we want to assign values to another object in main() we have to return values to the calling function. Counter Counter :: operator++(void) { Counter temp; temp.Count = ++ Count ; return ( temp ); } void main() { Counter c1,c2; c1 = c2 ++; } //increments to 1, then assigns.

In this example , the operator function creates a new object temp of the class Counter, assigns the incremented value of Count to the data member of temp and returns the new object. This object is returned to main(). We can do this in another way by creating a nameless temporary object and return it. class Counter { public : Counter(); // CONSTRUCTOR WITHOUT ARGUMENTS Counter( int c); // CONSTRUCTOR WITH 1 ARGUMENT Counter operator++(void); private : int Count; }; Counter::Counter() // CONSTRUCTOR WITHOUT ARGUMENTS { Count = 0; } Counter::Counter( int c) // CONSTRUCTOR WITH 1 ARGUMENT { Count = c;


} Counter Counter::operator++(void) { ++ Count ; return Counter(Count); } One change we can see is a constructor with one argument. No new temporary object is explicitly created. However return statement creates an unnamed temporary object of the class Counter initializes it with the value in Count and returns the newly created object. Hence one argument constructor is required. Yet another way of returning an object from the member function is by using the this pointer. This special pointer points to the object, which invokes the function. The constructor with one argument is not required in this approach. Counter Counter :: operator++(void) { ++ Count ; return ( * this); } Consider a class COMPLEX for Complex numbers. It will have a real and an imaginary member variable. Here we can see binary operator overloaded and also how to return values from the functions. class COMPLEX { public: COMPLEX operator+(COMPLEX); private: int real, imaginary; }; Suppose that C1, C2 and C3 are objects of this class. Symbolically addition can be carried out as C3 = C1 + C2; The actual instructions of the operator are written in a special member function. e.g. COMPLEX COMPLEX :: operator+( COMPLEX C2) { COMPLEX temp; temp.real = real + C2.real;


temp.imaginary = imaginary + C2. imaginary; return (temp); } The above example shows how Operator Overloading is implemented. It overloads “+” operator to perform addition on objects of COMPLEX class. Here we have overloaded a binary operator(+).

10.4 Rules for overloading the operators This summarizes the most important points you need to know in order to do operator function overloading. • • The only operators you may overload are the ones from the C++ list and not all of those are available. You cannot arbitrarily choose a new symbol (such as @) and attempt to “overload it.

Start by declaring a function in the normal function fashion, but for the function name use the expression: Operator op Where op is the operator to be overloaded. You may leave one or more spaces before op. • The pre-defined precedence rules cannot be changed. i.e. you cannot, for example, make binary ‘+’ have higher precedence than binary ‘*’. In addition, you cannot change the associativity of the operators.

The unary operators that you may overload are: -> indirect member operator ! not & address * dereference + plus minus ++ prefix increment ++ postfix increment (possible in AT & T version 2.1) -postfix decrement -prefix decrement (possible in AT & T version 2.1) ~ one’s complement

The binary operators that you may overload are: (), [], new, delete, *, / , %, + , - , <<,>>, <, <=, >, >=, ==,! =, &, ^, |, &&, ||, =, *=, /=, %=, +=, -, =, <<=, >>=, &=,! =, ^=, ','(Comma). The operators that can not be overloaded are: . .* direct member direct pointer to member


:: ?: • •

scope resolution ternary

No default arguments are allowed in overloaded operator functions. As with the predefined operators, an overloaded operator may be unary or binary. If it is normally unary, then it cannot be defined to be binary and vice versa. However, if an operator can be both unary and binary, then it can be overloaded either way or both. The operator function for a class may be either a non-static member or global friend function. A non-static member function automatically has one argument implicitly defined, namely the address of the invoking instance (as specified by the pointer variable this). Since a friend function has no this pointer, it needs to have all its arguments explicitly defined).

At least one of the arguments to the overloaded function explicit or implicit must be an instance of the class to which the operator belongs. 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 @a a@ a@b

Operator (@) + - * & ! ~ ++ -++ -+-*/%^&|< > == != <= >= << >> && || , = += -= *= /= %= ^= &= |= <<= >>= [] () ->

Function member A::operator@() A::operator@(int) A::operator@(B)

Global function operator@(A) operator@(A, int) operator@(A, B)




a(b, c...) a->b

A::operator()(B, C...) 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).

Consider an example, which depicts overloading of += (Compound assignment), <, >, == (Equality),!=, + (Concatenation) using String class. class String { public : String (); String ( char str [] );


void putstr(); String operator + (String); String operator += (String s2); int operator < (String s2); int operator > (String s2); int operator == (String s2); int operator != (String s2); private : char s[100]; };

String::String () // CONSTRUCTOR WITH { // NO ARGUMENTS s[0] = 0; };

String:: String( char str [] ) // CONSTRUCTOR WITH { // ONE ARGUMENT strcpy(s,str) }; void String:: putstr()// FUNCTION TO PRINT STRING { cout << s ; }; String String :: operator+(String s2) { String temp; strcpy(temp.s,s); strcat(temp.s,s2.s); return (temp); } String String :: operator+=(String s2) { strcat(s,s2.s); return (*this); }


int String::operator < (String s2) { return (strcmp (s, s2.s ) < 0); } int String::operator > (String s2) { return (strcmp (s, s2.s ) > 0); } int String::operator == (String s2) { return (strcmp (s, s2.s ) == 0); }

int String::operator != (String s2) { return (strcmp (s, s2.s ) != 0); } void main() { String s1 = “welcome “; String s2 = “ to the world of c++”; String s3; cout << endl << “s1 = “; s1.putstr(); cout << endl << “s2 = “; s2.putstr(); s3 = s1 + s2; cout << endl << “ s3 = “; s3.putstr(); String s4; cout <<endl<<” *********************”; s4 = s1 + = s2; cout << endl << “ s4 = “;


s4.putstr(); String s5 = “ Azzzz “; String s6 = “ Apple “; if( s5 < s6 ) { s5.putstr(); cout << ” < ”; s6.putstr(); } else if( s5 > s6 ) { s5.putstr(); cout << ” > ”; s6.putstr(); } else { if( s5 == s6 ) s5.putstr(); cout << ” = ”; s6.putstr(); } else { if( s5 != s6 ) s5.putstr(); cout << ” < ”; s6.putstr(); } }

Output: S1 = welcome S2 = to the world of C++ S3 = welcome to the world of c++ ************************** S4 = welcome to the world of c++ // 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); } int main () { CVector a (3,1); CVector b (1,2); CVector c; c = a + b; cout << c.x << "," << c.y; return 0; }

10.5 Pointer Oddities and Operator Overloading Consider an example, where the data members contain pointers and have been allocated memory using the operator new. In this case using an assignment operator to assign one object to another will result in the pointer variable being copied rather than the contents at the address. The following example explains this problem. class String { public : String (char *s = “”) // CONSTRUCTOR {

size = strlen(s); cptr = new char [size + 1]; strcpy(cptr,s); }; ~String() { delete cptr;


} void putstr() FUNCTION TO PRINT STRING { cout << cptr ; }; private : char *cptr; int size; }; void main() { String s1(“hello students “); String s2; s2 = s1; // Assignment s1.putstr(); s2.putstr(); } The constructor function allocates a string and copies the contents of its formal variable in it. The assignment operator in main() assigns the object s1 to s2. the data members’ cptr and size , of object s1, gets assigned to s2. The output of the program is : hello students hello students Null pointer assignment Why does the program give a ‘null pointer assignment’ message ? After the program is over, the destructor function is called automatically, which releases the memory allocated by new to the data member cptr. But, after the assignment, the data member cptr of both the objects point to the same location in the memory. Thus, the delete operator called for the first object releases the memory and for the second call attempts to release the same memory location again, resulting in the error message. The solution to this problem is to define an operator function for the assignment operator . It can be done as follows: String operator = (String s2) { delete cptr; size = strlen ( s2.cptr ); cptr = new char [size + 1]; strcpy(cptr, s2.cptr); return (*this); //


} On including this member function in the class definition of the above example , the program outputs the following : hello students hello students Hence, the operator function for assignment of an object to another of the same class removes the quirks associated with pointers as data members.

10.6 Overloading the Extraction and Insertion Operators We’ll finish this session by showing how to overload the extraction and insertion operators. Here, you can accept the input and output the results of the user-defined variables or objects just like normal variables. Consider a class Complex that consists of two system defined variables, both float to denote the real and complex part of a complex number. In general cases to accept these member variables, we need to write some function say getval() and invoke it using the object of the class say Comobj.getval() and similar method would be required to display them. Using operator overloading we can accept or display the user-defined object just like a normal variable . i.e. by overloading the extraction operator >> we can accept the complex object as, cin >> comobj; Similarly, by overloading the insertion operator we can display the member variables of the object as, cout << comobj; just as if it were a basic data type. Consider the example :

class Complex { public: friend istream& operator >> (istream &is, Complex &c2) friend ostream& operator << (ostream &os, Complex &c2) private: float real, imaginary; };

istream& operator >> (istream &is, Complex &c2) { cout << “ enter real and imaginary “ << endl;


is >> c2.real >> c2.imaginary; return (is); } istream& operator << (ostream &os, Complex &c2) { os << “ the complex number is “ <<endl; os << c2.real << “+i”<< c2.imaginary; return (os); }

void main() { Complex c1,c2; cin >> c1; cout << c1; cin >> c2; cout << c2; }

The operator functions have to be declared friends , since they have to access the user class and the objects of istream and ostream classes that are system defined. Since these operator functions are friend functions, the two objects – cin and cout are passed as arguments, along with the objects of the user-class. They return the istream and ostream objects so that the operator can be chained. That is the above two input statements can also be written as, cin >> c1 >> c2; cout << c1 << c2;

10.7 Conversion functions Conversion functions are member functions used for the following purposes: 1. 2. 3. Conversion of object to basic data type. Conversion of basic data type to object. Conversion of objects of different classes.

Conversions of one basic data type to another are automatically done by the compiler using its own built-in routines (implicit) and it can be forced using built-in conversion routines (explicit). However, since the compiler does not know anything about user-defined types (classes), the program has to define conversion functions. e.g. int i = 2, j =19;


float f = 3.5; i = f; // i gets the value 3 , implicit conversion f = (float) j; // f gets 19.00, explicit conversion

10.7.1 Conversion from Basic to User-Defined variable Consider the following example. class Distance { public : Distance(void) // Constructor with no { // argument feet = 0; inches = 0.0; }; Distance(float metres) { float f; // Constructor with f = 3.28 * metres; // one argument feet = int(f); // also used for inches = 12 * ( f – feet);// conversion void display(void) { cout << “ Feet = “ << feet <<”,”; cout << “ Inches = “ << inches << endl; };


private : int feet; float inches; };

void main (void) { Distance d1 = 1.25; // Uses 2nd constructor Distance d2; // Uses 1st constructor float m; d2 = 2.0 ; // Uses 2nd constructor

cout << “ 1.25 metres is : “ << d1.showdist() ;


cout << “ 2.0 metres is :“ << d2.showdist(); }

Output : 1.25 metres is :FEET = 4 , INCHES = 1.199999 2.0 metres is :FEET = 6 , INCHES = 6.719999 The above program converts distance in metres ( basic data type) into feet and inches ( members of an object of class Distance ). The declaration of first object d1 uses the second constructor and conversion takes place. However, when the statement encountered is d2 = 2.0; The compiler first checks for an operator function for the assignment operator. If the assignment operator is not overloaded, then it uses the constructor to do the conversion.

10.7.2. Conversion from User-Defined to Basic data type The following program uses the program in the previous section to convert the Distance into metres(float). class Distance { public : Distance(void) // Constructor with no { // argument feet = 0; inches = 0.0; }; Distance(float metres) { float f; // Constructor with f = 3.28 * metres; // one argument feet = int(f); // Also used for = 12 * ( f – feet); //conversion }; operator float(void) // Conversion function { // from Distance to float float f; f = inches / 12; f = f + float (feet); return ( f/ 3.28 ); };



void display(void) { cout << “ Feet = “ << feet <<”,”; cout << “ Inches = “ << inches << endl; }; private : int feet; float inches; };

void main (void) { Distance d1 = 1.25; // Uses 2nd constructor Distance d2; // Uses 1st constructor float m; d2 = 2.0 ; // Uses 2nd constructor

cout << “ 1.25 metres is :“ << d1.showdist (); cout << “ 2.0 metres is :“ << d2.showdist (); cout << “ CONVERTED BACK TO METRES “; m = float ( d1 ); // Calls function explicitly. cout << “ d1 = “ << m; m = d2; // Calls function explicitly. cout << “ d2 = “ << m; }

Output: 1.25 metres is :FEET = 4 ,INCHES = 1.199999 2.0 metres is :FEET = 6 ,INCHES = 6.719999 CONVERTED BACK TO METRES d1 = 1.25 d2 = 2.00

Actually, this conversion function is nothing but overloading the typecast operator float(). The conversion is achieved explicitly and implicitly.


m = float (d1); is forced where as , in the second assignment statement m = d2; first the compiler checks for an operator function for assignment ( = ) operator and if not found it uses the conversion function. The conversion function must not define a return type nor should it have any arguments.

10.7.3 Conversion Between Objects of Different Classes Since the compiler does not know anything about the user-defined type, the conversion instructions are to be specified in a function. The function can be a member function of the source class or a member function of the destination class. We will consider both the cases. Consider a class DistFeet which stores distance in terms of feet and inches and has a constructor to receive these. The second class DistMetres store distance in metres and has a constructor to receive the member. Conversion function in the Source Class class DistFeet { public : DistFeet(void) { feet = 0; inches = 0.0; }; DistFeet(int ft,float in) { feet = ft; inches = in }; void ShowFeet(void) { cout << “ Feet = “ << feet << “,”; cout << “ Inches = “ << inches << endl; }; private : int feet; float inches; }; // Constructor with no // argument


class DistMetres { public: DistMetres(void) { metres = 0 ; // constructor 1. } DistMetres(float m) { metres = m ; // constructor 2. } void ShowMetres(void) { cout << “ Metres = “ << metres << endl; }; operator DistFeet(void) // conversion { // function float ffeet, inches; int ifeet; ffeet = 3.28 * metres; ifeet = int (ffeet); inches = 12 * (ffeet – ifeet); return(DistFeet(inches,ifeet); }; private: float metres; }; void main (void) { DistMetres dm1 = 1.0; DistFeet df1; df1 = dm1 ; // OR df1 = DistFeet(dm1); // Uses conversion function

dm1.ShowMetres(); df1.ShowFeet(); }


In the above example, DistMetres contains a conversion function to convert the distance from DistMetres ( source class), to DistFeet ( destination class). The statement to convert one object to another df1 = dm1; calls the conversion function implicitly. It could also have been called explicitly as df1 = DistFeet(dm1);

10.7.4 Conversion function in the Destination Class class DistMetres { public: DistMetres(void) { metres = 0 ; // Constructor 1. } DistMetres(float m) { metres = m ; // constructor 2. } void ShowMetres(void) { cout << “ Metres = “ << metres << endl; }; float GetMetres(void) { return(metres); } private: float metres; }; class DistFeet { public : DistFeet(void) // Constructor1 with no { // argument feet = 0; inches = 0.0;


}; DistFeet(int ft,float in) { feet = ft; inches = in; }; void ShowFeet(void) { cout << “ Feet = “ << feet << endl; cout << “ Inches = “ << inches << endl; }; DistFeet( DistMetres dm) // Constructor 3 { float ffeet; ffeet = 3.28 * dm.GetMetres(); feet = int (ffeet); inches = 12 * (ffeet – ifeet); }; private : int feet; float inches; }; void main (void) { DistMetres dm1 = 1.0; DistFeet df1;

// Uses 2nd constructor // class DistMetres // Uses 1st constructor // class DistFeet

df1 = dm1 ; dm1.ShowMetres(); df1.ShowFeet(); }

// OR df1 = DistFeet(dm1); // Uses 3rd conversion function

This program works same as previous function. Here constructor is written in the destination class. Also, we can see a new function GetMetres() . The function returns the data member metres of the invoking object. The function is required because the constructor is defined in the DistFeet class and since metres is a private member of the DistMetres class, it cannot be accessed directly in the constructor function in the DistFeet class.


Since you can use any of the above methods, it is strictly a matter of choice which method you choose to implement.

10.8 Table for Type Conversions

Operation Function in Destination Class

Operation Function in Source Class Not Allowed

Basic to class


Class to Basic

Not Allowed

Conversion Function

Class to Class


Conversion Function

This session covered yet another concept of OOP – Polymorphism. It means one thing having many forms. It is very powerful, yet, simple concept which gives the C++ language a facility to redefine itself into a new language. There are two types of Polymorphism- operational and functional. This session covered operational polymorphism also called as operator overloading. We will see function overloading in future.

10.9 Self Test 1. 2. WAP to add 2 complex number using OOT(Operator Overloading techniques). WAP to add 2 times using OOT and display the resultant time in watch format.

3. WAP to add, subtract and multiply 2 matrices using OOT. Sort an array of objects. Each object has a string as a member variable. Overload >= or <= operators to compare the two strings. { make use of constructors and destructors whenever possible } 4. WAP to create a class called DATE . Accept 2 valid dates in the form of dd/mm/yyyy. Implement the following by overloading the operators – and + . Display the result after every operation. no_of_dasy = d1 – d2, where d1 and d2 are DATE objects; d1 > = d2 ; and no_of_days is an integer. b) d1 = d1 + no_of_days - where d1 is a DATE object and no_of_days is an integer. a)



Modify the matrix program (program 3) slightly. Overload == operator to compare 2 matrices to be added or subtracted. i.e., whether the column of first and the row of second matrix are same or not. if(m1==m2) { m3=m1+m2; m4=m1-m2; } else display error;

6. WAP to concatenate 2 strings by using a copy constructor. ------------ --------------- ---------------- --------------



Contents 11.1 11.2 Reusability. Inheritance concept- single inheritance. 11.2.1 Private derivation 11.2.2 Public derivation 11.2.3 The Protected Access 11.2.4 Summary of derivation 11.2.5 Table of derivation and access specifiers Using the derived class Constructor and destructor in derived class. Object initialization and conversion. Nested classes (Container classes). Multilevel inheritance. Multiple inheritance. Hybrid Inheritance. Virtual base class.

11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10

Object-oriented programming as seen in the preceding sessions emphasizes the data, rather than emphasizing algorithms. The previous sessions covered OOP features like extensibility, data encapsulation, information hiding, functional polymorphism and operational polymorphism. OOP, however, has more jargon associated to it, like reusability, inheritance. This session covers reusability and inheritance.

11.1 Reusability Reusability means reusing code written earlier ,may be from some previous project or from the library. Reusing old code not only saves development time, but also saves the testing and debugging time. It is better to use existing code, which has been time-tested rather than reinvent it. Moreover, writing new code may introduce bugs in the program. Code, written and tested earlier, may relieve a programmer of the nitty-gritty. Details about the hardware, user-interface, files and so on. It leaves the programmer more time to concentrate on the overall logistics of the program.

What is inheritance? Class, the vehicle, which is used to implement object-oriented concepts in C++, has given a new dimension to this idea of reusability. Many vendors now offer libraries of classes. A class library consists of data and methods to operate on that data, encapsulated in a class . The source


code of these libraries need not be available to modify them. The new dimension of OOP uses a method called inheritance to modify a class to suit one’s needs. Inheritance means deriving new classes from the old ones. The old class is called the base class or parent class or super class and the class which is derived from this base class is called as derived class or child class or sub class. Deriving a new class from an existing one , allows redefining a member function of the base class and also adding new members to the derived class . and this is possible without having the source of the course definition also. In other words, a derived class not only inherits all properties of the base class but also has some refinements of its own. The base class remains unchanged in the process. In other words, the derived class “is a “ type of base class, but with more details added. For this reason, the relationship between a derived class and its base class is called an “is-a” relationship.

Class A

Class B Single Inheritance Here class A is a base class and the class B is the derived class.

How to define a derived class ? A singly inherited derived class id defined by writing : • • • • • • e.g. class A { public : int public_A; void public_function_A(); private : int pri_A; void private_function_A(); The keyword class. The name of the derived class . A single colon (:). The type of derivation ( private , protected, or public ). The name of the base, or parent class. The remainder of the class definition.


protected : int protected_A; void protected_function_A(); }; class B : private A { public : int public_B; void public_function_B(); private : int pri_B; void private_function_B(); }; class C : public A { public : int public_C; void public_function_C(); private : int pri_C; void private_function_C(); }; class D : protected A { public : int public_D; void public_function_D(); private : int pri_D; void private_function_D(); };

A derived class always contains all of the member members from its base class . you cannot “subtract” anything from a base class. However, accessing the inherited variables is a different matter. It is also important to understand the privileges that the derived class has insofar as access to members of its base class are concerned. In other words, just because you happen to derive a class does not mean that you are automatically granted complete and unlimited access privileges to the members of the base class. to understand this you must look at the different types of derivation and the effect of each one.


11.2.1. Private derivation If no specific derivation is listed, then a private derivation is assumed. If a new class is derived privately from its parent class , then : • The private members inherited from its base class are inaccessible to new member functions in the derived class . this means that the creator of the base class has absolute control over the accessibility of these members , and there is no way that you can override this. The public members inherited from the base class have private access privilege. In other words, they are treated as though they were declared as new private members of the derived class, so that new member functions can access them. However, if another private derivation occurs from this derived class, then these members are inaccessible to new member functions.

e.g. class base { private : int number; }; class derived : private base { public : void f() { ++number; } }; The compiler error message is ‘ base :: number ‘ is not accessible in the function derived :: f();

// Private base member not accessible

e.g. class base { public : int number; }; class derived : private base {


public : void f() { ++number; } }; class derived2 : private derived { public : void g() { ++number; } }; The compiler error message is ‘ base :: number ‘ is not accessible in the function derived2 :: g(); Since public members of a base class are inherited as private in the derived class, the function derived :: f() has no problem accessing it . however, when another class is derived from the class derived , this new class inherits number but cannot access it. Of course, if derived1::g() were to call upon derived::f(), there is no problem since derived::f() is public and inherited into derived2 as private. i.e. In derived2 we can write, void g() { f(); } or there is another way. Writing access declaration does this. // Access to number O.K.

// Access to number is prohibited.

class base { public : int number; }; class derived : private base { public : base :: number ; void f() {


++number; } }; class derived2 : private derived { public : void g() { ++number; } };

// Access to number O.K.

// Access to number O.K

As you have just seen private derivations are very restrictive in terms of accessibility of the base class members . therefore, this type of derivation is rarely used.

11.2.2 Public derivation Public derivations are much more common than private derivations. In this situation : • • The private members inherited from the base class are inaccessible to new members functions in the derived class. The public members inherited from the base class may be accessed by new members functions in the derived class and by instances of the derived class .

e.g. class base { private : int number; };

class derived : public base { public :

void f() { ++number; } }; // Private base member not accessible


The compiller error message is ‘ base :: number ‘ is not accessible in the function derived::f(); Here, only if the number is public then you can access it. Note : However example 2 and 3 in the above section works here if you derive them as “public”.

11.2.3 The Protected Access In the preceding example, declaring the data member number as private is much too restrictive because clearly new members function in the derived class need to gain access to it and in order to perform some useful work. To solve this dilemma, the C++ syntax provides another class access specification called protected . here is how protected works : • In a private derivation the protected members inherited from the base class have private access privileges. Therefore, new member functions and friend of the derived class may access them. In a public derivation the protected members inherited from the base class retain their protected status. They may be accessed by new members function and friends of the derived class .

In both situations the new members functions and friends of the derived class have unrestricted access to protected members . however, as the instances of the derived class are concerned, protected and private are one and the same, so that direct access id always denied. Thus, you can see that the new category of protected provides a middle ground between public and private by granting access to new function and friends of the derived class while still blocking out access to non-derived class members and friend functions .

class base { protected : int number; }; class derived : public base { public : void f() { ++number; } };

// base member access O.K.


Protected derivation In addition to doing private and public derivations, you may also do a protected derivation. In this situation : • The private members inherited from the base class are inaccessible to new member functions in the derived class. ( this is exactly same as if a private or public derivation has occurred.) The protected members inherited from the base class have protected access privilege. The public members inherited from the base class have protected have protected access.

• •

Thus , the only difference between a public and a protected derivation is how the public members of the parent class are inherited. It is unlikely that you will ever have occasion to do this type of derivation.

Summary of access privileges 1. If the designer of the base class wants no one, not even a derived class to access a member , then that member should be made private . 2. If the designer wants any derived class function to have access to it, then that member must be protected. 3. if the designer wants to let everyone , including the instances, have access to that member , then that member should be made public .

11.2.4 Summary of derivations 1. Regardless of the type of derivation, private members are inherited by the derived class , but cannot be accessed by the new member function of the derived class , and certainly not by the instances of the derived class . 2. In a private derivation, the derived class inherits public and protected members as private . a new members function can access these members, but instances of the derived class may not. Also any members of subsequently derived classes may not gain access to these members because of the first rule. 3. In public derivation, the derived class inherits public members as public , and protected as protected . a new member function of the derived class may access the public and protected members of the base class ,but instances of the derived class may access only the public members. 4. In a protected derivation, the derived class inherits public and protected members as protected .a new members function of the derived class may access the public and protected members of the base class, both instances of the derived class may access only the public members .

11.2.5 Table of Derivation and access specifiers


Derivation Type

Base Class Member

Access in Derived Class

Private Private Public Protected Private Public Public Protected Private Protected Public Protected

(inaccessible ) Private Private (inaccessible ) Public Protected (inaccessible ) Protected Protected

We can summarize the different access types according to whom can access them in the following way: Access members of the same class members of derived classes Not-members public yes yes yes protected yes yes no private yes no no

where "not-members" represent any reference from outside the class, such as from main(), from another class or from any function, either global or local. 11.3. Using the Derived Class An instance of a derived class has complete access to the public members of the base class . assuming that the same name does not exist within the scope of the derived class , the members from the base class will automatically be used. Because there is no ambiguity involved in this situation, you do not need to use scope resolution operator to refer to this base class member. class base { public : base(int n = 0) { number = n; } int get_number(); protected : int number; };


int base :: get_number() { return number; } class derived : public base { };

void main() { derived d; // First checks class derived , then class base cout << d.get_number(); // Goes directly to class base cout<< d.base ::get_number(); } Output: 0 0. 11.4 Constructor and destructor in derived class. What is inherited from the base class? In principle every member of a base class is inherited by a derived class except: • • • Constructor and destructor operator=() member friends

Although the constructor and destructor of the base class are not inherited, the default constructor (i.e. constructor with no parameters) and the destructor of the base class are always called when a new object of a derived class is created or destroyed. If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class: derived_class_name (parameters) : base_class_name (parameters) {} For example: // constructors and derivated classes #include <iostream.h> mother: no parameters daughter: int parameter


class mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } }; class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } }; class son : public mother { public: son (int a) : mother (a) { cout << "son: int parameter\n\n"; } }; int main () { daughter cynthia (1); son daniel(1); return 0; }

mother: int parameter son: int parameter

Observe the difference between which mother's constructor is called when a new daughter object is created and which when it is a son object. The difference is because the constructor declaration of daughter and son: daughter (int a) // nothing specified: call default constructor son (int a) : mother (a) // constructor specified: call this one

11.5 Object Initialization and conversion Initialization An object of a derived class can be initialized to an object of a base class . If both the classes have same data members , then no specific constructor needs to be defined in the derived class . It uses the constructor of the base class . An object of a base class can be assigned to the object of the derived class , if the derived class doesn’t contain any additional data members . However, if it does , then the assignment operator will have to be overloaded for the same.



Just like initialization , conversions are also done automatically when an object of a derived class is assigned to an object of the base class . However, the compiler resorts to a member-wise assignment in the absence of an overloaded function for the assignment operator . 11.6 Nested Classes Many a times, it becomes necessary to have a class contain properties of two other classes. One way is to define a class within another – that is a class with member classes also called nested classes. This has nothing to do with inheritance. Another way is multiple inheritance , which will be discussed later. e.g. class Aclass { public : Aclass(int pv) { private_variable_A = pv; } private : int private_variable_A; }; class Bclass { public : Bclass(int bpv, int apv): Aobj(apv) { private_variable_B = bpv; } private : int private_variable_B; Aclass Aobj; // Declaring an object here. }; As can be seen , the class Bclass contains an object Aobj in its private section as one of its members. Also, it contains a constructor function with the same name , to which the two variables passed are bpv and apv. The variable bpv is used to initialize the private variable of Bclass. However, the constructor contains something after the colon. The part after the colon in the definition of a constructor is called as initialization section and the actual body of the constructor is called as assignment section . Initialization section initializes the base class members , whereas assignment section contains statements to initialize the derived class members .


As seen earlier , in case of the derived classes, the name of the base class constructor is written after colon in the initialization section. This base class constructor is called before the constructor in the derived class. However, this example does not contain a derived class. This is the example of the nested class . In this case, the name of the object of the member class ‘Aclass’ is written after the colon. It tells the compiler to initialize the Aobj data member of Bclass with the value in apv. It is exactly like declaring an object of Aclass with the statement : Aclass Aobj(apv); The only change is that, it is written after the colon in the initialization section of the Bclass constructor. Its assignment section contains code to initialize its own members. The same constructor function can also be written as : Bclass (int bovine apv):Aobj(apv),private_variable_B = bpv;

11.7 Multilevel Inheritance In multilevel inheritance there is a parent class , from whom we derive another class . now from this derived class we can derive another class and so on.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++ Class A

Class B

Class C

Multilevel Inheritance

class Aclass { : : } class Bclass : public Aclass { : : } class Cclass : public Bclass { : : }

11.8 Multiple Inheritance


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
Multiple inheritance , as the name suggests , is deriving a class from more than one class . The derived class inherits all the properties of all its base classes. Consider the following example : Class A Class B

Class C

Multiple Inheritance class Aclass { : : }; class Bclass { : : }; class Cclass : public Aclass , public Bclass { private : : : public : Cclass(...) : Aclass (...), Bclass(...) { }; };

The class Cclass in the above example is derived from two classes – Aclass and Bclass – therefore the first line of its class definition contains the name of two classes, both


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
publicly inherited. Like with normal inheritance , constructors have to be defined for initializing the data members of all classes. The constructor in Cclass calls constructors for base classes. The constructor calls are separated by commas. 11. 9 Multiple inheritance with a common base (Hybrid Inheritance) Inheritance is an important and powerful feature of OOP. Only the imagination of the person concerned is the limit. There are many combinations in which inheritance can be put to use. For instance, inheriting a class from two different classes, which in turn have been derived from the same base class . e.g.


Class A

Class B


Hybrid Inheritance

class base { : : };

class Aclass : public base


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
{ : : }; class Bclass : public base { : : }; class derived : public Aclass, public Bclass { : : };

Aclass and Bclass are two classes derived from the same base class . The class derived has a common ancestor – class base. This is multiple inheritance with a common base class . However, this subtlety of class inheritance is not all that simple. One potential problem here is that both, Aclass and Bclass, are derived from base and therefore both of them, contains a copy of the data members base class. The class derived is derived from these two classes. That means it contains two copies of base class members – one from Aclass and the other from Bclass. This gives rise to ambiguity between the base data members. Another problem is that declaring an object of class derived will invoke the base class constructor twice. The solution to this problem is provided by virtual base classes.

11. 10 Virtual Base Classes This ambiguity can be resolved if the class derived contains only one copy of the class base. This can be done by making the base class a virtual class. This keyword makes the two classes share a single copy of their base class . It can be done as follows : class base { : : };


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
class Aclass : virtual public base { : : }; class Bclass : virtual public base { : : }; class derived : public Aclass, public Bclass { : : }; This will resolve the ambiguity involved.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++

Contents 12.1 Abstract class. 12.2 Virtual function. 12.3 Pure virtual function 12.4 Self test

12.1. Abstract Classes Abstract classes are the classes, which are written just to act as base classes. Consider the following classes. class base { : : }; class Aclass : public base { : : }; class Bclass : public base { : : }; class Cclass : public base { : : };


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
void main() { Aclass objA; Bclass objB; Cclass objC; : : } There are three classes – Aclass, Bclass, Cclass – each of which is derived from the class base. The main () function declares three objects of each of these three classes. However, it does not declare any object of the class base. This class is a general class whose sole purpose is to serve as a base class for the other three. Classes used only for the purpose of deriving other classes from them are called as abstract classes. They simply serve as base class , and no objects for such classes are created.

12.2 Virtual Functions The keyword virtual was earlier used to resolve ambiguity for a class derived from two classes, both having a common ancestor. These classes are called virtual base classes. This time it helps in implementing the idea of polymorphism with class inheritance . The function of the base class can be declared with the keyword virtual. The program with this change and its output is given below. class Shape { public :

virtual void print() { cout << “ I am a Shape “ << endl; } }; class Triangle : public Shape { public : void print() {


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
cout << “ I am a Triangle “ << endl; } }; class Circle : public Shape { public : void print() { cout << “ I am a Circle “ << endl; } }; void main() { Shape S; Triangle T; Circle C; S.print(); T.print(); C.print(); Shape *ptr; ptr = &S; ptr -> print(); ptr = &T; ptr -> print(); ptr = &C; ptr -> print(); } The output of the program is given below: I am a Shape I am a Triangle I am a Circle I am a Shape I am a Triangle I am a Circle


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
Now, the output of the derived classes are invoked correctly. When declared with the keyword virtual , the compiler selects the function to be invoked, based upon the contents of the pointer and not the type of the pointer. This facility can be very effectively used when many such classes are derived from one base class . Member functions of each of these can be ,then, invoked using a pointer to the base class .

12.3 Pure Virtual Functions As discussed earlier, an abstract class is one, which is used just for deriving some other classes. No object of this class is declared and used in the program. Similarly, there are pure virtual functions which themselves won’t be used. Consider the above example with some changes.

class Shape { public : virtual void print() = 0; // Pure virtual function }; class Triangle : public Shape { public : void print() { cout << “ I am a Triangle “ << endl; } }; class Circle : public Shape { public : void print() { cout << “ I am a Circle “ << endl; } };


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++

void main() { Shape S; Triangle T; Circle C; Shape *ptr; ptr = &T; ptr -> print(); ptr = &C; ptr -> print(); } The output of the program is given below: I am a Triangle I am a Circle It can be seen from the above example that , the print() function from the base class is not invoked at all . even though the function is not necessary, it cannot be avoided, because , the pointer of the class Shape must point to its members.

Object oriented programming has altered the program design process. Exciting OOP concepts like polymorphism have given a big boost to all this. Inheritance has further enhanced the language. This session has covered some of the finer aspects of inheritance. The next session will resolve some finer aspects of the language. EXAMPLE:// virtual members #include <iostream.h> class CPolygon { protected: int width, height; public: void set_values (int a, int b) 20 10 0


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
{ width=a; height=b; } virtual int area (void) { return (0); } }; class CRectangle: public CPolygon { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon { public: int area (void) { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon poly; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; CPolygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ppoly3->area() << endl; return 0; } Now the three classes (CPolygon, CRectangle and CTriangle) have the same members: width, height, set_values() and area(). area() has been defined as virtual because it is later redefined in derived classes. You can verify if you want that if you remove this word (virtual) from the code and then you execute the program the result will be 0 for the three polygons instead of 20,10,0. That is because instead of calling the corresponding area() function for each object (CRectangle::area(), CTriangle::area() and CPolygon::area(), respectively), CPolygon::area() will be called for all of them since the calls are via a pointer to CPolygon.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
Therefore what the word virtual does is to allow that a member of a derived class with the same name as one in the base class be suitably called when a pointer to it is used, as in the above example. Note that in spite of its virtuality we have also been able to declare an object of type CPolygon and to call its area() function, that always returns 0 as the result.

12.4 Self test Create a class drugs containing encapsulated data for medicine name, whether solid or liquid, price and purpose of use. From this class derive two classes, Ayurvedic and Allopathic. The class Ayurvedic should additionally store data on the herbs used, association to be used (whether honey or water). The class Allopathic should additionally include data on the chemicals used and the weight in milligrams. The classes should contain constructors and destructors. They should contain functions to accept data and display the data. The main() should test the derived classes.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++

Contents 13.1 Templates. 13.1.1 Function template 13.1.2 Class templates 13.1.3 Template specialization 13.1.4 Parameter values for templates 13.1.5 Templates and multiple -file project 13.2 Exception handling 13.2.1 Exception not caught 13.2.2 Standard exception 13.3 Advanced class type-casting. 13.3.1 reinterpret cast 13.3.2 static cast 13.3.3 dynamic cast 13.3.4 const_cast 13.3.5 typeid 13.4 Preprocessor directives.

13.1 Templates 13.1.1 Function templates Templates allow to create generic functions that admit any data type as parameters and return a 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 identifier> function_declaration; template <typename identifier> 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:


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
template <class GenericType> GenericType GetMax (GenericType a, GenericType b) { return (a>b?a:b); } As the first line specifies, 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 the type for its two parameters a and b and as the return type 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 a 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. 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


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
} (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. In this concrete case where the generic T type is used as a parameter for function GetMax the compiler can find out automatically which data type is passed to it without having to specify it with patterns <int> or <long>. So we could have written: int i,j; GetMax (i,j); since both i and j are 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 << k << endl; cout << n << endl; return 0; } 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. 6 10


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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 our template function with two objects of different types as parameters: int i; long l; k = GetMax (i,l); This 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 by writing: int i,j; long l; i = GetMin<int,long> (j,l); or simply i = GetMin (j,l); even though j and l are of different types.

13.1.2 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; }


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
}; 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 also serve 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. If we define a function member outside the declaration we must always 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; } notice how the definition of member function getmax begins: 100


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
template <class T> T pair<T>::getmax () All Ts that appear are necessary because whenever you declare member functions you have to follow a format similar to this (the second T makes reference to the type returned by the function, so this may vary).

13.1.3 Template Specialization A template specialization allows a template to make specific implementations 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 it to work when the contained type is int. For the rest of the types we want this function to return 0. This can be done the following 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; } 25 0


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
int main () { pair <int> myints (100,75); pair <float> myfloats (100.0,75.0); cout << myints.module() << '\n'; cout << myfloats.module() << '\n'; return 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 and 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 equating them to the specialization (if one pays attention, in the example above we have had to include its own constructor, although it is identical to the one in the generic template). The reason is that no member is "inherited" from the generic template to the specialized one. 13.1.4 Parameter values for templates Besides the template arguments preceded by the class or typename keywords that represent a type, function 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: void 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; 100 3.1416


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
} 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,3.1416); cout << myints.getmember(0) << '\n'; cout << myfloats.getmember(3) << '\n'; return 0; } It is also possible to set default values for any template parameter just as it is done with function parameters. Some possible template examples seen above: template template template template template <class T> // The most usual: one class parameter. <class T, class U> // Two class parameters. <class T, int N> // A class and an integer. <class T = char> // With a default value. <int Tfunc (int)> // A function as parameter.

13.1.5 Templates and multiple-file projects From the point of view of the compiler, templates are not normal functions 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 a function specifically for that type from the template. 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 a library of functions as example, the interface generally consists of the prototypes of all the functions that can be called. 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 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


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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 this type of file other than there be 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.

13.2 Exception Handling During the development of a program, there 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 resources that do not exist or because it gets out of an expected range, etc... These types of anomalous situations are included in what we consider exceptions and C++ has recently incorporated three new operators to help us handle these situations: try, throw and catch. Their 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 the 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 executed a throw instruction within the try block, the catch block is executed receiving as parameter the exception passed by throw. For example: // exceptions Exception: Out of range


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
#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) { 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 that of return: Only the parameter does not need 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 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"; Exception: index 10 is out of range


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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; } In this case there is a possibility that at least two different exceptions could happen: 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 (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 the 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: 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 the expression throw; with no arguments is used. For example: try { try { // code here }


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
catch (int n) { throw; } } catch (...) { cout << "Exception occurred"; } 13.2.1 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();

13.2.2 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 a pattern for the standard hierarchy of exceptions: Exception bad_alloc bad_cast bad_exception bad_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) (thrown by new) (thrown by dynamic_cast when fails with a referenced type) (thrown when an exception doesn't match any catch) (thrown by typeid)


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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++). 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; } You can use the classes of standard hierarchy of exceptions to throw your exceptions or derive new classes from them. Exception: Attempted typeid of NULL pointer

13.3 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);


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
This is quite good for basic types that have standard defined conversions, however this operators can also be 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 () { CDummy d; CAddition * padd; padd = (CAddition*) &d; cout << padd->result(); return 0; } Although the previous program in syntactically correct in C++ (in fact it will compile with no warnings on most compilers) it is code with not much sense since we use function result, that is a member of CAddition, without having declared an 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. 13.3.1 Reinterpret Cast 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)


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
Where new_type is the destination type to which expression has to be casted. 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 a 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 one 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 large enough 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. 13.3.2 static_cast static_cast performs any casting that can be implicitly performed as well as the inverse cast (even if this is not allowed implicitly). 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 it 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);


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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);

13.3.3 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 = Base* b2 = Derived d1 Derived d2 new Derived; new Base; = dynamic_cast<Derived&*>(b1); // succeeds = 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:


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
class C {}; const C * a = new C; C * b = const_cast<C*> (a); Neither of the other three new cast operators can modify the constness of an object. 13.3.4 Typeid ANSI-C++ also defines a new operator called typeid that allows checking the type of an expression: typeid (expression) This operator returns a refernece to a constant object of type type_info that is defined in the 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 CDummy

Preprocessor Director 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 in charge of making 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 ;.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
#define At the beginning of this tutorial we have already spoken about a preprocessor directive: #define, that serves to generate what we called defined constannts 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: #define 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 of #define. It eliminates from the list of defined constants the one that has the name passed as a 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 the parameter has been defined, independently of its value. Its operation is: #ifdef name // code here #endif For example:


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
#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]; In this case, if when arriving at 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 so 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 #define MAX_WIDTH 50 #else #undef MAX_WIDTH #define 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 errors happen during the compiling process, the compiler shows the error that happened preceded by the name of the file and the line within the file where it has taken place.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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 to appear 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 another 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 the parameter: #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 the directories in which the compiler is going to look for the file. In the first case where the file is specified between quotes, the file is looked for in the same directory that includes the file containing the directive. In case that it is not there, the compiler looks for the file in the default directories where it is configured to look for the standard header files. If the file name is enclosed between angle-brackets <> the file is looked for directly where the compiler is configured to look for the standard header files. #pragma


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
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.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++

Contents 14.1 Input/Output with files. 14.2 Open a file 14.3 Closing a file 14.4 Methods of Input and Output Classes 14.5 Text mode files 14.6 state flags 14.7 Binary files 14.8 Buffers and Synchronization

14.1 Input Output With Files The techniques for file input and output, i/o, in C++ are virtually identical to those introduced in earlier lessons for writing and reading to the standard output devices, the screen and keyboard. To perform file input and output the include file fstream must be used. #include <fstream> Fstream contains class definitions for classes used in file i/o. Within a program needing file i/o, for each output file required, an object of class ofstream is instantiated. For each input file required, an object of class ifstream is instantiated. The ofstream object is used exactly as the cout object for standard output is used. The ifstream object is used exactly as the cin object for standard input is used. This is best understood by studying an example. C++ has support both for input and output with files through the following classes: • • • ofstream: File class for writing operations (derived from ostream) ifstream : File class for reading operations (derived from istream) fstream : File class for both reading and writing operations (derived from iostream)

14.2 Open a file The first operation generally done on an object of one of these classes is to associate it to a real file, that is to say, to open a file. The open file is represented within the program by a stream object (an instantiation of one of these classes) and any input or output performed on this stream object will be applied to the physical file.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
In order to open a file with a stream object we use its member function open(): void open (const char * filename, openmode mode); where filename is a string of characters representing the name of the file to be opened and mode is a combination of the following flags: ios::in ios::out ios::ate ios::app ios::trunc ios::binary Open file for reading Open file for writing Initial position: end of file Every output is appended at the end of file If the file already existed it is erased Binary mode

These flags can be combined using bitwise operator OR: |. For example, if we want to open the file "example.bin" in binary mode to add data we could do it by the following call to function-member open: ofstream file; ("example.bin", ios::out | ios::app | ios::binary); All of the member functions open of classes ofstream, ifstream and fstream include a default mode when opening files that varies from one to the other: class ofstream ifstream fstream default mode to parameter ios::out | ios::trunc ios::in ios::in | ios::out

The default value is only applied if the function is called without specifying a mode parameter. If the function is called with any value in that parameter the default mode is stepped on, not combined. Since the first task that is performed on an object of classes ofstream, ifstream and fstream is frequently to open a file, these three classes include a constructor that directly calls the open member function and has the same parameters as this. This way, we could also have declared the previous object and conducted the same opening operation just by writing: ofstream file ("example.bin", ios::out | ios::app | ios::binary); Both forms to open a file are valid.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
You can check if a file has been correctly opened by calling the member function is_open(): bool is_open(); that returns a bool type value indicating true in case that indeed the object has been correctly associated with an open file or false otherwise.

14.3 Closing a file When reading, writing or consulting operations on a file are complete we must close it so that it becomes available again. In order to do that we shall call the member function close(), that is in charge of flushing the buffers and closing the file. Its form is quite simple: void close (); Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes. In case that an object is destructed while still associated with an open file, the destructor automatically calls the member function close.

14.4 Methods of Input and Output Classes The ifstream class has several useful methods for input. These method are also in the class cin, which is used to read from standard input. These methods are used to read from any input stream. An input stream is a source of input such as the keyboard, a file or a buffer. • Extraction Operator, >> This overloaded operator handles all built in C++ data types. By default, any intervening white space is disregarded. That is, blanks, tabs, new lines, formfeeds and carriage returns are skipped over. get() This form of get extracts a single character from the input stream, that is, from the standard input, a file or a buffer. It does not skip white space. It returns type int. get(char &ch) This form of get also extracts a single character from the input stream, but it stores the value in the character variable passed in as an argument. get(char *buff, int buffsize, char delimiter='\n')


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
This form of get reads characters into the C-style buffer passed as an argument buffsize characters are read, the delimiter is encountered or an end of file is encountered. The '\n' is the new line character. The delimiter is not read into the buffer but is instead left in the input stream. It must be removed separately but using either another get or an ignore. Because of this added step, this form of get is a frequent source of errors and should be avoided. Fortunately, another method shown below, getline, reads in the delimiter as well and should be used in place of this form of get. • • • Getline There are several useful forms of getline. ignore(int count=1, int delim=traits_type::eof) This method reads and discards "count" characters from the input stream. peek() This method returns the next character from the input stream, but does not remove it. It is useful to look ahead at what the next character read will be. putback(char &ch) This method puts ch onto the input stream. The character in ch will then be the next character read from the input stream. unget() This method puts the last read character back into the input stream. read(char *buff, int count) This method is used to perform an unformatted read of count bytes from the input stream into a character buffer.

• •

The ofstream class has several useful methods for writing to an output stream. An output stream is standard output (usually the screen), a file or a buffer. These methods are also in the object cout, which is used for standard output. The simplest way to understand how to use these methods is by looking at a few examples. Since we have seen the extraction, >>, and insertion, << in several lessons, let's look at the other methods. Getline, which is very useful to read entire lines of text into a string. Suppose we need to read a file and determine the number of alphanumeric characters, the number of blanks and the number of sentences. To determine the number of sentences we will count the number of periods (dots). We will disregard newlines and tabs. Here is a program that solves the problem. #include <iostream> #include <fstream> using namespace std; int main()


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
{ int blank_count = 0; int char_count = 0; int sentence_count = 0; char ch; ifstream iFile("c:/lesson12.txt"); if (! iFile) { cout << "Error opening input file" << endl; return -1; } while (iFile.get(ch)) { switch (ch) { case ' ': blank_count++; break; case '\n': case '\t': break; case '.': sentence_count++; break; default: char_count++; break; } } cout << "There are " << blank_count << " blanks" << endl; cout << "There are " << char_count << " characters" << endl; cout << "There are " << sentence_count << " sentences" << endl; return 0; } As a second example, let's implement a program that will copy the contents of one file to another. The program will prompt the user for the input and output file names, and then copy.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { char ch; string iFileName; string oFileName; cout << "Enter the source file name: "; cin >> iFileName; cout << "Enter the destination file name: "; cin >> oFileName; ofstream oFile(oFileName.c_str()); ifstream iFile(iFileName.c_str()); //Error checking on file opens omitted for brevity. while (iFile.get(ch)) { oFile.put(ch); } return 0; } 14.5 Text mode files Classes ofstream, ifstream and fstream are derived from ostream, istream and iostream respectively. That's why fstream objects can use the members of these parent classes to access data. Generally, when using text files we shall use the same members of these classes that we used in communication with the console (cin and cout). As in the following example, where we use the overloaded insertion operator <<: // writing on a text file #include <fstream.h> int main () { This is a line. This is another line.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
ofstream examplefile ("example.txt"); if (examplefile.is_open()) { examplefile << "This is a line.\n"; examplefile << "This is another line.\n"; examplefile.close(); } return 0; } Data input from file can also be performed in the same way that we did with cin: // reading a text file #include <iostream.h> #include <fstream.h> #include <stdlib.h> int main () { char buffer[256]; ifstream examplefile ("example.txt"); if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); } while (! examplefile.eof() ) { examplefile.getline (buffer,100); cout << buffer << endl; } return 0; } This last example reads a text file and prints out its content on the screen. Notice how we have used a new member function, called eof that ifstream inherits from class ios and that returns true in case that the end of the file has been reached. This is a line. This is another line.

14.6 State flags In addition to eof(), other member functions exist to verify the state of the stream (all of them return a bool value): bad() Returns true if a failure occurs in a reading or writing operation. For example in case we try to write to a file that is not open for writing or if the device where we try to write has no space left.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++

Returns true in the same cases as bad() plus in case that a format error happens, as trying to read an integer number and an alphabetical character is received. eof() Returns true if a file opened for reading has reached the end. good() It is the most generic: returns false in the same cases in which calling any of the previous functions would return true. In order to reset the state flags checked by the previous member functions you can use member function clear(), with no parameters. get and put stream pointers All i/o streams objects have, at least, one stream pointer: • • ifstream, like istream, has a pointer known as get pointer that points to the next element to be read. ofstream, like ostream, has a pointer put pointer that points to the location where the next element has to be written.

• Finally fstream, like iostream, inherits both: get and put These stream pointers that point to the reading or writing locations within a stream can be read and/or manipulated using the following member functions: tellg() and tellp() These two member functions admit no parameters and return a value of type pos_type (according ANSI-C++ standard) that is an integer data type representing the current position of get stream pointer (in case of tellg) or put stream pointer (in case of tellp). seekg() and seekp() This pair of functions serve respectively to change the position of stream pointers get and put. Both functions are overloaded with two different prototypes: seekg ( pos_type position ); seekp ( pos_type position );


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
Using this prototype the stream pointer is changed to an absolute position from the beginning of the file. The type required is the same as that returned by functions tellg and tellp. seekg ( off_type offset, seekdir direction ); seekp ( off_type offset, seekdir direction ); Using this prototype, an offset from a concrete point determined by parameter direction can be specified. It can be: ios::beg ios::cur ios::end Offset specified from the beginning of the stream Offset specified from the current position of the stream pointer Offset specified from the end of the stream

The values of both stream pointers get and put are counted in different ways for text files than for binary files, since in text mode files some modifications to the appearance of some special characters can occur. For that reason it is advisable to use only the first prototype of seekg and seekp with files opened in text mode and always use nonmodified values returned by tellg or tellp. With binary files, you can freely use all the implementations for these functions. They should not have any unexpected behavior. The following example uses the member functions just seen to obtain the size of a binary file: // obtaining file size #include <iostream.h> #include <fstream.h> const char * filename = "example.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file.seekg (0, ios::end); m = file.tellg(); file.close(); cout << "size of " << filename; cout << " is " << (m-l) << " bytes.\n"; return 0; } size of example.txt is 40 bytes.


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
14.7 Binary files

In binary files inputting and outputting data with operators like << and >> and functions like getline, does not make too much sense, although they are perfectly valid. File streams include two member functions specially designed for input and output of data sequentially: write and read. The first one (write) is a member function of ostream, also inherited by ofstream. And read is member function of istream and it is inherited by ifstream. Objects of class fstream have both. Their prototypes are: write ( char * buffer, streamsize size ); read ( char * buffer, streamsize size ); Where buffer is the address of a memory block where the read data are stored or from where the data to be written are taken. The size parameter is an integer value that specifies the number of characters to be read/written from/to the buffer. // reading binary file #include <iostream.h> #include <fstream.h> const char * filename = "example.txt"; int main () { char * buffer; long size; ifstream file (filename, ios::in|ios::binary|ios::ate); size = file.tellg(); file.seekg (0, ios::beg); buffer = new char [size]; (buffer, size); file.close(); cout << "the complete file is in a buffer"; delete[] buffer; return 0; } the complete file is in a buffer

14.8 Buffers and Synchronization


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
When we operate with file streams, these are associated to a buffer of type streambuf. This buffer is a memory block that acts as an intermediary between the stream and the physical file. For example, with an out stream, each time the member function put


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
(write a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in the buffer for that stream. When the buffer is flushed, all data that it contains is written to the physic media (if it is an out stream) or simply erased (if it is an in stream). This process is called synchronization and it takes place under any of the following circumstances: • • • • When the file is closed: before closing a file all buffers that have not yet been completely written or read are synchronized. When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized. Explicitly with manipulators: When certain manipulators are used on streams synchronization takes place. These manipulators are: flush and endl. Explicitly with function sync(): Calling member function sync() (no parameters) causes an immediate syncronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure.

14.9 I/O Manipulators Up till now, we have accepted the default output formatting. C++ defines a set of manipulators which are used to modify the state of iostream objects. These control how data is formatted. They are defined in the include file, <ios>. It is not usually necessary to explicitly include this file because it is included indirectly via the use of other includes such as <iostream> or <fstream>. Let's see how some of these manipulators work in a simple program. Manipulator boolalpha noboolalhpa (default) dec (default) hex oct left right internal Use Causes bool variables to be output as true or false. Causes bool variables to be displayed as 0 or 1. Specifies that integers are displayed in base 10. Specifies that integers are displayed in hexadecimal. Specified that integers are displayed in octal. Causes text to be left justified in the output field. Causes text to be right justified in the output field. Causes the sign of a number to be left justified and the value to be right justified. Turns off displaying a prefix indicating the base of a number.

noshowbase (default)


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
showbase noshowpoint (default) showpoint noshowpos (default) showpos skipws (default) noskipws fixed (default) scientific nouppercase (default) uppercase Turns on displaying a prefix indicating the base of a number. Displays decimal point only if a fractional part exists. Displays decimal point always. No "+" prefixing a positive number. Displays a "+" prefixing a positive number. Causes white space (blanks, tabs, newlines) to be skipped by the input operator, >>. White space not skipped by the extraction operator, >>. Causes floating point numbers to be displayed in fixed notation. Causes floating point numbers to be displayed in scientific notation. 0x displayed for hexadecimal numbers, e for scientific notation 0X displayed for hexadecimal numbers, E for scientific notation

The manipulators in the above table modify the state of the iostream object. This means that once used on an iostream object they will effect all subsequent input or output done with the object. There are several other manipulators that are used to format a particular output but do no modify the state of the object. Setting Output Width setw(w) - sets output or input width to w; requires <iomanip> to be included. width(w) - a member function of the iostream classes. Filling White Space setfill(ch) - fills white space in output fields with ch; requires <iomanip> to be included. fill(ch) = a member function of the iostream classes. Setting Precision setprecision(n) - sets the display of floating point numbers at precision n. This does not effect the way floating point numbers are handled during calculations in your program. Here is a simple program illustrating the use of the i/o manipulators. #include <iostream> #include <iomanip> #include <string> using namespace std;


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
int main() { int intValue = 15; cout cout cout cout cout cout cout cout cout cout << << << << << << << << << << "Integer Number" << endl; "Default: " << intValue << endl; "Octal: " << oct << intValue << endl; "Hex: " << hex << intValue << endl; "Turning showbase on" << showbase << endl; "Dec: " << dec << intValue << endl; "Octal: " << oct << intValue << endl; "Hex: " << hex << intValue << endl; "Turning showbase off" << noshowbase << endl; endl;

double doubleVal = 12.345678; cout cout cout cout cout cout cout cout << << << << << << << << "Floating Point Number" << endl; "Default: " << doubleVal << endl; setprecision(10); "Precision of 10: " << doubleVal << endl; scientific << "Scientific Notation: " << doubleVal << endl; uppercase; "Uppercase: " << doubleVal << endl; endl;

bool theBool = true; cout cout cout cout << << << << "Boolean" << endl; "Default: " << theBool << endl; boolalpha << "BoolAlpha set: " << theBool << endl; endl;

string myName = "John"; cout << "Strings" << endl; cout << "Default: " << myName << endl; cout << setw(35) << right << "With setw(35) and right: " << myName << endl; cout.width(20); cout << "With width(20): " << myName << endl; cout << endl;


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++
return 0; }


BTech IIIrd Semester Paper Code: BTC31 Paper Name: Object Oriented Programming Using C++


Sign up to vote on this title
UsefulNot useful