1:
//istream.cpp
#include<iostream>
using namespace std;
int main()
{
char ch;
int n = 0;
cout << "Enter the characters ('$' to exit) : ";
while(cin.peek() != '$')
{
cin.get(ch);
if(ch != '$')
{
n++;
cout << ch;
}
}
cin >> ch;
cout << endl;
cout << "Total " << n << " characters ."<< endl;
cout << "Next character is " << ch << endl;
cout << "done . " << endl;
return 0;
}
2:
//fileadd.cpp
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 2)
{
cout << "Usage : " << argv[0] << " filename(s)" << endl;
exit(EXIT_FAILURE);
}
ofstream fout;
fout.open(argv[1], ios::out | ios::app);
if(!fout.is_open())
{
cout << "Could not open the file . " << endl;
exit(EXIT_FAILURE);
}
cout << "Input the words to add (enter to exit): " << endl;
string str;
while(cin.peek() != '\n')
{
getline(cin, str);
cout << str << endl;
fout << str << endl;
}
cout << "done ." << endl;
fout.close();
return 0;
}
3:
//filecope.cpp
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 3)
{
cout << "Usage " << argv[0] << "filename(destination) filename(source)" << endl;
exit(EXIT_FAILURE);
}
ofstream fout;
fout.open(argv[1], ios::out | ios::app);
if(!fout.is_open())
{
cout << "Could not open the " << argv[1] << endl;
exit(EXIT_FAILURE);
}
ifstream fin;
fin.open(argv[2]);
if(!fin.is_open())
{
cout << "Could not open the " << argv[2] << endl;
exit(EXIT_FAILURE);
}
char ch;
while(fin.get(ch))
{
fout << ch;
}
fin.close();
fout.close();
cout << "done . " << endl;
return 0;
}
5:
//party.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<set>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
ifstream fin;
string fname;
set<string> party;
cout << "Enter the Mat friends name : ";
getline(cin, fname);
fin.open(fname.c_str());
if(!fin.is_open())
{
cout << "Could not open the " << fname << endl;
exit(EXIT_FAILURE);
}
string temp;
while(!fin.eof())
{
getline(fin, temp);
cout << temp << endl;
party.insert(temp);
}
fin.close();
cout << "Enter the Pet friends name : ";
getline(cin, fname);
fin.open(fname.c_str());
if(!fin.is_open())
{
cout << "Could not open the " << fname << endl;
exit(EXIT_FAILURE);
}
while(!fin.eof())
{
getline(fin, temp);
cout << temp << endl;
party.insert(temp);
}
fin.close();
cout << "Enter the Party friends name : ";
ofstream fout;
getline(cin, fname);
fout.open(fname.c_str());
if(!fout.is_open())
{
cout << "Could not open the " << fname << endl;
exit(EXIT_FAILURE);
}
ostream_iterator<string, char> outer(cout, " ");
copy(party.begin(), party.end(), outer);
ostream_iterator<string, char> fouter(fout, " ");
copy(party.begin(), party.end(), fouter);
fout.close();
cout << "done . " << endl;
return 0;
}
6:
//employee.h
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include<iostream>
#include<fstream>
#include<string>
using std::string;
using std::ostream;
using std::ofstream;
using std::ifstream;
enum classId{employee, manager, fink, highFink};
class base_emp
{
private:
string name;
string job;
public:
base_emp():name("none"), job("none"){};
base_emp(const string &nm, const string &jb):name(nm), job(jb){};
virtual ~base_emp() = 0 ;
virtual void showAll()const;
virtual void setAll();
virtual void writeAll(ofstream &os);
virtual void readAll(ifstream &is);
friend ostream & operator<<(ostream &os, const base_emp &be);
};
class Employee:public base_emp
{
public:
Employee():base_emp(){};
Employee(const string &nm, const string &jb):base_emp(nm, jb){};
virtual void showAll()const{base_emp::showAll();};
virtual void setAll(){base_emp::setAll();};
void writeAll(ofstream &os);
void readAll(ifstream &is);
};
class Manager:virtual public base_emp
{
private:
int inchargeof;
protected:
int inChargeOf()const{return inchargeof;};
int & inChargeOf(){return inchargeof;};
void fileChargeOf(ofstream &os)const{os << "In charge of : " << inchargeof << " \t";};
void fileChargeOf(ifstream &is){is >> inchargeof;};
public:
Manager():base_emp(),inchargeof(0){};
Manager(const string &nm, const string &jb, int ic):base_emp(nm, jb), inchargeof(ic){};
Manager(const base_emp &be, int ic):base_emp(be), inchargeof(ic){};
Manager(const Manager &m):base_emp(m){inchargeof = m.inchargeof;};
virtual void showAll()const;
virtual void setAll();
void writeAll(ofstream &os);
void readAll(ifstream &is);
};
class Fink:virtual public base_emp
{
private:
string report;
protected:
const string Report()const{return report;};
string & Report(){return report;};
void fileReport(ofstream &os){os << "Report : " << report;};
void fileReport(ifstream &is){is >> report;};
public:
Fink():base_emp(), report("none"){};
Fink(const string &nm, const string &jb, const string rpt):base_emp(nm, jb), report(rpt){};
Fink(const base_emp &be, const string &rpt):base_emp(be), report(rpt){};
Fink(const Fink &f):base_emp(f){report = f.report;};
virtual void showAll()const;
virtual void setAll();
void writeAll(ofstream &os);
void readAll(ifstream &is);
};
class HighFink:public Manager, public Fink
{
public:
HighFink(){};
HighFink(const string &nm, const string &jb, int ic, const string &rpt):base_emp(nm, jb), Manager(nm, jb, ic), Fink(nm, jb, rpt){};
HighFink(const base_emp &be, int ic, const string &rpt):base_emp(be), Manager(be, ic), Fink(be, rpt){};
HighFink(const Manager &m, const string &rpt):base_emp(m), Manager(m), Fink(m, rpt){};
HighFink(const Fink &f, int ic):base_emp(f), Manager(f, ic), Fink(f){};
HighFink(const HighFink &h):base_emp(h), Manager(h), Fink(h){};
virtual void showAll()const;
virtual void setAll();
void writeAll(ofstream &os);
void readAll(ifstream &is);
};
#endif
//employee.cpp
#include<iostream>
#include<fstream>
#include"employee.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
base_emp::~base_emp()
{
}
void base_emp::showAll()const
{
cout << " Name : " << name << " Job : " << job;
}
void base_emp::setAll()
{
cout << "Enter the name : " ;
getline(cin, name);
cout << "Enter the job : ";
getline(cin, job);
}
void base_emp::writeAll(ofstream &os)
{
os << " Name : " << name << " Job : " << job ;
}
void base_emp::readAll(ifstream &is)
{
getline(is, name);
getline(is, job);
}
ostream & operator<<(ostream &os, const base_emp &be)
{
os << " Name : " << be.name << " Job : " << be.job << endl;
return os;
}
void Employee::writeAll(ofstream &os)
{
os << employee;
base_emp::writeAll(os);
os << endl;
}
void Employee::readAll(ifstream &is)
{
base_emp::readAll(is);
}
void Manager::showAll()const
{
base_emp::showAll();
cout << " In charge of : " << inchargeof << endl;
}
void Manager::setAll()
{
base_emp::setAll();
cout << "Enter the inchargeof : ";
cin >> inchargeof;
}
void Manager::writeAll(ofstream &os)
{
os << manager;
base_emp::writeAll(os);
os << " In charge of : " << inchargeof << endl;
os << endl;
}
void Manager::readAll(ifstream &is)
{
base_emp::readAll(is);
is >> inchargeof;
}
void Fink::showAll()const
{
base_emp::showAll();
cout << " Report : " << report << endl;
}
void Fink::setAll()
{
base_emp::setAll();
cout << "Enter the report : ";
getline(cin, report);
}
void Fink::writeAll(ofstream &os)
{
os << fink;
base_emp::writeAll(os);
os << " Report : " << report << endl;
os << endl;
}
void Fink::readAll(ifstream &is)
{
base_emp::readAll(is);
is >> report;
}
void HighFink::showAll()const
{
base_emp::showAll();
cout << " In charge of : " << Manager::inChargeOf();
cout << " Report : " << Fink::Report() << endl;
}
void HighFink::setAll()
{
int ic;
string rpt;
base_emp::setAll();
cout << "Enter the inchargeof : ";
cin >> ic;
inChargeOf() = ic;
cin.get();
cout << "Enter the report : ";
getline(cin, rpt);
Report() = rpt;
}
void HighFink::writeAll(ofstream &os)
{
os << highFink;
base_emp::writeAll(os);
Manager::fileChargeOf(os);
Fink::fileReport(os);
os << endl;
}
void HighFink::readAll(ifstream &is)
{
base_emp::readAll(is);
Manager::fileChargeOf(is);
Fink::fileReport(is);
}
//main.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include"employee.h"
using namespace std;
const int MAX = 10;
int main()
{
cout << "This demo is to save the multi-class object into a file " << endl;
base_emp *pe[MAX];
string fname;
cout << "Enter the file name : ";
getline(cin, fname);
ifstream fin;
fin.open(fname.c_str());
if(!fin.is_open())
{
cout << "Could not open the file . " << endl;
exit(EXIT_FAILURE);
}
char ch;
int emptype;
int i = 0;
while(fin >> emptype)
{
switch(emptype)
{
case employee:
pe[i] = new Employee;
break;
case manager:
pe[i] = new Manager;
break;
case fink:
pe[i] = new Fink;
break;
case highFink:
pe[i] = new HighFink;
break;
default:
cout << "Error type ";
}
pe[i]->readAll(fin);
pe[i]->showAll();
i++;
}
fin.close();
ofstream fout;
fout.open(fname.c_str(), ios::out | ios::app);
int index = 0;
cout << "Enter the class type you want to create : " << endl;
cout << "E(employee), M(Manager), F(fink), H(highFink), Q(quit)" << endl;
while(cin >> ch && ch != 'Q')
{
if(index < MAX)
{
cin.get();
switch(ch)
{
case 'E':
case 'e':
pe[index] = new Employee;
pe[index]->setAll();
break;
case 'M':
case 'm':
pe[index] = new Manager;
pe[index]->setAll();
break;
case 'F':
case 'f':
pe[index] = new Fink;
pe[index]->setAll();
break;
case 'H':
case 'h':
pe[index] = new HighFink;
pe[index]->setAll();
break;
default:
cout << "Error type . " << endl;
}
index++;
cout << "Enter E, M, F, H, Q again " << endl;
}
else
{
cout << "the index is of out of the array . " << endl;
break;
}
}
for(i = 0; i < index; i++)
{
pe[i]->writeAll(fout);
}
fout.close();
fin.clear();
fin.open(fname.c_str());
cout << "Here is the refreshed file " << endl;
while((fin >> emptype).get(ch))
{
switch(emptype)
{
case employee:
pe[i] = new Employee;
break;
case manager:
pe[i] = new Manager;
break;
case fink:
pe[i] = new Fink;
break;
case highFink:
pe[i] = new HighFink;
break;
default:
cout << "Error type ";
}
pe[i]->readAll(fin);
pe[i]->showAll();
i++;
}
fin.close();
cout << "done . " << endl;
return 0;
}
Practice makes perfect!