Você está na página 1de 29

UNIT-1: INTRODUCTION TO OOPs & INPUT AND OUTPUT IN C++

Lecture No. Topics


1 Introduction to C++
2 Differences between C and C++
3 The Object Oriented Technology
4 Disadvantage of Conventional Programming
5 Concepts of Object Oriented Programming
6 Advantages of OOP
7 Structure of a C++ Program and Header Files and Libraries
8 Introduction to Input and output
9 Streams in C++ and Stream Classes, Predefined Streams, Stream Classes
10 Formatted and Unformatted Data
11 Unformatted Console I/O Operations
12 Member Functions of Istream Class and Formatted Console I/O Operations
13 Bit Fields, Flags without Bit Fields
14 Manipulators, User Defined Manipulators
Lecture-1:Introduction to C++

INTRODUCTION:
C++ is an object-oriented programming language and is considered to be an extension of C.
Bjarne Stroustrup at AT&T Bell Laboratories, Murray Hill, New Jersey (USA) developed it in
the early eighties of twentieth century. Stroustrup, a master of Simula67 and C, wanted to
combine the features of both the languages into a more powerful language that could support
object-oriented programming with features of C.
The outcome was C++ as per Figure 1.1. Various ideas were derived from SIMULA67 and
ALGOL68. Stroustrup called the new language ‘C with classes’. However, in 1983, the name
was changed to C++. The thought of C++ came from the C increment operator ++. Rick Mascitti
coined the term C++ in 1983. Therefore, C++ is an extended version of C. C++ is a superset of
C. All the concepts of C are applicable to C++ also.

Fig. 1.1 Evolution of C++


UNIT-1: Introduction to OOPs & Input and Output in C++
29
Lecture-2: Differences between C and C++
Difference between C and C++

S.
C C++
No.

1 C is a structural or procedural programming C++ is an object oriented programming

language. language.

2 Emphasis is on procedure or steps to solve Emphasis is on objects rather than procedure.

any problem.

3 Functions are the fundamental building Objects are the fundamental building blocks.

blocks.

4 In C, the data is not secured. Data is hidden and can’t be accessed by

external functions.

5 C follows top down approach. C++ follows bottom up approach

6 C uses scanf() and printf() function for C++ uses cin>> and cout<< for standard

standard input and output. input and output.

7 Variables must be defined at the beginning in Variables can be defined anywhere in the

the function. function.

8 In C, namespace feature is absent. In C++, namespace feature is present.

9 C is a middle level language. C++ is a high level language.

Lecture-3: Object Oriented Technology

Object Oriented Technology


Nature is composed of various objects. Living beings can be categorized with different objects.
Let us consider an example of a teaching institute having two different working sections. They
are teaching and non-teaching. Further sub-grouping of teaching and non-teaching can be made
for the ease of management. The various departments of any organization can be thought as
objects working with certain goals and objectives.

Usually, an institute has faculties for different departments. Also, laboratory staff for the
assistance in conducting of practical’s and site development section for beautification of the
campus are essential. The Principal is a must for the overall management of the Institute. The
accounts department is also required for handling monetary transactions, and salaries of the

UNIT-1: Introduction to OOPs & Input and Output in C++


29
employees. The sports section is entrusted the responsibility of sports activities, the Registrar for
administration and his staff for dealing with administrative-matters of the Institute and so on are
required. Each department has an In-Charge who has clear-cut responsibilities. Every department
has its own work as stated above. When the work is distributed to different departments as shown
in Figure 1.2, it becomes easy to accomplish goals and objectives. The activities are carried out
smoothly. The burden of one particular department has to be shared among personnel.

As shown in Figure 1.2 an institute is divided into different departments such as library,
classroom, computer laboratory, etc. Each department performs its own activities in association
with other departments. This theory of objects can be extended to every walk of life and can be
implemented with the help of software. In general, objects are in terms of entities.

Fig. 1.2 Relationship between different sections

Lecture-4: Disadvantage of Conventional Programming

DISADVANTAGE OF CONVENTIONAL PROGRAMMING

Traditional programming languages such as COBOL, FORTRAN etc. are commonly known as
procedure oriented languages. The program written in these languages consists of a sequence of
instructions that tells the compiler or interpreter to perform a given task. When program code is
large then it becomes inconvenient to manage it. To overcome this problem, procedures or
subroutines were adopted to make a program more understandable to the programmers. A
program is divided into many functions. Each function can call another function as shown in
Figure1.3. Each function has its own task. If the program is too large the function also creates
problems. In many programs, important data variables are declared as global. In case of
programs containing several functions, every function can access the global data as simulated in

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Figure 1.4. Generally in a program of large size, it becomes difficult to understand the various
data used in different functions. As a result, a program may have several logical errors.

Following are the drawbacks observed in monolithic, procedural, and structured


programming languages:

1. Large size programs are divided into smaller programs known as functions. These
functions
2. can call one another. Hence, security is not provided.
3. Importance is not given to security of data but on doing things.
4. Data passes globally from one function to another.
5. Most functions have access to global data.

Fig. 1.3 Flow of functions in non-OOP languages

Fig. 1.4 Sharing of data by functions in non-OOP languages

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Lecture-5 and 6: Concepts of Object Oriented Programming

PROGRAMMING PARADIGMS

1. Monolithic Programming
2. Procedural Programming (functions)
3. Modular Programming (namespaces)
4. Object Oriented Programming (classes)
5. Generic Programming (templates)
(1) Monolithic Programming:

1. In monolithic programming languages such as BASIC and ASSEMBLY, the data


variables declared are global and the statements are written in sequence.
2. The program contains jump statements such as goto that transfer control to any statement
as specified in it. Figure 1.5 shows a program of monolithic type. The global data can
be accessed from any portion of the program. Due to this reason the data is not fully
protected.
3. The concept of subprograms does not exist and hence useful for smaller programs

Fig. 1.5 Program in monolithic programmin

(2) Procedural Programming

1. In procedural programming languages such as FORTRAN and COBOL, programs are


divided into a number of segments known as subprograms. Thus it focuses on functions apart
from data. Figure 1.6 describes a program of procedural type. It shows different subprograms
having access to the same global data. Here also, the data is not fully protected.
2. The control of program is transferred using unsafe goto statement.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
3. Data is global and all the subprograms share the same data.
4. These languages are used for developing medium size applications.

(3) Structured Programming

1. In structured programming languages such as PASCAL and C larger programs are developed.
These programs are divided into multiple submodules and procedures.
2. Each procedure has to perform different tasks.
3. Each module has its own set of local variables and program code as shown in Figure 1.7.
Here, the different modules are shown accessing the global data.
4. User-defined data types are introduced.

Fig. 1.6 Program in procedural programming

(4) Concepts of Object Oriented Programming

There are several fundamental or key concepts in object-oriented programming.

(1) Objects:
Objects are primary run-time entities in an object-oriented programming.
Objects are primary run-time entities in an object-oriented programming. They may stand
for a thing that has specific application for example, a spot, a person, any data item
related to program, including user-defined data types. Programming issues are analyzed
in terms of object and the type of transmission between them. Figure 1.10 describes
various objects.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Fig. 1.9 Features of object-oriented programming
Given below are some objects that we use in our daily life.

Fig. 1.10 Commonly available objects

(2) Classes
A class is grouping of objects having identical properties, common behavior, and shared
relationship.
A class is the accomplishment of abstract data type. It defines the nature and methods that
act on the data structure and abstract data type respectively. Specimens are also known as
objects. In other words, a class is a grouping of objects having identical properties,
common behavior, and shared relationship. The entire group of data and code of an object
can be built as a user-defined data type using class.

Fig. 1.11 Objects and their properties


In Figure 1.11Some common objects, their properties, and the actions they perform have
been described. A car can be distinguished from another car on the basis of its properties,

UNIT-1: Introduction to OOPs & Input and Output in C++


29
for example, company, model, color, capacity etc. and also on the basis of its actions for
example, speed, average, break etc.
(3) Method
An operation required for an object or entity when coded in a class is called a method.
An operation required for an object or entity when coded in a class is called a method.
The operations that are required for an object are to be defined in a class. All objects in a
class perform certain common actions or operations. Each action needs an object that
becomes a function of the class that defines it and is referred to as a method.

Fig. 1.12 Classes and their members


Example:

Class A
{
Private
data member1;
data member2; Members
data member(n);
public
method1(){ }
method2(){ }
method_n(){ }
}
(4) Data Abstraction
Abstraction directs to the procedure of representing essential features without including the
background details.
Abstraction directs to the procedure of representing essential features without including the
background details. Classes use the theory of abstraction and are defined as a list of abstract
properties such as size, cost, height, and few functions to operate on these properties. Data
abstraction is the procedure of identifying properties and methods related to a specific entity as
applicable to the application.
(5) Encapsulation

UNIT-1: Introduction to OOPs & Input and Output in C++


29
The packing of data and functions into a single component is known as encapsulation.
C++ supports the features of encapsulation using classes. The packing of data and functions into
a single component is known as encapsulation. The data is not accessible by outside functions.
Only those functions that are able to access the data are defined within the class. These functions
prepare the interface between the object's data and the program.

Fig. 1.15 Encapsulation: Private and Public sections


(6) Inheritance
Inheritance is the method by which objects of one class get the properties of objects of another
class. Inheritance is the method by which objects of one class get the properties of objects of
another class. In object-oriented programming, inheritance provides the thought of reusability.
The programmer can add new properties to the existing class without changing it. This can be
achieved by deriving a new class from the existing one.

(7) Polymorphism
Polymorphism allows the same function to act differently in different classes. Polymorphism
makes possible the same functions to act differently on different classes as shown in Figure 1.17.
It is an important feature of OOP concept and has the ability to take more than one form.
Polymorphism accomplishes an important part in allowing objects of different classes to share
the same external interface. It is possible to code a non-specific (generic) interface to a set of
associated actions.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Fig. 1.17 Polymorphism in OOP
(8) Dynamic Binding
Binding means connecting one program to another program that is to be executed in reply to the
call. Binding means connecting one program to another program that is to be executed in reply to
the call. Dynamic binding is also known as late binding. The code present in the specified
program is unknown till it is executed. It is analogous to inheritance and polymorphism.
(9) Message Passing
Object-oriented programming includes objects which communicate with each other. Data is
transferred from one object to another. A message for an object is the demand for implementation
of the process.
Programming with these objects should be followed in steps shown below:
(1) Declaring classes that define objects and their actions.
(2) Declaring objects from classes.
(3) Implementing relation between objects.

Fig. 1.18 Message passing


(10) Reusability
Object-oriented technology allows reusability of the classes by extending them to other classes
using inheritance. Object-oriented technology allows reusability of classes by extending them to
other classes using inheritance. Once a class is defined, the other programmer can also use it in

UNIT-1: Introduction to OOPs & Input and Output in C++


29
their programs and add new features to the derived classes. Class A is reused and class B is created.
Again class B is reused and class C is created.

Fig. 1.19 (a) Reusability (b)Relationships between two classes(kind of (inhiretance)and has a
(delegation) relationships)
(11) Delegation
In OOP, two classes can be joined either by - inheritance or delegation, which provide reusability
of the class. In inheritance, one class can be derived from other class and relationship between
them is known as kind of relationship. For example, if class Y is derived from class X, then class
Y is known as kind of X, as shown in Figure 1.19 (b).The second type of relationship is has a
relationship. When object of one class is used as data member in other class, such composition of
objects is known as delegation. As shown in Figure 1.19 (b) class C has two data members that
are objects of class A and B. Such a relationship between two classes is known as has a
relationship.
(12) Genericity
The software components of a program have more than one version depending on the data types
of arguments. This feature allows declaration of variables without specifying exact data type.
The compiler identifies the data type at run-time. The programmer can create a function that can
be used for any type of data. The template feature in C++ allows generic programming.
Lecture-7: ADVANTAGES OF OOP

UNIT-1: Introduction to OOPs & Input and Output in C++


29
ADVANTAGES OF OOP
Object-oriented programming provides many advantages to the programmer and the user. This
technology solves many problems related to software development, provides improved quality,
and low cost software.
OOP has the following advantages:

1. Object-oriented programs can be comfortably upgraded.


2. Using inheritance, redundant program codes can be eliminated and the use of previously
defined classes may be continued.
3. The technology of data hiding facilitates the programmer to design and develop safe
programs that do not disturb code in other parts of the program.
4. The encapsulation feature provided by OOP languages allows programmer to define the class
with many functions and characteristics and only few functions are exposed to the user.
5. All object-oriented programming languages can create extended and reusable parts of
programs.
6. Object-oriented programming enhances the thought process of a programmer leading to rapid
development of new software in short span of time.

Lecture-8: Structure of a C++ Program and Header Files and Libraries


Structure of C++ Program : Layout of C++ Program
C++ Programming language is most popular language after C Programming language. C++ is
first Object oriented programming language.We have summarize structure of C++ Program in
the following Figure.

Figure. Structure of C++ Program


