Thursday, April 27, 2017

File Handling: Reading and Writing into a text file using class

0 comments

Code

#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;

class marks
{
      private:
              int oop;
              int cal;
              int dis;
              int s;
              int a;
             
      public:
             marks():oop(0),cal(0),dis(0)
               {    }
           
             void getdata()
             {
                  cout<<"Enter marks of oop: ";
                  cin>>oop;
                  cout<<"Enter marks of cal: ";
                  cin>>cal;
                  cout<<"Enter marks of dis: ";
                  cin>>dis;
             }
           
             void display()
             {
                  cout<<"OOP = "<<oop<<endl;
                  cout<<"Cal = "<<cal<<endl;
                  cout<<"Dis = "<<dis<<endl;
             }
};

int main ()
{
    marks m;
    int s;
   
    m.getdata();
                                                        //opening the file in binary mode
    ofstream myfile("Writing_Reading_class.txt",ios::binary);        
                                                     
                                                       //writing in the file
    myfile.write(reinterpret_cast<char *>(&m),sizeof(m));
                 //closing the file
    myfile.close();
                                                      //re-opening the file in binary mode
    ifstream my("Writing_Reading.txt",ios::binary);
                                                      //Reading from the file
    my.read(reinterpret_cast<char *>(&m),sizeof(m));
             //display
    m.display();
   
    cout << "\n\nPress any key to close.";
    getch ();
    return 0;
}

Output


Read More..

Friday, March 10, 2017

find index number of the largest element using class

0 comments

Problem

Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint() that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array and the number of elements in it, and return the index number of the largest element. The program should call this function and then display the largest element and its index number.

Code

// p[4].cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class intgrs
{
private:
int a[5];
int i,j,z;

public:
intgrs():j(0),i(0),z(0)
{ }

void maxint()
{
cout<<"Enter Integers: ";
for(i=0; i<5; i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
if(a[i]>j)
{
j = a[i];
z=i;
}
}
cout<<"\n\nLargest Element = "<<j;
cout<<"\n\nIndex Number = "<<(z+1)<<endl;
}
};

int main()
{
intgrs i;

i.maxint();

cout<<"\n\nPress any key to close.";
getch();
return 0;
}

Output

find index number of the largest element using class

Read More..

Thursday, March 9, 2017

Input and display employee data of 100 employees using array of objects

1 comments

Problem

Create a class called employee that contains a name (an object of class string) and an employee number (type long). Include a member function called getdata() to get data from the user for insertion into the object, and another function called putdata() to display the data. Assume the name has no embedded blanks.
Write a main() program to exercise this class. It should create an array of type employee, and then invite the user to input data for up to 100 employees. Finally, it should print out the data for all the employees.

Code

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class employee
{
private:
char name[50];
long no;

public:
employee():no(0)
{ }

void getdata()
{
fflush(stdin);
cout<<"\nEnter Employee Data:\n";
cout<<"Enter Name: ";
cin.get(name,50);
fflush(stdin);
cout<<"Enter Number: ";
cin>>no;
fflush(stdin);
}

void putdata()
{
cout<<"\n\nName= "<<name<<"\nE.number = "<<no<<endl;
}

};

int main()
{
int array_length = 3;
employee e1[array_length];

for(int i=0;i<array_length;i++){
e1[i].getdata();
fflush(stdin);
}

for(int i=0;i<array_length;i++){
e1[i].putdata();
}

cout<<"\n\nPress any key to close.";
getch();
return 0;
}

Output


Input and display employee data of 100 employees using array of objects
Read More..

Wednesday, October 19, 2016

Polymorphism example in c++ language

0 comments

Problem

Implement the following given scenario, include two  constructors (no argument, with arguments), two functions getvalue(), and  displayvalue() for all classes.
polymorphism program in c++

Code

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Vehicle
{
      protected:
                int i;
                string s;
     
      public:
             Vehicle(): i(0), s("")
             {         }
             Vehicle(int a,string ss): i(a), s(ss)
             {           }
             void getdata()
             {
                  cout<<"Enter Vehicle Number: ";
                  cin>>i;
                  fflush(stdin);
                  cout<<"Enter vehicle name: ";
                  getline(cin, s);
             }
           
             void display()
             {
                  cout<<"Vehicle Number: "<<i;
                  cout<<"Vehicle Name: "<<s;
             }
};

class land_Vehicle : public Vehicle
{
      protected:
                int b;
                string s1;
     
      public:
             land_Vehicle(): b(0), s1("")
             {         }
             land_Vehicle(int a,string ss, int r, string s0): b(a), s1(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Land Vehicle Number: ";
                  cin>>b;
                  fflush(stdin);
                  cout<<"Enter Land vehicle name: ";
                  getline(cin, s1);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<b;
                  cout<<"Vehicle Name: "<<s1;
             }
};

class Boat : public Vehicle
{
      protected:
                int c;
                string s2;
     
      public:
             Boat(): c(0), s2("")
             {         }
             Boat(int a,string ss, int r, string s0): c(a), s2(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Boat Number: ";
                  cin>>c;
                  fflush(stdin);
                  cout<<"Enter Boat name: ";
                  getline(cin, s2);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<c;
                  cout<<"Vehicle Name: "<<s2;
             }
};

class aircraft : public Vehicle
{
      protected:
                int d;
                string s3;
     
      public:
             aircraft(): d(0), s3("")
             {         }
             aircraft(int a,string ss, int r, string s0): d(a), s3(ss), Vehicle(r,s0)
             {           }
             void getdata()
             {
                  Vehicle :: getdata();
                  cout<<"Enter Aircraft Number: ";
                  cin>>d;
                  fflush(stdin);
                  cout<<"Enter Aircarft name: ";
                  getline(cin, s3);
             }
           
             void display()
             {
                  Vehicle :: display();
                  cout<<"Vehicle Number: "<<d;
                  cout<<"Vehicle Name: "<<s3;
             }
};

class car : public land_Vehicle
{
      protected:
                int e;
                string s4;
     
      public:
             car(): e(0), s4("")
             {         }
             car(int a,string ss, int q1, string q2,int q3,string q4): e(a), s4(ss),land_Vehicle(q1,q2,q3,q4)
             {
             }
             void getdata()
             {
                  land_Vehicle :: getdata();
                  cout<<"Enter car Number: ";
                  cin>>e;
                  fflush(stdin);
                  cout<<"Enter car name: ";
                  getline(cin, s4);
             }
           
             void display()
             {
                  land_Vehicle :: display();
                  cout<<"Vehicle Number: "<<e;
                  cout<<"Vehicle Name: "<<s4;
             }
};

class truck  : public land_Vehicle
{
      protected:
                int f;
                string s5;
     
      public:
             truck(): f(0), s5("")
             {         }
             truck(int a,string ss, int q, string qq,int q3,string q4): f(a), s5(ss),land_Vehicle(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  land_Vehicle :: getdata();
                  cout<<"Enter truck Number: ";
                  cin>>f;
                  fflush(stdin);
                  cout<<"Enter truck name: ";
                  getline(cin, s5);
             }
           
             void display()
             {
                  land_Vehicle :: display();
                  cout<<"Vehicle Number: "<<f;
                  cout<<"Vehicle Name: "<<s5;
             }
};

class sports_car  : public car
{
      protected:
                int g;
                string s6;
     
      public:
             sports_car(): g(0), s6("")
             {         }
           
             sports_car(int a,string ss, int q, string qq,int q3,string q4,int q5,string q6): g(a), s6(ss),car(q,qq,q3,q4,q5,q6)
             {           }
             void getdata()
             {
                  car:: getdata();
                  cout<<"Enter sports car Number: ";
                  cin>>g;
                  fflush(stdin);
                  cout<<"Enter sports car name: ";
                  getline(cin, s6);
             }
           
             void display()
             {
                  car:: display();
                  cout<<"Vehicle Number: "<<g;
                  cout<<"Vehicle Name: "<<s6;
             }
};

class yacht  : public Boat
{
      protected:
                int h;
                string s7;
     
      public:
             yacht(): h(0), s7("")
             {         }
             yacht(int a,string ss, int q, string qq,int q3,string q4): h(a), s7(ss),Boat(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  Boat::getdata();
                  cout<<"Enter yacht Number: ";
                  cin>>h;
                  fflush(stdin);
                  cout<<"Enter yacht name: ";
                  getline(cin, s7);
             }
           
             void display()
             {
                  Boat :: display();
                  cout<<"Vehicle Number: "<<h;
                  cout<<"Vehicle Name: "<<s7;
             }
};

class sail_boat  : public Boat
{
      protected:
                int k;
                string s8;
     
      public:
             sail_boat(): k(0), s8("")
             {         }
             sail_boat(int a,string ss, int q, string qq,int q3,string q4): k(a), s8(ss),Boat(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  Boat :: getdata();
                  cout<<"Enter sail_boat Number: ";
                  cin>>k;
                  fflush(stdin);
                  cout<<"Enter sail_boat name: ";
                  getline(cin, s8);
             }
           
             void display()
             {
                  Boat :: display();
                  cout<<"Vehicle Number: "<<k;
                  cout<<"Vehicle Name: "<<s8;
             }
};

class f16 : public aircraft
{
       protected:
                int j;
                string s9;
     
      public:
             f16(): j(0), s9("")
             {         }
             f16(int a,string ss, int q, string qq,int q3,string q4): j(a), s9(ss), aircraft(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  aircraft :: getdata();
                  cout<<"Enter f16 Number: ";
                  cin>>j;fflush(stdin);
                  cout<<"Enter f16 model: ";
                  getline(cin, s9);
             }
           
             void display()
             {
                  aircraft :: display();
                  cout<<"f16 Number: "<<j;
                  cout<<"f16 model: "<<s9;
             }
};

class helicopter  : public aircraft
{
      protected:
                int l;
                string s10;
     
      public:
             helicopter(): l(0), s10("")
             {         }
             helicopter(int a,string ss, int q, string qq,int q3,string q4): l(a), s10(ss),aircraft(q,qq,q3,q4)
             {           }
             void getdata()
             {
                  aircraft :: getdata();
                  cout<<"Enter helicopter Number: ";
                  cin>>l;
                  cout<<"Enter helicopter model: ";
                  getline(cin, s10);
             }
           
             void display()
             {
                  aircraft :: display();
                  cout<<"helicopter Number: "<<l;
                  cout<<"helicopter model: "<<s10;
             }
};

int main()
{
    aircraft a;
    a.getdata();
    a.display();
   
    Boat b;
    b.getdata();
    b.display();
   
    land_Vehicle l;
    l.getdata();
    l.display();
   
    car c;
    c.getdata();
    c.display();
   
    truck t;
    t.getdata();
    t.display();
   
    sports_car sp;
    sp.getdata();
    sp.display();
   
    yacht y;
    y.getdata();
    y.display();
   
    sail_boat s;
    s.getdata();
    s.display();
   
    f16 f;
    f.getdata();
    f.display();
   
    helicopter h;
    h.getdata();
    h.display();
   
    cout<<"Press any key exit.";
    getch();
    return 0;
}

Output

polymorphism program in c++

Read More..

Thursday, October 13, 2016

CD player simulator using c++

0 comments

Problem

Design a simple CD player simulator. The CD player has the following buttons: "POWER", "EJECT", "PLAY" and "STOP", and a tray where a CD is loaded. User can load a CD only if the tray is open. The tray can be opened only if the power is turned on (regard it as something like a motor-driven tray installed on a PC). If the power is turned off with the tray open, the tray will keep open and the power is turned off.
Implement the following actions of the CD player as methods of class CDPlayer.
  • pushPower() -- simulates pushing POWER(on/off) button.
    • if the power is turned off, turn on the power, and display "Power turned on."
    • if the power is turned on,
      • if playing, stop it.
