Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Object Oriented Programming with C++

1. Explain the use of scope resolution operator in C++ with an example.


1. Variables will be declared in three basic places: inside functions, in the definition of function
parameters, and outside of all functions. These are local variables, formal parameters, and global
variables respectively.
2. In case if these variables have same declaration, the declaration of an inner block’s variable hides
the declaration of the outer block’s variable. ( A new declaration is given to the inner block which
overrides the declaration of the outer block )
3. In order to access a variable from the outer block from within the inner block, The Scope
Resolution Operator is used.
4. Scope Resolution Operator helps to access the global version of a variable.
5. The symbol used is : :
6. The following example displays the working of a Scope Resolution Operator
SOURCE CODE (Use of Scope Resolution Operator)

7. In the above example both the Local and Global variables have different values, helping to identify
the version accessed while printing.

2. Explain use of functions in C++.


1. A function is a group of statements that together perform a task. They can also be referred as self-
contained block of statements.
2. A function declaration tells the compiler about a function's name, return type, and parameters.
3. A function definition provides the actual body of the function.
4. The general form of a C++ function definition is as follows:

ReturnType FunctionName (parameter list)


{
Body of the function
}

1
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

5. ReturnType: This will suggest what the function will return. It can be int, char, some pointer or
even a class object. There can be functions which do not return anything, they are mentioned
using void.
6. FunctionName: the functions are called / invoked using FunctionName.
7. Parameters: These are variables to hold values of arguments passed while a function is called. A
function may or may not contain a parameter list.

2(A) Explain the use of Inline Functions is C++


1. C++ inline function is powerful concept that is commonly used with classes.
2. If a function is inline, the compiler places a copy of the code of that function at each point where
the function is called at compile time.
3. To create an inline function, place the keyword inline before the function name and define the
function before any calls are made to the function.
4. The compiler can ignore the inline qualifier in case defined function is more than a line.
5. A function definition in a class definition is an inline function definition, even without the use of
the inline keyword.
6. The following example displays the use of inline functions
SOURCE CODE (Use of inline functions)

2
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

2(B) Explain Function Overloading in C++

1. C++ allows you to specify more than one


definition for a function name in the same
scope, which is called function overloading.
2. An overloaded declaration for a function is a
declaration that had been declared with the
same name as a previously declared
declaration in the same scope, except that
both declarations have different arguments
and different definition (implementation).
3. When you call an overloaded function, the
compiler determines the most appropriate
definition to use by comparing the argument
types used to call the function or operator
with the parameter types specified in the
definitions. The process of selecting the most
appropriate overloaded function is called
overload resolution.
4. There can be multiple definitions for the
same function name in the same scope.
5. The definition of the function must differ
from each other by the types and/or the
number of arguments in the argument list.
6. The function declarations that differ only by
return type cannot be overloaded.

3
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

Passing arguments to a function has two methods: (A) Call by Value and (B) Call by Reference

2(C) Explain CALL BY VALUE in C++


1. This method, copies the actual value of an argument into the formal parameter of the function.
2. In this case, changes made to the parameter inside the function have no effect on the argument.
3. By default, C++ uses call by value to pass arguments.
4. In general, this means that code within a function cannot alter the arguments used to call the
function.
5. The following code displays the use of the above swap () function using Call by Value method
SOURCE CODE (Use of Call by Value)

6. This shows that there is no change in the values though they had been changed inside the function.

4
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

2(D) Explain Call by Reference (Use of Pointers and References)

1. This method copies the reference of an argument into the formal parameter.
2. Inside the function, the reference is used to access the actual argument used in the call. This means
that changes made to the parameter affects the arguments passed.
3. A reference provides an alias (an alternate name) for the variable.
4. To pass the value by reference, argument reference is passed to the functions just like any other
value.
5. So to declare the function parameters as reference types as in the following function
Swap (), which exchanges the values of the two integer variables pointed to by its arguments.
6. The following example displays use of Call by Reference.
SOURCE CODE: (Use of Call by Reference)

5
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

3. Explain what are class members?


1. A member function of a class is a function that has its definition or its prototype within the class
definition like any other variable. It operates on any object of the class of which it is a member,
and has access to all the members of a class for that object.
2. Member functions can be defined within the class definition or separately using scope resolution
operator.
3. Defining a member function within the class definition declares the function inline, even if you do
not use the inline specifiers.
4. getVolume() can be defined as follows:

(Method ONE: Inside class declaration of member functions)

(Method TWO: Outside class declaration of member functions)

