VPMP Unit 5
VPMP Unit 5
};
void main( )
{
ABC a1;
a1.getdata(50);
a1.print( );
}
Output:
a=50
Example:
#include<iostream.h>
#include<conio.h>
};
void main( )
{
person p1,p2;
p1.getdata( );
p2.getdata( );
p1.putdata( );
p2.putdata( );
person& p3=p1.elder(p2);
cout<<” Elder is : “;
p3.putdata( );
getch( );
}
Output:
Enter Name and Age : Sunita 20
Name: Sunita
Age: 20
Enter Name and Age : Anita 18
Name: Anita
Age: 18
Elder is :
Name: Sunita
Age: 20
Virtual function
When we use the same function name in both the base and derived classes, the function in base class
is declared as virtual using the keyword virtual preceding its normal declaration.
When a function is made virtual, C++ determines which function to use at run time based on the type
of object pointed to by the base pointer, rather than the type of the pointer.
So, by making the base pointer to point to different objects, we can execute different versions of the
virtual function.
They are mainly used in Runtime polymorphism
Rules for virtual function
};
void main()
{
base *b;
base b1(5);
b=&b1;
bdisplay( ); //call display( ) function of base class
derived d1(10,20);
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Object Oriented Programming Unit No: 5 Subject Code: 4320702
derived d2(30,40);
b=&d1;
bdisplay( ); //call display( ) function of derived class
b=&d2;
bdisplay( ); //call display( ) function of derived class
}
Output:
b=5
b=10 d=20
b=30 d=40
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
float r;
public:
shape(float r1)
{
r=r1;
}
virtual float area( )=0;
};
The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal
with both the console and disk file.
These classes are called as stream classes.
The stream classes are used for input and output operations with the console unit.
These classes are declared in the header file iostream.
The ios class is the base class for istream and ostream.
The istream and ostream classes are base classes for iostream.
The class ios is declared as the virtual base class so that only one copy of its members is inherited by
the iostream.
The class ios provides the basic support for formatted and unformatted I/O operations .
ios class:
• It provides the input & output facilities.
• It contains a pointer to a buffer object.
istream class:
• Inherits the properties of ios.
• Declares input functions such as get( ), getline( ) and read( )
• Contains overloaded extraction operator >>
ostream class:
• Inherits the properties of ios.
• Declares input functions such as put( ) and write( )
• Contains overloaded extraction operator <<
iostream class:
• It inherits the properties of ios, istream and ostream through multiple inheritances and thus
contains all the input and output functions.
streambuf:
• Provides an interface to physical devices through buffers.
• Acts as a base for filebuf class used ios files.
File handling is used to store data permanently in a computer. Using file handling we can store our
data in secondary memory
File Operations
1. Creating a new file. (fopen with mode “a” or “w”)
2. Opening an existing file. (fopen)
3. Reading from or writing information to the file. (fscanf or fgets, fprintf or fputs)
4. Closing the file. (fclose).
Example:
file *fptr;
Opening a file
Opening a file is done using the fopen() function in the header file stdio.h.
Syntax:
file_pointer = fopen(“file_name”, “mode”);
Example:
fptr= fopen(“D:\\myfolder\\file1.txt”, “w”);
File Opening modes in C:
r Open for reading. If the file does not exist, fopen( ) returns NULL.
If the file exists, its contents are overwritten. If the file does
Open for writing.
w not exist, it will be created.
Open for writing in binary If the file exists, its contents are overwritten. If the file does
wb mode. not exist, it will be created.
Data is added to the end of the file. If the file does not exist,
Open for append.
a it will be created.
Open for append in binary Data is added to the end of the file. If the file does not exist,
ab mode. it will be created.
fgets( ):
The fgets() function in C++ reads a specified maximum number of characters from the given file stream.
On success, the fgets() function returns str and on failure it returns null pointer.
Syntax:
Example:
fgets(str,10, fptr);
fputs( )
The fputs() function in C++ writes a string completely except the terminating null character to the given
output file stream.
Syntax:
Example:
fputs(“HELLO” , fptr);
Closing a file
The file should be closed after reading or writing. Closing a file is performed using the fclose() function.
Syntax:
fclose(file_pointer);
Example:
fclose(fptr);
cout<<” setiosflags …“
int value;
cout << “ enter a number \n”;
cin >> value;
cout << setiosflags (ios : : showbase) ;
cout << setiosflags (ios : : dec) ;
cout << “ decimal = “ << value << endl;
cout << setiosflags (ios : : hex) ;
cout << hexadecimal = “ << value << endl ;
cout << setiosflags (ios : : oct) ;
cout << “ octal = “ << value << endl ;
}
Output:
setw() ..............
11
2222
4
setprecision …
1.7
1.67
1.667
setiosflags …
enter a number
10
decimal = 10
hexadecimal = 0xa
octal = 012