Docsity

C++ I/O: Streams, Text/Binary, Formatted I/O, Variables, Constants, Study notes of Object Oriented Programming

Partial preview of the text

Download C++ I/O: Streams, Text/Binary, Formatted I/O, Variables, Constants and more Study notes Object Oriented Programming in PDF only on Docsity!

  • Session-

I/O Operation

Data types

Variable

Static

Constant

Pointer

Type conversion

Topics covered

Stream in C

Text Stream (^) Binary Stream

<< operator –It can use multiple times in the same line.

Its called Cascading

Cout ,Cin can cascaded

For example

cout<<“\n Enter the Marks”;

cin>> ComputerNetworks>>OODP;

Cascading of Input or Output Operators

Formatted I/O

I/O class function and flages

Manipulators

#include<iostream.h> #define PI 3. main() { cout.precision(3); cout.width(10); cout.fill(‘*’); cout<<PI; Output *****3.

Formatted Input and Output Operations

The setf() is a member function of the ios class that is used to

set flags for formatting output.

syntax -cout.setf(flag, bit-field)

Here, flag defined in the ios class specifies how the output

should be formatted and bit-field is a constant (defined in ios )

that identifies the group to which the formatting flag belongs to.

There are two types of setf()—one that takes both flag and bit-

fields and the other that takes only the flag.

Formatting with flags

• C++ has a header file iomanip.h that contains certain

manipulators to format the output

Formatting Output Using Manipulators

Data Types in C++

Variables

Local Variables Instance Variables^ Static Variables^ Constant Variables

Local variable: These are the variables which are declared within the method of a class.

Example: public class Car { public: void display(int m){ // Method int model=m; // Created a local variable model cout<<model; }

Static variables: Static variables are also called as class variables. These variables have only one copy that is shared by all the different objects in a class. Example: public class Car { public static int tyres; // Created a class variable void init(){ tyres=4; } }

Instance variable: These are the variables which are declared in a class but outside a method, constructor or any block. Example: public class Car { private: String color; // Created an instance variable color Car(String c) { color=c; }}

Constant is something that doesn't change. In C language and C++ we use the keyword const to make program elements constant. Example: const int i = 10; void f(const int i) class Test { const int i; };

CONSTANTS

Constants are identifiers whose value does not change. While

variables can change their value at any time, constants can never

change their value.

Constants are used to define fixed values such as Pi or the charge

on an electron so that their value does not get changed in the

program even by mistake.

A constant is an explicit data value specified by the programmer.

The value of the constant is known to the compiler at the compile

time.