C++

1.Complete File I/O program in C++ using Objects in MinGW compiler

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;


class student
{
protected:
char name[20];
char address[50];
int roll;
float marks;

public:
       void getdata()
        {
        cout<<"Enter the name:";cin>>name;
        cout<<"Enter the address:";cin>>address;
        cout<<"Enter the roll no:";cin>>roll;
        cout<<"Enter the marks:";cin>>marks;
        }

        void putdata()
        {

        cout<<setw(10)<<name<<setw(15)
        <<address<<setw(10)<<roll
        <<setprecision(2)<<setw(10)
        <<marks<<endl;
        }
     
};


int main()
{
student std;
fstream inout("RECORD.txt",ios::ate|ios::binary|ios::in|ios::out);
cout<<"What do you like to do?"<<"\n";
cout<<"Enter 1 to write into file:\n";
cout<<"Enter 2 to display the content of file:"<<"\n";
cout<<"Enter 3 to update record in file:"<<"\n";
cout<<"Enter 4 to modify record of student:"<<"\n";
cout<<"Enter 5 to count number of objects in file:"<<"\n";
int m;
cin>>m;

switch(m)
{
case 1:
       {
     
       inout.seekg(0,ios::beg);
       int z;
       cout<<"Enter the number of students";cin>>z;
       for(int k=1;k<=z;k++)
          {
          std.getdata();
          inout.write(reinterpret_cast<const char *>(&std),sizeof(std));
          }
          inout.clear();
          break;

    }



//Displays current contents
case 2:
       {
     
        inout.seekg(0,ios::beg);
        cout<<"current contents of file"<<"\n";
        cout<<setw(10)<<"Name"<<setw(15)
        <<"Address"<<setw(10)<<"Roll"
        <<setprecision(2)<<setw(10)
        <<"Marks"<<endl;
       while(inout.read(reinterpret_cast<char *>(&std),sizeof(std)))
         {
         std.putdata();
         }
       inout.clear();
       break;
       }

//update student record
case 3:
       {
       cout<<"\n Add a student \n";
       std.getdata();
       char ch;
       cin.get(ch);
       inout.write(reinterpret_cast<const char *> (&std),sizeof(std));
       cout<<"Record Added";
       break;
       }
case 4:
       {
       cout<<"Enter the object number to be modified"<<endl;
       int object,location;
       cin>>object;
       location=(object-1)*sizeof(std);
       inout.seekg(location);
       cout<<"Enter the new data"<<endl;
       std.getdata();
       inout.write(reinterpret_cast<const char *> (&std),sizeof(std));
       cout<<"Record modified"<<endl;
       break;
       }
case 5:
      {
   
      int last=inout.tellg();
      int n=last/sizeof(std);
      cout<<"Total number of objects:"<<n<<"\n";
      cout<<"Total size of file:"<<last<<"bytes";
      break;
      }
default:
       {
       cout<<"\nWrong choice";
       }
}

inout.close(); //closing the file
return 0;
}


2.Exception Handling 1:
#include<iostream>
using namespace std;

int main()
{
int i,j,a,x;
cout<<"Enter three numbers a,i and j"<<endl;
cin>>a>>i>>j;
x=i-j;
try
{
 if(x==0)
  throw(x);
 else
 cout<<"The result of a/x is:"<<a/x;
}
catch(int k)
 {
   cout<<"Exception caught:X=0"<<endl<<
    "Division by zero is undefined"<<endl;
 }

}

3.Exception Handling 2:
#include<iostream>

using namespace std;

void divide(int x,int y,int z)
{
cout<<"We are inside function:"<<endl;
int r=x-y;
if(r!=0)
cout<<"result of z/r"<<z/r<<endl;
else
throw(x-y);
}



int main()
{
try
{
cout<<"We are inside try block"<<endl;
divide(20,10,30);
divide(10,10,30);

}
catch(int i)
{
cout<<"Exception caught"<<"X="<<i<<endl;
}
return 0;

}



4.Class and Object Concept:
#include<iostream>

using namespace std;

class student
{
private:
int roll;char name[25];long int tel_no;float score;
public:
void getdata()
{
cout<<"Enter the roll number,name,telephone number and score of student"<<endl;
cin>>roll>>name>>tel_no>>score;
}
void showdata()
{
cout<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl<<"Telephone number"<<tel_no<<endl
    <<"Score:"<<score<<endl;
}
void rank();
};

void student::rank()
{
if(score>80) cout<<"Distinction";
else if(score>50 && score<80) cout<<"First Division";
else if(score>40 && score<50) cout<<"Second Division";
else if(score>32 && score<40) cout<<"Third Division";
else cout<<"Failed";
}

int main()
{
student s1,s2;
s1.getdata();
s1.showdata();
s1.rank();
s2.getdata();
s2.showdata();
s2.rank();
return 0;

}



5.Adding two complex numbers using Class and Object Concept:
#include<iostream>
using namespace std;

class complex
{

float re,im;

public:

complex(int a,int b)
{
re=a;im=b;
}

complex()//Constructor
{
float x,y;
cout<<"Enter complex number of form a+ib";
cin>>x>>y;
re=x;im=y;
}

void sum(complex cc1,complex cc2)//Evaluates sum
{
re=cc1.re+cc2.re;
im=cc1.im+cc2.im;
}

void showdata()
{
cout<<"the sum is"<<endl<<"Re="<<re<<endl<<"Im="<<im<<endl;
}

};


int main()
{
complex c1,c2;
complex c3(0,0);
c3.sum(c1,c2);
c3.showdata();
return 0;

}

6.Function overloading:In calculating Area of different geometries
#include<iostream>

using namespace std;

const float p=3.14;

float area(float r)
{
return (p*r*r);
}

int area(int a,int b)
{
return(a*b);
}

int main()
{
float r;
int a,b;
cout<<"Enter the radius of the circle"<<endl;
cin>>r;
cout<<"Enter the length and breadth of a rectangle"<<endl;
cin>>a>>b;
cout<<"The area of circle is "<<area(r)<<endl<<"and that of rectangle is "<<area(a,b);
return 0;
}

7.Adding three complex numbers using binary operator overloading
#include<iostream>

using namespace std;

class complex
{
float re;
float im;
public:
void getdata()
{
cout<<"Enter a complex number a+ib"<<endl;
cin>>re>>im;
}
void setdata()
{
cout<<"Enter another complex number c+id"<<endl;
cin>>re>>im;
}
void Enterdata()
{
cout<<"Enter another complex number e+if"<<endl;
cin>>re>>im;
}

complex operator +(complex c2)
{
complex c;
c.re=re+c2.re;
c.im=im+c2.im;
return c;
}
void showdata()
{
cout<<"The sum is"<<re<<"+"<<"i"<<im<<endl;
}
};

int main()
{
complex c1,c2,c3,c4;
c1.getdata();
c2.setdata();
c3.Enterdata();
c4=c1+c2+c3;
c4.showdata();
return 100;

}


8.Concept of Constructor and Destructor
#include<iostream>
using namespace std;

class test
{
public:
test()
{
cout<<"The control is inside the constructor"<<"\n";
cout<<"The object is in itialized here"<<"\n";
}
~test()
{
cout<<"The control is inside the destructor"<<"\n";
cout<<"The object goes out of scope"<<"\n";
cout<<"Clean up process is done"<<"\n";
}
};

int main()
{
test a;//constuctor called
cout<<"Main function terminating........."<<"\n";
cout<<"Object a goes out of scope"<<"\n";
return 0;
}

9.Concept of Reference in C++
#include<iostream>

using namespace std;

float &assign(float&,float&);

int main()
{
cout<<"Enter any two numbers"<<endl;
float a,b;
cin>>a>>b;
cout<<"Before calling"<<endl;
cout<<"a="<<a<<endl<<"b="<<b<<endl;
float smaller=assign(a,b);
cout<<"After calling function"<<endl;
cout<<"smaller="<<smaller;
return 0;
}

float &assign(float&x,float&y)
{
if(x<y)
return x;
else
return y;
}









2 comments:

  1. It is a very informative and useful post thanks it is good material to read this post increases my knowledge. Mechanical Engineering Degree

    ReplyDelete
  2. Join SEO training in Chennai with Aorta Digital Services. We provide a well-educated and experienced faculty who are aware of all SEO trends. For more information: SEO training in Chennai

    ReplyDelete