5. Objects Creation: A member function will be called using a dot operator [ . ] on an object where it
will manipulate data related to that object only as follows:

6
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

7
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

4. Explain what are Access Specifiers in C++


1. Data hiding is one of the important features of Object Oriented Programming which allows
preventing the functions of a program to access directly the internal representation of a class type.
2. The access restriction to the class members is specified by the labeled public,
private, and protected sections within the class body.
3. The keywords public, private, and protected are called access modifiers / access specifiers.
4. A class can have multiple public, protected, or private labeled sections.
5. The default access for members and classes is private.
6. Public: A public member is accessible from anywhere outside the class but within a program.
7. Private: A private member variable or function cannot be accessed, or even viewed from outside
the class. Only the class and friend functions can access private members.
8. Practically, we define data in private section and related functions in public section so that they
can be called from outside of the class as shown in the following program.

5. Explain the use of constructors in C++


1. A class constructor is a special member function of a class that is executed whenever we create
new objects of that class.
2. A constructor will have exact same name as the class and it does not have any return type at all,
not even void. Constructors can be very useful for setting initial values for certain member
variables.

5(A) Part One - Default Constructors

1. A default constructor is a constructor which can be called with no arguments (either defined with
an empty parameter list, or with default arguments provided for every parameter)
2. The default constructors are called during default initializations and value initializations.

5(B) Part Two - Parameterized Constructors

1. A default constructor does not have any parameter, but if required, a constructor can have
parameters.
2. This helps to assign initial value to an object at the time of its creation.
8
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

5(C) Part Three – Copy Constructors


1. The copy constructor is a constructor which creates an object by initializing it with an object of the
same class, which has been created previously. The copy constructor is used to:

a. Initialize one object from another of the same type.

b. Copy an object to pass it as an argument to a function.

c. Copy an object to return it from a function.

2. If a copy constructor is not defined in a class, the compiler itself defines one.
3. If the class has pointer variables and has some dynamic memory allocations, then it is a must to
have a copy constructor. The most common form of copy constructor is shown in the below
example.

6. Destructors
1. A destructor is a special member function of a class that is executed whenever an object of its class
goes out of scope.
2. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither
return a value nor can it take any parameters. Destructor can be very useful for releasing
resources before coming out of the program like closing files, releasing memories etc.

9
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

10
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

11
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

7. Explain what are friend functions with an example?


1. One of the important concepts of OOP is data hiding; this implies that a nonmember function
cannot access an object's private or protected data.
2. But, sometimes this restriction may force programmer to write long and complex codes. So, there
is mechanism built in C++ programming to access private or protected data from non-member
function which is friend function and friend class.
3. If a function is defined as a friend function then, the private and protected data of class can be
accessed from that function. The complier knows a given function is a friend function by its
keyword friend.
4. The declaration of friend function should be made inside the body of class (can be anywhere
inside class either in private or public section) starting with keyword friend.

5. Syntax:

12
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

6. Here, friend function func() is declared inside Distance class. So, the private data can be accessed
from this function.

8. Explain operator overloading in C++


1. Overloaded operators are functions with special names the keyword operator followed by the
symbol for the operator being defined. Like any other function, an overloaded operator has a
return type and a parameter list.
2. Every operator has its own pre-defined meaning/purpose.
3. Overloading an operator will give a special meaning (definition) to the default operator as per
programmer’s requirement.
4. Defining Operator Overloading:
A special function is used to specify what it means in relation to the class to which the operator is
applied. This function is called operator function.
5. The general form of operator function is :

13
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

6. Rules for Overloading Operators:


1. Only existing operators can be overloaded. New operators cannot be overloaded.
2. The overloaded operator must have at least one operand that is of user defined type.
3. We cannot change the basic meaning of an operator. That is to say, we cannot redefine the
plus (+) operator to subtract one value from the other.
4. Overloaded operators follow the syntax rules of the original operators. They cannot be
overridden.
5. There are some operators that cannot be overloaded like size of operator (sizeof),
membership operator (.), pointer to member operator (.*), scope resolution operator (::),
conditional operators (?:)
6. We cannot use “friend” functions to overload certain operators. However, member function
can be used to overload them. Friend Functions cannot be used with assignment
operator(=), function call operator(()), subscripting operator([]), class member access
operator(->) etc.
7. Unary operators, overloaded by means of a member function, take no explicit arguments
and return no explicit values, but, those overloaded by means of a friend function, take one
reference argument (the object of the relevant class).
8. Binary operators overloaded through a member function take one explicit argument and
those which are overloaded through a friend function take two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand
operand must be an object of the relevant class.
10. Binary arithmetic operators such as +,-,* and / must explicitly return a value. They must
not attempt to change their own arguments.

8(A) Demonstrate Unary Operator Overloading in C++ with an example:


1. The unary operators operate on a single operand and following are the examples of Unary
operators:

a. The increment (++) and decrement (--) operators.


b. The unary minus (-) operator.
c. The logical not (!) operator.

2. The unary operators operate on the object for which they were called and normally, this operator
appears on the left side of the object, example !obj, -obj, and ++obj but sometime they can be used
as postfix as well like obj++ or obj--.Following example explains how minus (-) operator can be
overloaded for prefix as well as postfix usage.

14
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

8(B) Demonstrate Binary Operator Overloading in C++ with an example.


1. The binary operators take two arguments and following are the examples of Binary operators. You
use binary operators very frequently like addition (+) operator, subtraction (-) operator and
division (/) operator.

2. Following example explains how addition (+) operator can be overloaded. Similar way, you can
overload subtraction (-) and division (/) operators.

15
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

When the above code is


compiled and executed, it
produces the following result:

16
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

17
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

9. Inheritance
1. Inheritance is one of the key features of object-oriented programming including C++ which allows
user to create a new class (derived class) from a existing class (base class).
2. The derived class inherits all features from a base class and it can have additional features of its
own.
3. Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality and
fast implementation time.
4. When creating a class, instead of writing
completely new data members and member
functions, the programmer can designate that the
new class should inherit the members of an
existing class.
5. This existing class is called the base class, and the
new class is referred to as the derived class.
6. The inheritance relationship enables a derived
class to inherit features from its base class.
7. Furthermore, the derived class can add new
features of its own. Therefore, rather than create
completely new classes from scratch, you can take
advantage of inheritance and reduce software
complexity.

9(A) Forms of Inheritance


1. Single Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from one base class.
2. Multiple Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from multiple base class
3. Hierarchical Inheritance: It is the inheritance
hierarchy wherein multiple subclasses inherit
from one base class.

18
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

4. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class
for other classes.
5. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other
four types of inheritance.

(i) Single Inheritance (Base and Derived Classes)

1. A class can be derived from more than one class, which means it can inherit data and functions
from multiple base classes. To define a derived class, we use a class derivation list to specify the
base class.

2. A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

3. Where access-specifier is one of public, protected, or private, and base-class is the name of a
previously defined class.

4. If the access-specifier is not used, then it is private by default.

5. Consider a base class Shape and its derived class Rectangle as follows:

6. When the above code is compiled and executed, it produces


the following result:

19
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

(ii) Multiple Inheritance


1. A C++ class can inherit members from more than one class and here is the extended syntax:

class derived-class: access baseA, access baseB....

2. Here, access is one of public, protected, or private and would be given for every base class and
they will be separated by comma as shown above.
3. Let us try the following example:

4. When the above code is compiled and


executed, it produces the following result:

20
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

(iii) Hierarchical Inheritance


1. Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived
from common base class.
2. Refer the following example:

21
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

(iv) Multilevel Inheritance


1. Multilevel Inheritance is a method where a derived class is derived from another derived class.

22
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

(v) Hybrid Inheritance


1. Hybrid Inheritance is a method where one or more types of inheritance are combined together
and used.

23
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

9. b. Types of Inheritance

How Members of Base Class appear in Derived Class

Member Access
Specifiers Private Members of Base Protected members of base Public members of base

Private Inheritance Inaccessible Private Private

Protected
Inaccessible Protected Protected
Inheritance

Public Inheritance Inaccessible Protected Public

NOTE:
In principle, a derived class inherits every member of a base class except constructor and destructor. It
means private members are also become members of derived class. But they are inaccessible by the
members of derived class.

24
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

10. Polymorphism
1. The word polymorphism means having many forms. Typically, polymorphism occurs when there
is a hierarchy of classes and they are related by inheritance.
2. C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that calls the function.

3. Consider the following example where a base class has been derived by other two classes:

25
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

4. When the above code is compiled and executed, it produces the following result:

5. The above output is incorrect


6. The reason for the incorrect output is that the call of the function area () is being set once by the
compiler as the version defined in the base class.
7. This is called static resolution of the function call, or static linkage - the function call is fixed
before the program is executed. This is also sometimes called early binding because the area ()
function is set during the compilation of the program.
8. But now, let's make a slight modification in our program and precede the declaration of area() in
the Shape class with the keyword virtual so that it looks like this:

26
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

9. This time, the compiler looks at the contents of the pointer instead of its type.
10. Since addresses of objects of tri and rec classes are stored in *shape the respective area () function
is called.
11. As you can see, each of the child classes has a separate implementation for the function area ().
12. This is how polymorphism is generally used. You have different classes with a function of the same
name, and even the same parameters, but with different implementations.

10(A) Virtual Functions

1. A virtual function is a function in a base class that is declared using the keyword virtual. Defining
in a base class a virtual function, with another version in a derived class, signals to the compiler
that we don't want static linkage for this function.
2. What we do want is the selection of the function to be called at any given point in the program to
be based on the kind of object for which it is called.
3. This sort of operation is referred to as dynamic linkage or late binding.

10(B) Pure Virtual Functions

1. It's possible that you'd want to include a virtual function in a base class so that it may be redefined
in a derived class to suit the objects of that class, but that there is no meaningful definition you
could give for the function in the base class.
2. We can change the virtual function area() in the base class to the following:

3. The line virtual int area () = 0 tells the compiler


that the function has no body and above virtual function
will be called pure virtual function.
4. A class containing pure virtual functions is called an
abstract base class.

27
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

10(C) Rules for Virtual Functions:

1. The virtual functions must be members of some class.


2. They cannot be static member.
3. They are accessed by using object pointes.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class versions
must be identical. If two functions with the same name have different prototypes, C++ considers
them as overloaded functions, and the virtual function mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can point to any type of the derived objects, the reverse is not true. That is to
say, we cannot use a pointer to a derived class to access an object of the base type.
9. When a base pointer points to a derived class, incrementing or decrementing it will not make it to
point to the next object of the derived class. It is incremented or decremented only relative to its
base type. Therefore, we should not use this method to move the pointer to the next object.
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived
class. In such cases, calls will invoke the base function.

28
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

10(D) Distinguish between Early Binding and Late Binding

1. Before concluding on virtual functions and run-time polymorphism, there are two terms that need
to be defined because they are used frequently in discussions of C++ and object-oriented
programming: early binding and late binding.
2. Early binding refers to events that occur at compile time. In essence, early binding occurs when
all information needed to call a function is known at compile time. (Put differently, early binding
means that an object and a function call are bound during compilation.)
3. Examples of early binding include normal function calls (including standard library functions),
overloaded function calls, and overloaded operators.
4. The main advantage to early binding is efficiency. Because all information necessary to call a
function is determined at compile time, these types of function calls are very fast.
5. Late binding is the opposite of early binding. As it relates to C++, late binding refers to function
calls that are not resolved until run time.
6. Virtual functions are used to achieve late binding. As you know, when access is via a base pointer
or reference, the virtual function actually called is determined by the type of object pointed to by
the pointer.
7. Because in most cases this cannot be determined at compile time, the object and the function are
not linked until run time. The main advantage to late binding is flexibility.
8. Unlike early binding, late binding allows you to create programs that can respond to events
occurring while the program executes without having to create a large amount of "contingency
code".
9. Keep in mind that because a function call is not resolved until run time, late binding can make for
somewhat slower execution times.

29
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

11. Files and Streams

1. Before beginning our discussion of the C++ file system, it is necessary to know the difference
between the terms streams and files.
2. The C++ I/O system supplies a consistent interface to the programmer independent of the actual
device being accessed. That is, the C++ I/O system provides a level of abstraction between the
programmer and the device.
3. This abstraction is called a stream and the actual device is called a file. It is important to
understand how streams and files interact.

11(A) Streams:
1. The C++ file system is designed to work with a wide variety of devices, including terminals, disk
drives, and tape drives.
2. Even though each device is very different, the buffered file system transforms each into a logical
device called a stream.
3. All streams behave similarly. Because streams are largely device independent, the same function
that can write to a disk file can also be used to write to another type of device, such as the console.
4. There are two types of streams: text and binary.

5. Text file: It is a file that stores information in ASCII characters. In text files, each line of text is
terminated with a special character known as EOL (End of Line) character or delimiter character.
When this EOL character is read or written, certain internal translations take place.
6. Binary file: It is a file that contains information in the same format as it is held in memory. In
binary files, no delimiters are used for a line and no translations occur here.
11(B) Files:
1. In C++, a file may be anything from a disk file to a terminal or printer.
2. You associate a stream with a specific file by performing an open operation.
3. Once a file is open, information may be exchanged between it and your program.
4. Not all files have the same capabilities. For example, a disk file can support random access while
some printers cannot. This brings up an important point about the C++ I/O system: All streams are
the same but all files are not.