Section 1 : Header File Declaration Section
Header files used in the program are listed here.
Header File provides Prototype declaration for different library functions.
We can also include user define header file.
Basically all preprocessor directives are written in this section.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Section 2 : Global Declaration Section
Global Variables are declared here.
Global Declaration may include –
Declaring Structure
Declaring Class
Declaring Variable
Section 3 : Class Declaration Section
Actually this section can be considered as sub section for the global declaration section.
Class declaration and all methods of that class are defined here.
Section 4 : Main Function
Each and every C++ program always starts with main function.
This is entry point for all the function. Each and every method is called indirectly through main.
We can create class objects in the main.
Operating system call this function automatically.
Section 5 : Method Definition Section
This is optional section . Generally this method was used in C Programming.
Example of basic structre of C++
Let us look at a simple code that would print the words Hello World.
#include <iostream>
using namespace std;
// main() is where program execution begins.

int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Let us look various parts of the above program:
• The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream> is needed.
• The line using namespace std; tells the compiler to use the std namespace. Namespaces
are a relatively recent addition to C++.
• The next line // main() is where program execution begins. is a single-line comment
available in C++. Single-line comments begin with // and stop at the end of the line.
• The line int main() is the main function where program execution begins.
• The next line cout << "This is my first C++ program."; causes the message "This is my
first C++ program" to be displayed on the screen.
• The next line return 0; terminates main( )function and causes it to return the value 0 to
the calling process.

Compile & Execute C++ Program:

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Let's look at how to save the file, compile and run the program. Please follow the steps given
below:
• Open a text editor and add the code as above.
• Save the file as: hello.cpp
• Open a command prompt and go to the directory where you saved the file.
• Type 'g++ hello.cpp ' and press enter to compile your code. If there are no errors in your
code the command prompt will take you to the next line and would generate a.out
executable file.
• Now, type ' a.out' to run your program.
• You will be able to see ' Hello World ' printed on the window.

$ g++ hello.cpp
$ ./a.out
Hello World

Make sure that g++ is in your path and that you are running it in the directory containing file
hello.cpp.
C++ Standard Library header files
As programs grow larger and larger (and include more files), it becomes increasingly tedious to
have to forward declare every function you want to use that lives in a different file. Wouldn’t it
be nice if you could put all your declarations in one place?

C++ code files (with a .cpp extension) are not the only files commonly seen in C++ programs.
The other type of file is called a header file, sometimes known as an include file. Header files
usually have a .h extension, but you will sometimes see them with a .hpp extension or no
extension at all. The purpose of a header file is to hold declarations for other files to use
The interface of C++ standard library is defined by the following collection of header files.
Utilities library
General purpose utilities: program control, dynamic memory
<cstdlib>
allocation, random numbers, sort and search

<csignal> Functions and macro constants for signal management

<csetjmp> Macro (and function) that saves (and jumps) to an execution context

<cstdarg> Handling of variable length argument lists

<typeinfo> Runtime type information utilities

<typeindex> (since C++11) std::type_index

<type_traits> (since C++11) Compile-time type information

<bitset> std::bitset class template

UNIT-1: Introduction to OOPs & Input and Output in C++


29
<functional> Function objects, designed for use with the standard algorithms

<utility> Various utility components

<ctime> C-style time/date utilites

<chrono> (since C++11) C++ time utilites

<cstddef> typedefs for types such as size_t, NULL and others

<initializer_list> (since C++11) std::initializer_list class template

<tuple> (since C++11) std::tuple class template

<any> (since C++17) std::any class template

<optional> (since C++17) std::optional class template

Dynamic memory management

<new> Low-level memory management utilities

<memory> Higher level memory management utilities

<scoped_allocator> (since C++11) Nested allocator class

<memory_resource> (since C++17) Polymorphic allocators and memory resources

Numeric limits

<climits> limits of integral types

<cfloat> limits of float types

<cstdint> (since C++11) fixed-size types and limits of other types

<cinttypes> (since C++11) formatting macros , intmax_t and uintmax_t math and conversions

<limits> standardized way to query properties of arithmetic types

Error handling

<exception> Exception handling utilities

<exception_list> (since C++17) Exception handling for parallel algorithms

<stdexcept> Standard exception objects

<cassert> Conditionally compiled macro that compares its argument to zero

<system_error> (since C++11) defines std::error_code, a platform-dependent error code

<cerrno> Macro containing the last error number

Strings library

<cctype> functions to determine the type contained in character data

UNIT-1: Introduction to OOPs & Input and Output in C++


29
<cwctype> functions for determining the type of wide character data

<cstring> various narrow character string handling functions

<cwchar> various wide and multibyte string handling functions

<cuchar> (since C++11) C-style Unicode character conversion functions

<string> std::basic_string class template

<string_view> (since C++17) std::basic_string_view class template

Containers library

<array> (since C++11) std::array container

<vector> std::vector container

<deque> std::deque container

<list> std::list container

<forward_list> (since C++11) std::forward_list container

<set> std::set and std::multiset associative containers

<map> std::map and std::multimap associative containers

std::unordered_set and std::unordered_multisetunordered associative


<unordered_set> (since C++11)
containers

std::unordered_map and std::unordered_multimapunordered associative


<unordered_map> (since C++11)
containers

<stack> std::stack container adaptor

<queue> std::queue and std::priority_queue container adaptors

Algorithms library

<algorithm> Algorithms that operate on containers

<execution_policy> (C++17) Predefined execution policies for parallel versions of the algorithms

Iterators library

<iterator> Container iterators

Numerics library

<cmath> Common mathematics functions

<complex> Complex number type

<valarray> Class for representing and manipulating arrays of values

<random> (since C++11) Random number generators and distributions

UNIT-1: Introduction to OOPs & Input and Output in C++


29
<numeric> Numeric operations on values in containers

<ratio> (since C++11) Compile-time rational arithmetic

<cfenv> (since C++11) Floating-point environment access functions

Input/output library

<iosfwd> forward declarations of all classes in the input/output library

<ios> std::ios_base class, std::basic_ios class template and several typedefs

<istream> std::basic_istream class template and several typedefs

<ostream> std::basic_ostream, std::basic_iostream class templates and several typedefs

<iostream> several standard stream objects

std::basic_fstream, std::basic_ifstream,std::basic_ofstream class templates and


<fstream>
several typedefs

std::basic_stringstream, std::basic_istringstream,std::basic_ostringstream class


<sstream>
templates and several typedefs

<strstream> std::strstream, std::istrstream, std::ostrstream(deprecated)

<iomanip> Helper functions to control the format or input and output

<streambuf> std::basic_streambuf class template

<cstdio> C-style input-output functions

Localization library

<locale> Localization utilities

<clocale> C localization utilities

<codecvt> (since C++11) Unicode conversion facilities

Regular Expressions library

<regex> (since C++11) Classes, algorithms and iterators to support regular expression processing

Atomic Operations library

<atomic> (since C++11) Atomic operations library

Thread support library

<thread> (since C++11) std::thread class and supporting functions

<mutex> (since C++11) mutual exclusion primitives

<shared_mutex> (since C++14) shared mutual exclusion primitives

<future> (since C++11) primitives for asynchronous computations

<condition_variable> (since C++11) thread waiting conditions

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Files system library

<filesystem> (since C++17) std::path class and supporting functions

Experimental libraries
Standard libraries extensions and Extensions for
<experimental/algorithm> (library fundamentals TS)
Parallelism

<experimental/any> (library fundamentals TS) Standard libraries extensions

<experimental/chrono> (library fundamentals TS) Standard libraries extensions

<experimental/deque> (library fundamentals TS) Standard libraries extensions

<experimental/execution_policy> (parallelism TS) Extensions for Parallelism

<experimental/exception_list> (parallelism TS) Extensions for Parallelism

<experimental/filesystem> (filesystem TS) Filesystem library

<experimental/forward_list> (library fundamentals TS) Standard libraries extensions

<experimental/future> (library fundamentals TS) Standard libraries extensions

<experimental/list> (library fundamentals TS) Standard libraries extensions

<experimental/functional> (library fundamentals TS) Standard libraries extensions

<experimental/map> (library fundamentals TS) Standard libraries extensions

<experimental/memory> (library fundamentals TS) Standard libraries extensions

<experimental/memory_resource> (library fundamentals TS) Standard libraries extensions

<experimental/numeric> (parallelism TS) Extensions for Parallelism

<experimental/optional> (library fundamentals TS) Standard libraries extensions

<experimental/ratio> (library fundamentals TS) Standard libraries extensions

<experimental/regex> (library fundamentals TS) Standard libraries extensions

<experimental/set> (library fundamentals TS) Standard libraries extensions

<experimental/string> (library fundamentals TS) Standard libraries extensions

<experimental/string_view> (library fundamentals TS) Standard libraries extensions

<experimental/system_error> (library fundamentals TS) Standard libraries extensions

<experimental/tuple> (library fundamentals TS) Standard libraries extensions

<experimental/type_traits> (library fundamentals TS) Standard libraries extensions

UNIT-1: Introduction to OOPs & Input and Output in C++


29
<experimental/unordered_map> (library fundamentals TS) Standard libraries extensions

<experimental/unordered_set> (library fundamentals TS) Standard libraries extensions

<experimental/utility> (library fundamentals TS) Standard libraries extensions

<experimental/vector> (library fundamentals TS) Standard libraries extensions

C compatibility headers

<ciso646> empty header. The macros that appear in iso646.h in C are keywords in C++

<ccomplex> (since C++11) simply includes the header <complex>

simply includes the headers <ccomplex> and <cmath>: the overloads equivalent to the
<ctgmath> (since C++11)
contents of the C headertgmath.h are already provided by <cmath> and <complex>

<cstdalign> (since C++11) defines one compatibility macro constant

<cstdbool> (since C++11) defines one compatibility macro constant

Deprecated headers

<assert.h> (deprecated) behaves as if each name from <cassert> is placed in global namespace

<complex.h> (deprecated) behaves as if each name from <ccomplex> is placed in global namespace

<ctype.h> (deprecated) behaves as if each name from <cctype> is placed in global namespace

<errno.h> (deprecated) behaves as if each name from <cerrno> is placed in global namespace

<fenv.h> (deprecated) behaves as if each name from <cfenv> is placed in global namespace

<float.h> (deprecated) behaves as if each name from <cfloat> is placed in global namespace

<inttypes.h> (deprecated) behaves as if each name from <cinttypes> is placed in global namespace

<iso646.h> (deprecated) behaves as if each name from <ciso646> is placed in global namespace

<limits.h> (deprecated) behaves as if each name from <climits> is placed in global namespace

<locale.h> (deprecated) behaves as if each name from <clocale> is placed in global namespace

<math.h> (deprecated) behaves as if each name from <cmath> is placed in global namespace

<setjmp.h> (deprecated) behaves as if each name from <csetjmp> is placed in global namespace

<signal.h> (deprecated) behaves as if each name from <csignal> is placed in global namespace

<stdalign.h> (deprecated) behaves as if each name from <cstdalign> is placed in global namespace

<stdarg.h> (deprecated) behaves as if each name from <cstdarg> is placed in global namespace

<stdbool.h> (deprecated) behaves as if each name from <cstdbool> is placed in global namespace

<stddef.h> (deprecated) behaves as if each name from <cstddef> is placed in global namespace

UNIT-1: Introduction to OOPs & Input and Output in C++


29
<stdint.h> (deprecated) behaves as if each name from <cstdint> is placed in global namespace

<stdio.h> (deprecated) behaves as if each name from <cstdio> is placed in global namespace

<stdlib.h> (deprecated) behaves as if each name from <cstdlib> is placed in global namespace

<string.h> (deprecated) behaves as if each name from <cstring> is placed in global namespace

<tgmath.h> (deprecated) behaves as if each name from <ctgmath> is placed in global namespace

<time.h> (deprecated) behaves as if each name from <ctime> is placed in global namespace

<uchar.h> (deprecated) behaves as if each name from <cuchar> is placed in global namespace

<wchar.h> (deprecated) behaves as if each name from <cwchar> is placed in global namespace

<wctype.h> (deprecated) behaves as if each name from <cwctype> is placed in global namespace

Lecture-9: Introduction to Input and output and Streams in C++

INPUT AND OUTPUT IN C++


Applications generally involve reading a large amount of data from input devices and sending
them to the output devices. Hence to control such operations every language provides a set of in-
built functions. C++ supports all Input /Output functions of C. C++ also has library functions. A
library is a set of .obj files, which is linked to the program and gives additional support to its
functions. A programmer can use the library functions in the programs. The library is also called
as iostream library. The difference between C and C++ I/O functions is that C functions do not
support object-oriented platform whereas C++ supports it.
STREAMS IN C++
C++ supports a number of Input/output (I/O) operations to read and write operations. These C++
I/O functions make it possible for the user to work with different types of devices such as
keyboard, disk, tape drivers etc. The stream is an intermediator between I/O devices and the user.
The Stream is flow of data in bytes in sequence. If data is received from input devices in
sequence then it is called as source stream and when the data is passed to output devices then it is
called as destination stream.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Fig.2.1 Streams and I/O devices
Data in source stream can be used as input data by the program. So, the source stream is also
called as input stream. The destination stream that collects output data from the program is
known as output stream. As mentioned earlier, the stream is an intermediator between I/O
devices and the user. The input stream pulls the data from keyboard or storage devices such as
hard disk, floppy disk etc.

Fig.2.2 C++ Input and output streams

Lecture-10: Pre-Defined Streams and Stream Classes


PRE-DEFINED STREAMS
C++ has a number of pre-defined streams. These pre-defined streams are also called as standard
I/O objects. These streams are automatically activated when the program execution starts. Table
2.1 describes the pre-defined streams.
Table 2.1 Predefined C++ stream or I/O global objects

UNIT-1: Introduction to OOPs & Input and Output in C++


29
A simple program is illustrated using above I/O objects as follows.

Fig.2.3 Frequently used unformatted input functions with cin object

Fig.2.4 Frequently used unformatted output functions with cout object


Example:
#include<iostream.h>
#include<conio.h>
Void main()
{
clrscr();
cout<<”\n hai dileep”;
cerr<<”\n hai dileep”;
UNIT-1: Introduction to OOPs & Input and Output in C++
29
clog<<”\n hai dileep”;
}
Output:
hai dileep
hai dileep
Explanation: In this program the pre-defined object cout, cerr, and clog are used to display
messages “dileep” on the screen.
STREAM CLASSES
C++ streams are based on class and object theory. C++ has a number of stream classes that are
used to work with console and file operations. These classes are known as stream classes. Figure
2.6 indicates the stream classes. All these classes are declared in the header file iostream.h. The
file iostream.h must be included in the program if we are using functions of these classes.

Fig.2.6 (a) Stream classes

Fig. 2.6 (b) Hierarchy of stream classes


As described in Figure 2.6 (a) the classes istream and ostream are derived classes of base class
ios. The ios class contains member variable object streambuf.v The streambuf places the buffer.
The member function of streambuf class handles the buffer by providing the facilities to flush
clear and pour the buffer. The class iostream is derived from the classes istream and ostream by

UNIT-1: Introduction to OOPs & Input and Output in C++


29
using multiple inheritance. The ios class is a virtual class and this is to avoid ambiguity that
frequently appears in multiple inheritance.

Lecture-11: Formatted , Unformatted Data and Unformatted Console


I/O Operations
FORMATTED AND UNFORMATTED DATA
Formatting means representation of data with different settings as per the requirement of the user.
The various settings that can be done are number format, field width, decimal points etc. If the
user needs to accept or display data in hexa-decimal format, manipulators with I/O functions are
used. The data obtained or represented with these manipulators are known as formatted data.
The data accepted or printed with default setting by the I/O function of the language is known as
unformatted data. For example, when the cin statement is executed it asks for a number.
Example:
cout<<hex<<15;
The above statement converts decimal 15 to hexadecimal F.
UNFORMATTED CONSOLE I/O OPERATIONS
Input and Output Streams
The input stream uses cin object to read data and the output stream uses cout object to display
data on the screen. The cin and cout are predefined streams for input and output of data.
INPUT STREAM:
The input stream does read operation through keyboard. It uses cin as object. The cin statement
uses >> (extraction operator) before variable name. The cin statement is used to read data
through the input device.
Syntax:
cin >>variable
Example:
int v1;
float v2;
char v3;

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Fig.2.7 Working of cin and cout statements
OUTPUT STREAM:
The output stream manages output of the stream i.e., displays contents of variables on the screen.
It uses << insertion operator before variable name.
Syntax:
cout<<variable
Example: 1
cout <<v1 <<v2 <<v3…<<vn;
where v1, v2, and v3 are variables. The above statement displays the contents of these variables
on the screen.
Example: 2
int weight; // Displays integer value
cout<<weight;
Lecture-12: Member Functions of Istream Class and Formatted

Console I/O Operations


