Академический Документы
Профессиональный Документы
Культура Документы
FILES
POLYMORPHISM
Runtime Polymorphism
#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
Pointers to
base class
int main(){
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
virtual members
#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
int main(){
Polygon * ppoly, poly;
virtual
function
ppoly = &poly;
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
Rectangle rect;
ppoly = ▭
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
Triangle trgl;
ppoly = &trgl;
ppoly->set_values (4,5);
cout << ppoly->area() << endl;
return 0;
}
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 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
return 0;
}
#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
virtual program-2
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}
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)
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
ios::out
ios::ate
ios::app
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
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
while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
cout << buffer << endl;
}
examplefile.close();
return 0;
}
Programs
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.