11(C) File Classes:


1. To perform file I/O, you must include the header <fstream> in your program. It defines several
classes, including ifstream, ofstream, and fstream.
2. These classes are derived from istream, ostream, and iostream, respectively. Remember, istream,
ostream, and iostream are derived from ios, so ifstream, ofstream, and fstream also have access
to all operations defined by ios.
3. Another class used by the file system is filebuf, which provides low-level facilities to manage a file
stream. Usually, you don't use filebuf directly, but it is part of the other file classes.
4. So far, we have been using the iostream standard library, which provides cin and cout methods for
reading from standard input and writing to standard output respectively.
5. We will now check how to read and write from a file.

30
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

6. This requires another standard C++ library called fstream, which defines three new data types:

Data Type Description

This data type represents the output file stream and is used to create files and to
ofstream
write information to files.

This data type represents the input file stream and is used to read information from
ifstream
files.

This data type represents the file stream generally, and has the capabilities of both
fstream ofstream and ifstream which means it can create files, write information to files,
and read information from files.

Opening a File:
1. A file must be opened before you can read from it or write to it.
2. Either the ofstream or fstream object may be used to open a file for writing.
3. The ifstream object is used to open a file for reading purpose only.
4. Following is the standard syntax for open () function, which is a member of fstream, ifstream, and
ofstream objects.

Void open(const char *filename, ios::openmode mode);

5. Here, the first argument specifies the name and location of the file to be opened and the second
argument of the open () member function defines the mode in which the file should be opened.
Mode Flag Description

ios::app Append mode. All output to that file to be appended to the end.

Ios::ate Open a file for output and move the read/write control to the end of the file.

Ios::in Open a file for reading.

Ios::out Open a file for writing.

Ios::trunc If the file already exists, its contents will be truncated before opening the file.

Ios::binary Binary File

ios::nocreate Open fails if the file does not exist

ios::noreplace Open fails if the file already exist.

6. You can combine two or more of these values by OR-ing them together.

31
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

7. For example if you want to open a file in write mode and want to truncate it in case it already
exists, following will be the syntax:
ofstream outfile;
outfile.open (“file.dat”, ios::out | ios::in);

8. In a similar way, you can open a file for reading and writing purpose as follows:

fstream afile;
afile.open (“file.dat”, ios::out | ios::in);

Closing a File:
1. When a C++ program terminates it automatically closes flushes all the streams, release all the
allocated memory and close all the opened files.

2. But it is always a good practice that a programmer should close all the opened files before
program termination.

3. Following is the standard syntax for close () function, which is a member of fstream, ifstream, and
ofstream objects.

Void close();

Writing to a File:

1. While doing C++ programming, you write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to output information to the screen.
2. The only difference is that you use an ofstream or fstream object instead of the cout object.

Reading from a File:

1. You read information from a file into your program using the stream extraction operator (>>) just
as you use that operator to input information from the keyboard.
2. The only difference is that you use an ifstream or fstream object instead of the cin object.

32
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

Read & Write Example:


1. Following is the C++ program which opens a file in reading and writing mode. After writing
information inputted by the user to a file named afile.dat, the program reads information from the
file and outputs it onto the screen:

2. Above examples make use of additional functions from cin object, like getline () function to read
the line from outside and ignore () function to ignore the extra characters left by previous read
statement.

33
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

Error handling functions:

FUNCTION RETURN VALUE AND MEANING

eof() returns true (non zero) if end of file is encountered while reading; otherwise return false(zero)

fail() return true when an input or output operation has failed

File Pointers and Their Manipulation:


1. All I/O streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read
in the next input operation.
2. The ofstream, like ostream, has a pointer known as the put pointer that points to the location
where the next element has to be written.
3. Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived
from both istream and ostream).
4. These internal stream pointers that point to the reading or writing locations within a stream can
be manipulated using the following member functions:

seekg() moves get pointer(input) to a specified location

seekp() moves put pointer (output) to a specified location

tellg() gives the current position of the get pointer

tellp() gives the current position of the put pointer

5. The other prototype for these functions is:


seekg (offset, refposition);
seekp (offset, refposition);
6. The parameter offset represents the number of bytes the file pointer is to be moved from the
location specified by the parameter refposition.

34
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++

7. The refposition takes one of the following three constants defined in the ios class.

ios::beg --- start of the file


ios::cur --- current position of the pointer
ios::end --- end of the file
8. Example:
file.seekg(-10, ios::cur);

35
COMPILED BY – NISHANT KAWA

You might also like