Read/Write Class Objects From/to File in C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Read/Write Class Objects from/to File in C++

The iostream standard library has two methods cin, to accept input from standard input stream
and cout to print output to the standard output stream.
Reading and writing data to and from files requires another standard library of C++ <fstream>.
The three main data types of fstream are −

 ifstream − represents input file stream and reads information from files.
 ofstream − represents output file stream and writes information to files.
 fstream − represents general file stream and has capabilities of both.

Writing and Reading class objects


Syntax:
file1.write( (char*)&Emp_1, sizeof(Emp1) );
file2.read( (char*)&Emp_1, sizeof(Emp1) );
Closing the file
file1.close();
file2.close();
Example
class Employee {
public:
   string Name;
   int Employee_ID;
   int Salary;
};
int main(){
   Employee Emp_1;
   Emp_1.Name="John";
   Emp_1.Employee_ID=2121;
   Emp_1.Salary=11000;
   //Wriring this data to Employee.txt
   ofstream file1;
   file1.open("Employee.txt", ios::app);
   file1.write((char*)&Emp_1,sizeof(Emp_1));
   file1.close();
   //Reading data from EMployee.txt
   ifstream file2;
   file2.open("Employee.txt",ios::in);
   file2.seekg(0);
   file2.read((char*)&Emp_1,sizeof(Emp_1));
   printf("\nName :%s",Emp_1.Name);
   printf("\nEmployee ID :%d",Emp_1.Employee_ID);
   printf("\nSalary :%d",Emp_1.Salary);
   file2.close();
   return 0;
}

Friend function:
A friend function in C++ is defined as a function that can access private, protected and public
members of a class.

The friend function is declared using the friend keyword inside the body of the class.

Syntax:
class className {
    ... .. ...
    friend returnType functionName(arguments);
    ... .. ...
}
By using the keyword, the ‘friend’ compiler understands that the given function is a friend function.

Characteristics of Friend Function in C++


 Friend functionality is not restricted to only one class
 We can invoke it like any normal function of the class.
 Friend functions have objects as arguments.
 We can declare it either in the ‘public’ or the ‘private’ part.
 The function is not in the ‘scope’ of the class to which it has been declared a
friend.
Implementing Friend Functions

A method of another class:


     We declare a friend class when we want to access the non-public data
members of a particular class.

A Global function:
      A ‘global friend function’ allows you to access all the private and protected
members of the global class declaration.

Example
Friend Class:
Friend Class is a class that can access both private and protected variables of
the class in which it is declared as a friend, just like a friend function. Classes
declared as friends to any other class will have all the member functions as
friend functions to the friend class. Friend functions are used to link both these
classes.

Advantages of friend function in C++


 Friend function in c++ provide a degree of freedom in the interface design option
 A friend function is used to access all the non-public members of a class.
 It increases the versatility of overloading operators.
 You may declare a member function of a class as a friend of another class.
 It works symmetrically with all its friends.

Example
Private/public in inheritance:
public inheritance makes public members of the base class public in the derived
class, and the protected members of the base class remain protected in the derived
class.
private inheritance makes the public and protected members of the base
class private in the derived class.

Public:
Private:
Multilevel inheritance:
In C++ programming, not only you can derive a class from the base class but you
can also derive a class from the derived class. This form of inheritance is known as
multilevel inheritance.

class A {

... .. ...

};

class B: public A {

... .. ...

};

class C: public B {

... ... ...

};
Static binding/Dynamic binding:
They both are the types of polymorphism.

1. Static Binding (or Compile time) Polymorphism, e.g., Method Overloading


2. Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding
Compile time polymorphism: This type of polymorphism is achieved by function
overloading or operator overloading.
 Function Overloading : When there are multiple functions with same name
but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
Rules of Function Overloading
Runtime polymorphism: This type of polymorphism is achieved by Function
Overriding.
 Function overriding  on the other hand occurs when a derived class has a
definition for one of the member functions of the base class. That base
function is said to be overridden.
Pure virtual function:
A pure virtual function is a virtual function in C++ for which we need not to
write any function definition and only we have to declare it. It is declared by
assigning 0 in the declaration. An abstract class is a class in C++ which have at
least one pure virtual function. If an Abstract Class has derived class, they must
implement all pure virtual functions, or else they will become Abstract too.

Abstract Class:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy