File Handling

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
void main()
 {
clrscr();
ofstream out;
ifstream in;
char ch,fname1[10],fname2[10];
cout<<"Enter the file name to be copied:";
cin>>fname1;
cout<<"Enter the new file name:";
cin>>fname2;
out.open(fname1);
out<<"This is a test program.";
out.close();
in.open(fname1);
if(in.fail())
       {
cerr<<"No such a file exists.";
getch();
exit(1);
       }
out.open(fname2);
if(out.fail())
       {
cerr<<"Unable to create the file.";
exit(1);
getch();
       }
while(!in.eof())
       {
ch=(char)in.get();
out.put(ch);
       }
in.close();
out.close();
cout<<"The file is copied.\n";
in.open(fname2);
cout<<"The copied contents are:";
while(!in.eof())
 {
ch=(char)in.get();
cout<<ch;
 }
in.close();
getch();
 }

Multiple Inheritance

#include<iostream.h>
#include<constream.h>
class A { protected : int a;};
class B { protected : int b;};
class C { protected : int c;};
class D { protected : int d;};
class E : public A,B,C,D
 {
 int e;
public:
 void getdata()
  {
cout<<"\n Enter values of a,b,c,d,e:\n";
cin>>a>>b>>c>>d>>e;
  }
 void showdata()
  {
cout<<"\n a="<<a<<"\n b="<<b<<"\n c="<<c<<"\n d="<<d;               cout<<"\n e="<<e;
  }
 };
void main()
 {
clrscr();
E X;
X.getdata();
X.showdata();
getch();
 }

Multilevel Inheritance

#include<iostream.h>
#include<constream.h>
class A1
 {
protected:
     char name[15];
     int age;
 };
class A2: public A1
 {
protected:
float height;
float weight;
 };
class A3: public A2
 {
protected:
char sex;
public:
void get()
 {
cout<<"name :";
cin>>name;
cout<<"age :"; cin>>age;
cout<<"sex :"; cin>>sex;
cout<<"height :"; cin>>height;
cout<<"weight :"; cin>>weight;
 }
void show()
 {
cout<<"\n Name :"<<name;
cout<<"\Age :"<<age<<"years";
cout<<"\Sex :"<<sex;
cout<<"\Height :"<<height<<"feets";
cout<<"\Weight :"<<weight<<"kg.";
 }
 };
void main()
 {
clrscr();
A3 X;
X.get();
X.show();
getch();
 }

Single Inheritance

#include<iostream.h>
#include<constream.h>
class ABC
 {
protected:
     char name[15];
          int age;
 };
class abc: public ABC
 {
Protected:
     float height;
     float weight;
public:
  void getdata()
   {
cout<<"\n Enter Name and Age:";
cin>>name>>age;
cout<<"\n Enter Height and Weight:";
cin>>height>>weight;
   }
  void show()
   {
cout<<"\n Name:"<<name<<"\n Age:"<<age<<"years";
cout<<"\n Height:"<<height<<"feets";    
cout<<”\n weight:"<<weight<<"kg";
   }
 };
void main()
 {
clrscr();
abc x;
x.getdata();
x.show();
getch();
 }
Related Posts with Thumbnails