turn off the power, and display "Power turned off."
  • pushEject() -- simulates pushing EJECT(tray open/close) button
    • if the power is not turned on, display "Power not turned on."
    • if the power is turned on,
      • if the tray is not open, open the tray and display "Tray opened."
      • if the tray is open, close the tray and display "Tray closed."
  • pushPlay() -- simulates pushing PLAY button
    • if the power is turned on,
      • if the media is loaded and tray is closed, begin to play and display "Now playing..."
      • otherwise, display "No CD loaded or tray is open."
  • pushStop() -- simulates pushing STOP button
    • if the power is turned on,
      • if it's playing, stop playing and display "Playing stopped."
  • loadUnloadMedia() -- simulates loading/unloading media to/from the tray (tray must be open when loading/unloading)
    • if the tray is open,
      • if the media is loaded, unload the media and display "Media unloaded."
      • if the media is not loaded, load the media and display "Media loaded."
    • if the tray is not open,
      • display "Tray is not open."

Code

#include<iostream>
#include<conio.h>
using namespace std;


class CDplayer
{
      private:
              int power;
              int eject;
              int tray;
              int media;
              int play;
              
      public:
      CDplayer():power(0),eject(0),tray(0),media(0),play(0)
      {
      }
      
      void pushPower()
      {
           cout<<"\n\nNOTE: Press one(1) to turn on and zero(0) to turn off.";
           cout<<"\nWant to turn on the power? ";
           cin>>power;
           
           if(power == 1)
                cout<<"Power is ON.";
           else if (power == 0)
                cout<<"Power is OFF.";
           else
               cout<<"Invalid key";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushEject()
      {
           if(power == 1)
           {
                if(tray == 0)
                {
                         tray = 1;
                         cout<<"Tray opened.";
                }
                else if(tray == 1)
                {
                     tray = 0;
                     cout<<"Tray closed.";
                }
           }
           else if(power == 0)
                cout<<"Power not turned on.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushPlay() 
      {
           if(power == 1)
           {
                if(media == 1 && tray == 0)
                {
                    play = 1;
                    cout<<"Now playing...";
                }
                else
                    cout<<"No CD loaded or tray is open.";
           }
           else if(power == 0)
                cout<<"Power not turned on.";
                
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushStop() 
      {
           if(power == 1)
           {
                if(play == 1)
                {
                        play = 0;
                        cout<<"Playing stopped.";
                }
           }
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void loadUnloadMedia()
      {
           if(tray == 1)
           {
                   if(media == 1)
                   {
                            media = 0;
                            cout<<"Media unloaded.";
                   }
                   else if(media == 0)
                   {
                        media = 1;
                        cout<<"Media loaded.";
                   }
           }
           else if(tray == 0)
                cout<<"Tray is not open.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
};

int main()
{
    CDplayer c1;
    int s;
    while(1)
    {
            system ("cls");
            cout<<"\n\nPlease, Press one of the following buttons: ";
            cout<<"\n1. POWER(on/off)";
            cout<<"\n2. EJECT(tray open/close)";
            cout<<"\n3. PLAY";
            cout<<"\n4. STOP";
            cout<<"\n5. Load/Unload media to/from the tray(tray will be opened)";
            cout<<"\n6. Close CDplayer";
            
            cout<<"\n\nEnter your choice: ";
            cin>>s;
            
            switch(s)
            {
                     case 1:
                          system ("cls");
                          c1.pushPower();
                          break;
            
                     case 2:
                          system ("cls");
                          c1.pushEject();
                          break;
            
                     case 3:
                          system ("cls");
                          c1.pushPlay();
                          break;
            
                     case 4:
                          system ("cls");
                          c1.pushStop();
                          break;
            
                     case 5:
                          system ("cls");
                          c1.loadUnloadMedia();
                          break;
            
                     case 6:
                          exit(0);
            }
    }
    
    getch();
    return 0;
}

Output

CD player simulator using c++
CD player simulator using c++

Read More..

Overload - and > operator for class Time

0 comments

Problem

Overload - and > operator for class Time, so that this will work:
if (Time2 > Time1)
Time3 = Time2 - Time1;
else
Time3 = Time1 - Time2;

Code

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class time
      {
      private:
            int hr;
            int min;
            int sec;

      public:
            time():hr(0),min(0),sec(0)
            {     }

            void getdata()
            {
                  cout<<"Enter Hours: ";
                  cin>>hr;
                  cout<<"Enter Minutes: ";
                  cin>>min;
                  cout<<"Enter Seconds: ";
                  cin>>sec;
            }

            bool operator > (time t1)
            {
                  if(hr>t1.hr)
                        return true;
                  else if(hr == t1.hr)
                  {
                        if(min>t1.min)
                              return true;
                        else if(min == t1.min)
                        {
                              if (sec > t1.sec)
                                    return true;
                              else if(sec == t1.sec)
                              {
                                    cout<<"\n\nTimes are EQUAL.";
                                    cout<<"\n\nPress any key to close.";
                                    getch();                           
                                    exit(0);
                              }
                              else
                                    return false;
                        }
                        else
                              return false;
                  }
                  else
                        return false;
            }

            time operator - (time t1)
            {
                  time temp;
                  temp.hr = hr - t1.hr;
                  temp.min = min - t1.min;
                  temp.sec = sec - t1.sec;

                  if(temp.min < 0)
                        temp.min = (temp.min * (-1));
                  if(temp.sec < 0)
                        temp.sec = (temp.sec * (-1));
                 
                  return temp;
            }

            void display()
            {
                  cout<<hr<<":"<<min<<":"<<sec;
            }

      };

int main()
{
      time time1,time2,time3;
     
      cout<<"Enter 1st Time: \n";
      time1.getdata();

      cout<<"\n\nEnter 2nd Time: \n";
      time2.getdata();

      if(time2>time1)
      {
            time3 = time2 - time1;
            cout<<"\n\n2nd Time is greater.";
            cout<<"\nTime after subtracting: ";time3.display();
      }
      else
      {
            time3 = time1 - time2;
            cout<<"\n\n1st Time is greater.";
            cout<<"\nTime after subtracting: ";time3.display();
      }
     
      cout<<"\n\nPress any key to close.";
      getch();
      return 0;
}

Output

Overload - and > operator for class Time

Read More..

Class of Employee with static members to count number of employees in the particular department.

0 comments

Problem

Create a Class of Employee with static members to count number of employees in the particular department?

Code

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class employee
      {
      private:
            static int cs;
            static int phy;
            static int maths;
            static int eng;
            static int mtr;

      public:

            void comp()
            {     cs++;       }

            void PHY()
            {     phy++;      }

            void MATHS()
            {     maths++;    }

            void ENG()
            {     eng++;      }

            void MTR()
            {     mtr++;      }

            void display()
            {
                cout<<"\nNo. of employees in cs department      ="<<cs;
                cout<<"\nNo. of employees in physics department ="<<phy;
                cout<<"\nNo. of employees in maths department    ="<<maths;
                cout<<"\nNo. of employees in engineering department="<<eng;
                cout<<"\nNo. of employees in metrology department = "<<mtr;
                cout<<"\n\nTotal no. of employees= "<<(cs+phy+maths+eng+mtr);
            }
      };

int employee::cs=0;
int employee::phy=0;
int employee::maths=0;
int employee::eng=0;
int employee::mtr=0;



int main()
{
      employee c[15],phy[15],mth[15],eng[15],mtr[15],e;
      int i,n,j;

      cout<<"Enter number of employees of cs department: ";
      cin>>n;
      for(i=0;i<n;i++)
      {     c[i].comp();      }

      cout<<"Enter number of employees of physics department: ";
      cin>>n;
      for(i=0;i<n;i++)
      {     phy[i].PHY();     }

      cout<<"Enter number of employees of maths department: ";
      cin>>n;
      for(i=0;i<n;i++)
      {     c[i].MATHS();    }

     
      cout<<"Enter number of employees of engineering department: ";
      cin>>n;
      for(i=0;i<n;i++)
      {     c[i].ENG();    }
           
      cout<<"Enter number of employees of metrology department: ";
      cin>>n;
      for(i=0;i<n;i++)
      {     c[i].MTR();    }

      e.display();

      cout<<"\n\nPress any key to close.";
      getch();
      return 0;
}

Output

Class of Employee with static members to count number of employees in the particular department.

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact