Вы находитесь на странице: 1из 23

POLYMORPHISM

FILES

pksa, CSE dept of NIT Rourkela

pksa, CSE dept of NIT Rourkela

POLYMORPHISM

(one name, multiple forms)


Polymorphism

Compile time Polymorphism

Runtime Polymorphism

Function Overloading Operator Overloading Virtual Functions

pksa, CSE dept of NIT Rourkela

#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public: void set_values(int a,int b){
width=a; height=b;
}
};
class Rectangle: public Polygon {
public: int area(void){
return (width * height);
}
};
class Triangle: public Polygon{
public: int area(void){
return(width*height/2);
}

Pointers to
base class

pksa, CSE dept of NIT Rourkela

Pointers to
base class

int main(){
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}

Both *ppoly1 and *ppoly2 are of type Polygon* and


therefore we can only refer to the members that Rectangle and
Triangle inherit from Polygon. For this reason when calling
the area() members we have not been able to use the pointers
*ppoly1 and *ppoly2.
pksa, CSE dept of NIT Rourkela

virtual members

In order to declare an element of a class which


we are going to redefine in derived classes we
must precede it with the keyword virtual so
that the use of pointers to objects of that class
can be suitable.

pksa, CSE dept of NIT Rourkela

#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values(int a,int b)
{width=a; height=b; }
virtual int area()
{return 0;}
};
class Rectangle: public Polygon {
public: int area(void)
{return (width * height);
};
class Triangle: public Polygon{
public: int area(void)
{return(width*height/2); }
};

virtual
function

pksa, CSE dept of NIT Rourkela

int main(){
Polygon * ppoly, poly;

virtual
function

ppoly = &poly;
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
Rectangle rect;
ppoly = &rect;
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
Triangle trgl;
ppoly = &trgl;
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
return 0;
}

pksa, CSE dept of NIT Rourkela

Pure virtual functions


A virtual function that has no definition within the
base class is called as pure virtual function.
virtual ReturnType FunctionName(ParameterList) = 0;

When a virtual function is made pure, any derived


class must provide its own definition.

pksa, CSE dept of NIT Rourkela

Abstract base classes


A class that contains at least one pure virtual
function is said to be abstract.
// abstract class Polygon
class Polygon {
protected:int width, height;
public:
void set_values(int a,int b)
{ width=a; height=b; }
virtual int area(void) = 0;
};
Instances (objects) of abstract classes cannot
be created, but pointers can be.
pksa, CSE dept of NIT Rourkela

Abstract base classes


Therefore a declaration like:
Polygon poly;
would be incorrect for the abstract base class
declared above.
Nevertheless the pointers:
Polygon * ppoly1;
Polygon * ppoly2;
are be perfectly valid.
This is because the pure virtual function that it
includes is not defined and it is impossible to
create an object if it does not have all its
members defined.

pksa, CSE dept of NIT Rourkela

virtual program-1

#include <iostream>
using namespace std;
class Polygon {
protected: int width, height;
public:
void set_values (int a, int b)
{ width = a;
height = b; }
virtual int area () = 0;
};
class Rectangle: public Polygon {
public:
int area(){ return (width * height);}
};
class Triangle: public Polygon {
public: int area(){return(width*height/2);}
};
pksa, CSE dept of NIT Rourkela

virtual program-1
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
return 0;
}

pksa, CSE dept of NIT Rourkela

#include <iostream>
class Polygon {
protected: int width, height;
public:
void set_values (int a, int b)
{ width = a; height = b; }
virtual int area () = 0;
void printarea()
{ cout << this->area() << endl;}
};
class Rectangle: public Polygon {
public:
int area(){return (width * height); }
};
class Triangle: public Polygon {
public:
int area(){return(width*height / 2);}
};

virtual program-2

pksa, CSE dept of NIT Rourkela

virtual program-2
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}

pksa, CSE dept of NIT Rourkela

files
C++ has support both for input and output with
files through the following classes:
ofstream : File class for writing
operations (derived from ostream)
ifstream : File class for reading
operations (derived from istream)
fstream : File class for both reading and
writing operations (derived from iostream)

pksa, CSE dept of NIT Rourkela

writing to file
// writing on a text file
#include <fstream>
using namespace std;
int main () {
ofstream examplefile ("example.txt");
if(examplefile.is_open()){
examplefile<<"This is a line.\n";
examplefile<<"This is another line.\n";
examplefile.close();
}
return 0;
}
pksa, CSE dept of NIT Rourkela

file open
In order to open a file with a stream object
we use its member function open()
void open(const char* filename, openmode mode);
where filename is a string of characters representing the
name of the file to be opened and mode is a combination
of the following flags:
ios::in

Open file for reading

ios::out

Open file for writing

ios::ate

Initial position: end of file

ios::app

Every output is appended at the end of file

ios::trunc If the file already existed it is erased


ios::binar
y

Binary mode
pksa, CSE dept of NIT Rourkela

file open
These flags can be combined using bitwise operator
OR: |. For example, if we want to open the file
"example.bin" in binary mode to add data we could
do it by the following call to function-member open
ofstream file;
file.open("example.bin",ios::out|ios::app|ios::binary);

or
ofstream file("example.bin",ios::out|ios::app|ios::binary);

class
ofstream
ifstream
fstream

default mode to parameter


ios::out | ios::trunc
ios::in
ios::in | ios::out
pksa, CSE dept of NIT Rourkela

file close
When reading, writing or consulting operations on
a file are complete we must close it so that it
becomes available again.
void close ();
Once this member function is called, the stream
object can be used to open another file, and the
file is available again to be opened by other
processes.
In case that an object is destructed while still
associated with an open file, the destructor
automatically calls the member function close.
pksa, CSE dept of NIT Rourkela

// reading a text file


#include <iostream>
#include <fstream>
using namespace std;
int main () {
char buffer[256];
ifstream examplefile ("example.txt");
if (! examplefile.is_open())
{ cout << "Error opening file"; exit (1); }

reading from file

while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
cout << buffer << endl;
}
examplefile.close();
return 0;
}

pksa, CSE dept of NIT Rourkela

Programs

1. Assume that a bank maintains two kinds of accounts for


customers, one called as savings account and the other as
current account. The savings account provides compound interest
and withdrawal facilities but not cheque book facility. The current
account provides cheque book facility but no interest. Current
account holders should also maintain a minimum balance and if
the balance falls below this level, a service charge is imposed.
Create a class Account that stores customers name, account
number and type of account. From this derive the classes
CurrentAcc and SavingsAcc to make them more specific to
their requirements. Include necessary member functions in order
to achieve the following tasks.
a) Accept deposit from a customer and update the balance.
b) Display the balance.
c) Compute and deposit the interest.
d) Permit withdrawal and update the balance.
e) Check for the minimum balance, impose penalty,
necessary, and update the balance.
pksa, CSE dept of NIT Rourkela

Programs
2. Write a C++ program that prompts the user to input
two file names (one source and another destination)
and an operation to be performed (encryption or
decryption) on the source file. After reading the
source file from the disk, the program encrypts or
decrypts its contents and stores it in the destination
file depending on the operation input.
NB.:-For encrypting/decrypting you may change the
ASCII value of each character of the file.

pksa, CSE dept of NIT Rourkela

Вам также может понравиться