MEMBER FUNCTIONS OF ISTREAM CLASS
The istream contains following functions that can be called using cin object.
peek( ): It returns the succeeding characters without extraction.
Example: cin.peek( )==‘#’;
Where cin is an object and ‘#’ is a symbol to be caught in the stream.
ignore ( ): The member function ignore( ) has two arguments, maximum number of characters to
avoid, and the termination character.
Example: cin.ignore(1,‘#’);

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Example 1: Write a program to demonstrate the use of peek( ) and ignore( ) functions.
# include <iostream.h>
# include <conio.h>
main( )
{
char c;
clrscr( );
cout <<"enter text (press F6 to end:) ";
while(cin.get(c))
{
cout<<c;
while(cin.peek( )==‘#’)
{
cin.ignore(1,‘#’);
}
}
return 0;
}

FORMATTED CONSOLE I/O OPERATIONS


C++ provides various formatted console I/O functions for formatting the output.
They are of three types.
(1) ios class function and flags
(2) Manipulators
(3) User-defined output functions
The ios grants operations common to both input and output. The classes derived from ios are
(istream, ostream, and iostream) special I/O with high-level formatting operations: The iostream
class is automatically loaded in the program by the compiler. Figure 2.6 describes hierarchy of
stream classes.
(a) istream performs formatted input.
(b) ostream performs formatted output.
(c) iostream performs formatted input and output.
The streambuf allows an abstraction for connecting to a physical device. Classes derived from it
work with files, memory, etc. The ios communicates to a streambuf. It keeps information on the
state of the streambuf (good, bad, eof, etc.), and saves flags for use by istream and ostream.
The streambuf class controls the buffer and its related member functions. It allows the ability to
fill, flush, and empty the buffer.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
 The streambuf is an object of ios class. The base class of input and output stream classes
is ios class.
 The istream and ostream classes are derived classes of ios class and control input and
output streams.
 The iostream class is derived class of istream and ostream. It provides input and output
functions to control console operations.

Fig.2.9 Formatted functions with cout object


Declaration:
(a) int width( );
(b) int width(int);
(a) ios::width :
Lecture-13: Bit Fields and Flags without Bit Fields
BIT FIELDS
The ios class contains the setf( ) member function. The flag indicates the format design.
The syntax of the unsetf( ) function is used to clear the flags. The syntaxes of function are
as follows.
Syntax: cout.setf(v1, v2);
Syntax: cout.unsetf(v1);
where, variable v1 and v2 are two flags. Table 2.4 describes the flag and bit-field setting
that can be used with this function. The unsetf( ) accepts setting of v1 and v2 arguments.
Declaration:
static const long adjustfield;
(a) ios::floatfield
It is a data member and used with setf( ) function to set the float point notation to
scientific or fixed. It is declared as follows.

FLAGS WITHOUT BIT FIELD


The flags described in Table 2.5 have no corresponding bit fields. The programs on these
functions are illustrated below:
Table 2.5 Flags without bit fields

UNIT-1: Introduction to OOPs & Input and Output in C++


29
Explanation: In the below program, the setting ios::showpos displays the + sign before
the number 1254. The setting ios::showpoint displays the trailing zeroes after the number
1254.524. The setting ios::hex converts the decimal number to hexadecimal. It uses small
letters. The setting ios::uppercase displays the hexadecimal number in uppercase.
Example:
# include <iostream.h>
# include <conio.h>
void main( )
{
clrscr( );
cout.setf(ios::showpos);
cout <<1254;
cout.setf(ios::showpoint);
cout <<"\n"<<1254.524;
cout.setf(ios::hex,ios::basefield);
cout <<"\n"<<184;
cout.setf(ios::uppercase);
cout <<"\n"<<184;
}
Lecture-14: Manipulators, User Defined Manipulators
MANIPULATORS
The output formats can be controlled using manipulators. The header file iomanip.h has a
set of functions. Effect of these manipulators is the same as ios class member functions.
Every ios member function has two formats. The manipulator can be used with cout( )
statement as given below.
cout <<m1 <<m2 <<v1;
Here m1 and m2 are two manipulators and v1 is any valid C++ variable.
The manipulator hex, dec, oct, ws, endl, and flush are defined in iostream.h. The
manipulator setbase, width( ), fill( ) etc. that requires an argument are defined in
iomanip.h.

UNIT-1: Introduction to OOPs & Input and Output in C++


29
USER-DEFINED MANIPULATORS
The programmer can also define his/her own manipulator according to the requirements
of the program. The syntax for defining manipulator is given below
Syntax:
ostream & m_name( ostream & o )
{
statement1;
statement2;
return o;
}
MANIPULATOR WITH ONE PARAMETER
The prototype declared in the iomanip.h as described earlier allows us to define
individual set of macros. The manipulator can be created without the use of int or long
arguments.
# include <iostream.h>
# include <iomanip.h>
# include <string.h>
# include <conio.h>
ostream& fc (ostream& ost, int iw)
{
for (int k=0;k<((75-iw)/2);k++)
ost <<" ";
return (ost);
}
OMANIP (int) middle (int iw)
{
return OMANIP (int) (fc,iw);
}
int main( )
{
clrscr( );
char *m=" * Well come *";
cout <<middle (strlen(m)) <<m;
return 0;
}

UNIT-1: Introduction to OOPs & Input and Output in C++


29

Você também pode gostar