File Handling in C++

File In a computer system files are used to store the necessary information / data stored. There are mainly two types of files:

  1. Text file
  2. Binary file

Text file. It stores information/data in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character.

Binary file. It contains the data/information in the same format as it is held in memory. In binary files, no delimiters or EOF are used for a line for terminate the line

Classes for file stream operation

By using stream classes we can perform different operation such as read a file or write a file. There are some stream classes:

  • ofstream: This stream class is used to write on files
  • ifstream: This stream class is used to read from files
  • fstream:  This stream class is used to both read and write from/to files.

Modes  of  file

Mode

Description

ios::in

open file for reading only

ios::out

open file for writing only

ios::binary

Open file in a binary mode.

ios::ate

Set the initial position at the end of the file.
If the value of flag is not set to any value, then the by default initial position is the beginning of the file.

ios::app

All output operations are performed at the end of the file.

ios::trunc

If the file opened for output operations,then its previous content is deleted and replaced by the new one.

Opening a file:There are two ways for open a file:-

OPENING FILE USING CONSTRUCTOR

OPENING FILE USING open()

Example: Write a program to open a file using constructor & write a character in a file using write() function

Output

Note: In the above program, we are opening a file x.txt which is exists in an E drive with the help of ofstream class object (f).

After that we will enter character (x) in a console and and write the entered character in a file using wrire() function.   Then we will close a file by using close () function.

Use of read() and write() function 

“read () and write()” functions are used  to performs the reading and writing operation in a file in a binary format.

The format of read() and write() function are as follows:  

read( (char*)&p , sizeof(p) );  

write( (char*)&p , sizeof(p) );           

These function receives two argument. The first argument is the address of the variable P and the second argument is the size of variable P in byte.

Example: Write a program to open a file using open () function & write a character in a file using write () function.

Output

Note: In the above program, we are opening a file x.txt which is exists in an E drive with the help of ofstream class object (f).

After that we will enter character (x) in a console and write the entered character in a file using wrire() function.  

Then we will close a file by using close () function.       

Example: Write a program read a character in a file using read () function.

Output

Note: In the above program, we are opening a file x.txt which is exist in a E drive.

After that we will read a  character (a) from a file “x.txt” and write that character in a console.     

Example: Write a program write and read a character in a file using read () & write () function. Use ifstream and ofstream class.

Output

Note: In the above program, we are opening a file x.txt which is exists in a E drive with the help of ofstream class object (f).

After that we will enter character (s) in a console and write the entered character in a file using wrire() function.

Then we will close a file by using close () function.    

Again we will open a x.txt file with the help of ifstream class object (f1) and read a character (s) from a file and write that character in a console.     

 Example:  Write a program to write and read a character in a file using read () & write () function. Use fstream class.

Output

Example: Write a program to write a string in a file using write () function.

Output

Example:  Write a program to read a string in a file using  read() function.

Output

Note:  Assume in a file “x.txt”, ”this is my book” is written. Then read () function will read only first string (this) or first 50 character string.

Example: Write a program to write and read a integer data in a file using read() & write() function. use ifstream and ofstream class.

Output

Example: Write a program to write in a file using  ofstream object.

Output

Example: Write a program to write and read in a file using ofstream & ifstream object.

Output

Note: In the above program, statement (f1>>a) will read only string “aaaa” from a file.  If we will again read string using ifstream object f1 then for example

Output

Find End Of File (EOF)

We can find the end of file by using the eof() function.

The eof() function returns the non-zero value when end of file is detected, otherwise eof() function returns zero.

Example:  Write a program to write and read in a file & display the content of file. Use eof() function.

Output

Note: In the above program, statement               

Initially function eof() return zero and condition will be true.

Then we will read a data from a file and print it in a console.

When function eof() detect the end of file then it will return non-zero value and then condition will be false and while loops terminates. 

 FILE POINTERS AND THEIR MANIPULATION

In a C++ file pointer is used to point to the reading or writing locations within a stream. There are  following member functions of file pointer

Function Description

seekg()

moves pointer to a specified location in write mode

seekp()

moves  pointer to a specified location in read mode

tellp()

gives current position of the pointer  for write

tellg()

gives current position of the pointer for read

Use of tellg()and tellp() functions

tellg() – gives current position of the pointer  for write

tellp() – gives current position of the pointer for read    

Example: Write a program to show the use of tellg() & tellp() function.  

Or

Write a program to show the present position of input and output pointer using tellg() & tellp().

Output

Use of seekg()and seekp() functions

Function

Description

seekg()

moves pointer to a specified location in write mode

seekp()

moves pointer to a specified location in read mode

seekg() and seekp() function have a  two arguments. Their  format is fellows:-  

  seekg( offset , pre_position );  

seekp( offset , pre_position ); 

first argument specifies the number of bites the file pointer is to be shifted from the pre_position of the pointer.        

The pre_position may have one of the following possible values:-     

pre_position

Description

ios :: beg

Beginning of the file

ios :: cur

Current position of the file pointer

ios :: end

End of the file

File Pointer With Its Arguments in a seekg() function

Seek Option

Working

seekg(0,ios :: beg );

Go to the beginning of the file.

seekg(0,ios :: cur );

Rest at the current position.

seekg(0,ios :: end );

Go to the end of the file.

seekg(n,ios :: beg );

Shift file pointer to n+1 byte in the file.

seekg(n,ios :: cur );

Go front by n byte from current position.

seekg(-n,ios :: cur );

Go back by n byte from the present position.

seekg(-n,ios :: end );

Go back by n byte from the end of position.

Example: Write a program to enter text and again second time re-enter text from beginning position and replace the first word and display the content after 10 byte of the file pointer from the beginning of the file. 

Or

Write a program to show the use of seekp() and seekg() function.

Output

Note: In the above program, first, we write statement “aaaa bbbb cccc” then we  will shift the output  file pointer by using seekp() function  (f.seekp(0, ios :: beg);)at the beginning of the file and again write statement ”xxxx” in place of “aaaa”. 

Now the new statement/new content of file is “xxxx bbbb cccc”.

During reading file first we move the input file pointer 10 byte in a forward direction by using seekg() function (f1.seekg(10, ios :: beg);)   then we will read file till end of file.

Example: Write a program using seekg() to achive the following:   

  •  To move the pointer by 15 position.
  • To go backward by 20 byte from the end.
  • To go byte 50 in the file.

First code is

Second code is

Third is

Example:  Write a program to copy the content of one file into another file.

Output

Use of put() and get() function

get()” is a member function of the class fstream. This function reads the single character from a file.

“put()” function is a member of fstream class. The put() function write a single character in the file.

Example: Write a program to write and read the characters in a file using put() & get() function.

Output

Example: Write a program to write and read five characters in a file using put() & get() function.

Output

Use of fail() function

“fail()” stream state member function is used to check whether a file has been opened for input or output successfully or not.

 It returns the non-zero value if an operation is unsuccessful(or file is not opened) and return zero when it is open.

Output

Note: In the above program, fail() return 0 when file is open and condition will be true after that we can perform operation and return non-zero  value  when file is not open and condition will be false.

Categories C++

Exception Handling

In C++ programming, an exception is an abnormal condition that arises/occurs in a source code during run time.  

Or

 Exception is a run time error.  

Example:

Output

When we try to compile this program we will receive an error message because value of 100/a (100/0) is infinite and it can’t be possible to save and print infinite value.    

Handle Exception in C++

In a C++ programming language whenever an exceptional condition is arises it can be handled by the three keywords: try, catch and throw.

The Keyword try:  Any code or statement that we want to monitor for an exception must be put inside the try block.

Syntax for try block:

   Example:  

In an above statement we have written try block.

Inside the try block we have placed a code (b=100/a) in which exception may occur.

Value of “ a” can be any integer value there is no problem but if the value of variable “a” is  zero then error will generate because  100/a (100/0) is infinite and it can’t be possible to save and print infinite value.             

The Keyword throw: If the exception occurs within try block it is automatically thrown by system or it can be manually thrown by using “throw” keyword. Syntax for throw:

throw    (excep);    

when excep is throw catch statement associated with the try block is handle this exception.

The Keyword catch: When the exceptional condition is arises within the try block it is thrown by the keyword throw and catch by the catch statement associated with the try block.

 Syntax for catch block:

Note: If there is no exception within the try block then catch block will never execute.

Example:   Write a program to show exception.

Output

 
compile time error, arithmetic error

 Example:   Write a program to throw an exception when a =0 otherwise perform division.

Output

Example:   Write a program to throw an exception when a =1 otherwise perform subtraction of x and y.

Output

Multiple Catch Block

In a C++ programming we can define multiple catch statement for a single try block. Syntax for multiple catch is:  

Example:   Write a program to throw multiple exception and defined multiple catch statement.

Output

Default catch()

In a C++ exception handling  it is also possible to define single or default catch block for one or more exception of different types.

In such a situation, a single catch block can  catch exception thrown by multiple throw statement.  

Example:   Write a program to throw multiple exception and defined multiple catch statement.

Output

Categories C++

Template in C++

In  C++ programming language, Template is used to declare a group of function or class in a generic manner.

Generic  means that that can process all types of data.

By using template a single function can defined for a group of functions, it is called as a “function template”.

Similarly, when template is associated with classes are known as class template.

Advantage of function template:  It avoid unnecessary repetition of source code.

Using template we can create a single function that can process any type of data .

It can accept data of any type such as int, float, long etc.

    Syntax for declare class template:                       

“template” is a keywords.

“template class <t>”  statement tells the compiler the following class declaration can use the template data type.

Here T is a variable of template type. It is used to define variable of template (generic) type.

Within the angle bracket <> we declare a variable of template.

We can declare one or more variable separated by comma.

“template” always be global and should not be local means template cannot be declared inside of function or class.                       

T  k;     Where , k is variable of type template. 

  Normal Template Function Declaration

Syntax for Template Function

template class < T > type function_name() { // code
}

Example:   Write a program to perform constructor overloading and pass different data type argument.

Output

In the above program we have created three overloaded constructor, first accept integer data as argument and second accepts float data as argument and third constructor accepts character data as argument.

This approach has following disadvantages

  • Re-defining the same function separately for each data type. It increases the requires more time.  
  • Size of program is increased. Hence, occupies more memory.  
  • If constructor/function contains bug, it should be correct in every constructor/function. 

Example:   Write a program to create constructor and pass different data type as argument using  template.

Output

In the above program we have created a class “xyz” and one constructor it can accept all data type because, constructor contains the variable of template T.

Normal Template  Function Declaration

Syntax for Template Function

template class < T > type function_name() { // code
}

Example:   Write a program to define normal template function.

Output

Example:   Write a program to find the square of different data type using template.

Output

Class Template With More Parameter

In C++ programming, class template can contain one or more parameter of generic data type.

The argument is separated by comma with template declaration.

Syntax for class template with more than one parameter:

template < class T1, class T2 > class name_of_class {
}

Example:   Write a program to define a constructor with multiple template variables.

Output

Example:   Write a program to exchange the value of two variable. Use Template variable as function argument.

Output

Overloading of Template Function

In a C++ programming language, like a function & constructor we can also override the template.

If we create more than one function with same name and one function take template type data as a argument and another function takes primitive data type as a argument then it overrides the function which takes template data as an argument.

Output

In the above program we have created two function (show ()) with same name.

One function take template type data as a argument and another function takes integer data type as a argument then it overrides the function which takes template data as an argument.

Categories C++

Abstract Class in C++

In a C++ programming language , a class that contains at least one pure virtual function is called  abstract class.

This  pure virtual function is a function is declared within base class and defined by derived class.

Like a other class abstract class can also contains normal function.

We can not create object of an abstract class,Any attempt to so will result in an error, but we can create reference to an abstract class.  

Output

Description: In the above program, we have declared a pure virtual function “show()” in a base class A which is defined by derived class B. 

Because class A contain a pure virtual function therefore class A is an abstract class.  

Abstract class can also contain a normal member   function (in this program member function is display() ).

Categories C++

Virtual Function in C++

In a C++ programming language, A virtual function is a member function that is declared and defined within a base class and redefined by a derived class even if the number and type of argument are matching.  

Rules for Virtual Function: 

  • A virtual function may be declared as friend for another class.  
  • Constructor cannot be declared as virtual, but destructor can be declared as virtual.
  • Like other function virtual function can return a value. 

Example:   Write a program to declare virtual function and executes the same function defined in base & derived class.

Or

Write a program to use pointer for both base and derived class and call the member function. Use virtual keyword. 

Or 

Write a program to create a virtual function.

Output

Note: In the above program ‘a ‘is an object of base class A and ‘b’ is an object of derived class B.

The pointer variable ‘p’ point to the base class. In the statement “p = &a;” the address of object ‘a’ is assigned to pointer ‘p’.

Then the pointer calls the member function of class A.

Similarly, in the statement “p = &b;” the address of object ‘b’ is assigned to pointer ‘p’.

Then the pointer calls the member function of class B.

Pure Virtual Function

In a C++ programming language, A pure virtual function is a function which is declared within base class and defined by derived class.

We can declare a pure virtual function by using “virtual” keyword.

Any normal function cannot be declared as pure function. Syntax for declaration of pure virtual function: 

virtual type function _name(parameter list)= 0;    

Example:    virtual void show () =0;     

Example:   Write a program to create a pure virtual function.

Output

Description: In the above program, we have declared a pure virtual function “show ()” in a base class A which is defined by derived class B.

Categories C++

Function Overriding in C++

In a C++ programming language, it the base and derived both class has a function with same name and same parameter then derived class overrides the base class definition.

Such function’s are called overridden function and this process is called function overriding.   

Output

Note: In the above program, class A & B both has a function (show ()) with same name and same parameter list.

When we call a show () function of base class A then show () function of derived class overrides the show () function of base class.

Categories C++

Constructor and Destructor Calling in C++

In a C++ programming language constructor are called from base class to derived class and destructor are called reverse of constructor means from derived class to base class.                                                                                                                      

Example:   Write a program to show the sequence of execution of constructor and destructor.

Output

Categories C++

Virtual Base Class in C++

In a C++ programming language to overcome the ambiguity problem occurred due to multipath inheritance we use “virtual” keyword.

By using the “virtual” keyword we can declares the specified class virtual. 

When a class declared as virtual, then the compiler takes necessary precaution to avoid the duplication of member variable.

We can make a class virtual if it is a base class and is used by more than one derived classes as their base class. 

Example:   Write a program to declare virtual base classes. Derive a class using two virtual base class.

Output

Categories C++

Inheritance and types of Inheritance in C++

Inheritance is a one of the most important feature of object oriented programming.

Inheritance allow an object of one class to acquire the properties of another object of another class.

It supports the reusability and support hierarchical classification. 

The class which inherits the another class is called derived class and the class which inherited is called base class.

 Syntax of creating a derived class:

Example:

In the above syntax, class A is a base class, class B is a derived class. Here class B is derived publicly.

Example:

In the above syntax, class A is a base class, class B is a derived class.

Here class B is derived privately.

Example:

If no access specifier is given , by default derivation is private.

Public & Private Inheritance: In a C++ when a class derived and public access specifier is used.

All the public member of base class can be accessed directly in the derived class but when private access specifier is used, then even public member of base class can not access directly in derived class

Public Inheritance: When a public access specifier is used, the public member of the base class are the public member of the derived class.

Example:

Output

Description: In the above program, class B derives class A  means  class A is a base class, class B is a derived class. 

During inheriting a class public access specifier is used therefore public member of base class A can directly access in derived class B. 

In above program “x” is a public member of base class A but it can directly access in derived class B.

Private Inheritance: When a class derived and private access specifier is used, then an object of derived class has no permission to access even public member of base class directly in main() function.

In such case, the public member of base class can be accessed using public member function of the derived class.

Example : When a class derived and private access specifier is used, then an object of derived class has no permission to access even public member of base class directly in main() function.

Output

Solution of such problem is that the public member of base class can be accessed using public member function of the derived class.

Output

Derived class can not access private data of base class

In a C++  programming ,derived class can’t access private data of base class.

If we will try to do so we will get an error message.

Example: In the below program we are trying to access the private data of base class in a derived class.

Output

Protected access specifier with public  derivation : when a protected access specifier is used, the protected member of the base class are the protected member of the derived class.  

Protected access specifier : A protected access specifier is similar to the private only difference is that it has a access to their derived classes. 

Example 1

Output

Example 2

Output

Types of Inheritance

There are following types of inheritance supported by C++:

  1.  Single Inheritance                                                        
  2.  Multilevel Inheritance
  3.  Multiple Inheritance
  4.  Hierarchical Inheritance
  5. Hybrid Inheritance
  6. Multipath Inheritance

Single inheritance: When only one class derived a class and derived class is not used as a base class such type of inheritance are called Single inheritance.

Here, X is a base class, Y is a derived class. In a single inheritance there is only one base class and one derived class.

Single Inheritance
Single Inheritance

Multilevel Inheritance When a class derived from another derived class such type of inheritance is called multilevel inheritance.

Multileve Inheritance
Multileve Inheritance

Here X is s base class for Y, Y is a derived class from X and Y is also base class for Z. Z is a derived class from Y.

Multiple Inheritance :  When a single class is derived from two or more then two class then such type of inheritance is called multiple inheritance.

X & Y are the base class for Z , Z is deriver class. Class Z  is derived from Class X & Y.

Hierarchical Inheritance: When a two or more classes are derived from a single base class is  known as hierarchical inheritance.

W is a only base class and X,Y & Z are derived class. X,Y & Z are the derived from W.

Hybrid Inheritance :  The combination of two or more inheritance are called hybrid inheritance.

In this type two types of inheritance are use single inheritance and multilevel inheritance.          

Single Inheritance:  W is a base class, Z is a derived class. Single inheritance has only one base class and one derived class.                   

Multilevel Inheritance:X is s base class for Y, Y is a derived class from X and base class for Z. Z is a derived class from Y.

Multipath Inheritance : When a class derived from two or more classes that are derived from same base class such type of inheritance is known as multipath inheritance.

W is a base class and X & Y are derived class. The class X & Y are the derived  from W. 

Further Z is derived from X & Y. means X & Y  is a base class for Z and Z is a derived class.

Here, ambiguity is generated. Hence, virtual keyword is used to avoid ambiguity.

Single Inheritance

Single inheritance: When only one class derived a class and derived class is not used as a base class such type of inheritance are called Single inheritance.

Single Inheritance
Single Inheritance

 Here, X is a base class, Y is a derived class. In a single inheritance there is only one base class and one derived class.

Example:   Write a program to create a base class student and derived class record to read student record such as name, roll number, age  & height and display by using single inheritance.

Output

Note: In the above program, we have created base class student and derived class record.

Student class holds the protected data member name & rollno(A protected data member  is similar to the private only difference is that it has a access to their derived classes).

Class record is derived from student means that it inherits all properties of student class.

Multilevel Inheritance :When a class derived from another derived class such type of inheritance is called multilevel inheritance.

Multileve Inheritance
Multileve Inheritance

Here X is s base class for Y, Y is a derived class from X and Y is also base class for Z. Z is a derived class from Y.

Example:   Write a program to read student record such as name, roll number & age and display by using the concept of multilevel  inheritance, create classes A1, A2 & A3.

Output

Multiple Inheritance :  When a single class is derived from two or more then two class then such type of inheritance is called multiple inheritance

X & Y are the base class for Z , Z is deriver class. Class Z  is derived from Class X & Y.

Example:   Write a program to derive a class from multiple base classes.

Output

Note: In this program, A, B & C are the base class and class D is derived class from A ,B & C .

Class D inherits the properties of class A, B & C.

Hierarchical Inheritance: When a two or more classes are derived from a single base class is  known as hierarchical inheritance

W is a only base class and X,Y & Z are derived class. X,Y & Z are the derived from W.

Example:   Write a program to show the hierarchical inheritance.

Output

Note: A is a only base class. B, C & D are the derived class from A. Class B,C & D all inherits the properties of class A.

Hybrid Inheritance :  The combination of two or more inheritance are called hybrid inheritance.

Hybrid Inheritance
Hybrid Inheritance

In this type two types of inheritance are use single inheritance and multilevel inheritance. 

  Single Inheritance:  W is a base class, Z is a derived class. Single inheritance has only one base class and one derived class. 

Multilevel Inheritance:                X is s base class for Y, Y is a derived class from X and base class for Z. Z is a derived class from Y.   

Example:   Write a program to show the hybrid inheritance. 

or

Write a program to create a derived class from multiple base class.

 or

 Write a program to create hybrid inheritance and take data members such as name of the player, age, city and game name.

Hybrid Inheritance Example
Hybrid Inheritance Example

Output

Note: In this type two types of inheritance are used single inheritance and multilevel inheritance.

Single Inheritance:  location is a base class, game is a derived class.

Single level inheritance has only one base class and one derived class.

Class game inherits the properties of class location.

Multilevel Inheritance: player is a base class for Year, Year is a derived class from player .

year is a  base class for game. game is a derived class from Year. Class game inherits the properties of both  class player & year.

Multipath Inheritance : When a class derived from two or more classes that are derived from same base class such type of inheritance is known as multipath inheritance. 

  W is a base class and X & Y are derived class. The class X & Y are the derived  from W.  Further Z is derived from X & Y.

Means X & Y  is a base class for Z and Z is a derived class. Here, ambiguity is generated.

Hence, virtual keyword is used to avoid ambiguity.

Multipath Inheritance Example
Multipath Inheritance Example

Example:

In the above example, A1 is a base class. The class A2 & A3 are the derived  from class A1. Both A2 and A3 can access the variables/data of class A1 .

Further A4 is derived from A2 & A3.

If we try to access variable a1 of class A1 from class A4, Here, ambiguity is generated &  compiler shows error message.  

Member is ambiguous : “A1:a1” and “A1:a1”

The derived class A4 has two sets of data member of class A1 through the middle base classes A2 and A3.

The A1 is inherited twice.

Hence, virtual keyword is used to avoid ambiguity.

Categories C++

Operator Overloding in C++

In a C++ language, operator overloading is one of the  important feature of c++.  

It is an example of polymorphism. Polymorphism means one function having many forms .  

There are different operators we use in a traditional programming  language the operator such as +,-,*,/ etc.

For example: +( plus ) operator is used to perform addition of two variables , we cant add two object with “+” operator.                 

In C++ to perform operation with object we redefine the definitions of various operators.

Operator overloading feature helps to use these operators( +,- etc. ) with the object and object can be used in a natural manner as the variables of basic data types.

Example: Write a program to perform the addition of two object and store the result in third object. Display the content of third object.

Output

Operator Overloading
Operator Overloading

Example: Write a program to perform addition of two objects using “operator” keyword.

Output

Description : In the above program, A, B, C are the object of class xyz.                                                                  

When the statement C= A + B is execute, the compiler searches for definition of operator + .

The object A call the operator function and object B is passed as an argument

The copy of object B is stored in temporary object O1. In the operator function member variable of object A can directly accessible.

Then we have created additional object “O2” which holds the sum of objects A & O1 (B).

The return type of operator function is same as that of its class. The function return object O2 and it is assigned to  object C.  

Overloading Binary Operator

In a C++ programming, overloading with single operator is called a binary operator overloading.

In C++,  like a arithmetic operator binary operator can also be overloaded.

Binary operator are overloaded by using member function and friend function.

1 Binary operator overloading using member  function:  In a binary operator overloading using member function one argument is required.

This argument contains value of the object.                                   

For example: If we perform the addition of two object o1 & o2, then the overloaded function can declared as:  

operator + (xyz  o) ;                          

Where, “xyz” is a class name and “o” is the object.   To call operator function the statement is as follow:

  o3 = o1 + o2;  

Here, object o1 call the operator function and object o2 is passed as an argument & copy of object “o2” store in temporary object ”o”.

The following statement can also be rewrite as:

                o3 = o1.operator + (o2);                                           

Example. Write a program to overload + binary operator.

Output

Note: In the above program, A, B, C are the object of class xyz. 

When the statement C= A + B is execute, the compiler searches for definition of operator + .

The object A call the operator function and object B is passed as an argument

The copy of object B is stored in temporary object O1.

In the operator function member variable of object A can directly accessible .

Then we have created additional object “O2” which holds the sum of objects A & O1 (B).

The return type of operator function is same as that of its class.

The function return object O2 and it is assigned to  object C.  

2. Binary operator overloading using friend  function: In a C++ we can also overload binary operator by using friend  function.

We can say friend function can be alternate of  member function for overloading binary operator.

Both member & friend function gives the same result.

Friend function is generally useful when we perform an operation with operand of  two different types. 

For example consider the statement: 

                                x = 3 + y ; 

Where, x  &  y are the object of same type.  3 and ‘y’ will pass as an argument to the operator+() function and + operator  will call operator+() function.

Example. Write a program to perform multiplication using an integer an object by using friend function.  

Output

 In the above program, A & B are the object of class xyz. Statement  “  B = 3 * A ;” perform  multiplication.

This statement contains both integer and class object.

As we known that left-hand operand is always used to call the function and right-hand operand passed as an argument.

But In this case left-hand operand is integer and cannot call the function.

 The operator *( ) function is declared as friend .   

When the Statement  “  B = 3 * A ;” will execute it will call the “*” operator will call the operator*() function.          

Categories C++