Read/Write Class Objects From/to File in C++
Read/Write Class Objects From/to File in C++
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.
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.
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.
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.
Abstract Class: