title: Address Book using Inheritance and Operator Overloading or Exception handling
body: <p>I have to make an address book using C++ and should include classes and inherited classes. The main class contains name and address and the inherited Teacher class contains teacher Phone number and CNIC and Employee class contains Phone and CNIC and the Student class contains student ID and Phone. I made a program in C++ but that would always over write the previous data. I am stuck here. I would need a skull program or simple algorithms to guide me ahead.</p>
  
  <p>This is my code.</p>
  
  <pre><code>            // new.cpp : Defines the entry point for the console application.
  
              // Address Book.cpp : Defines the entry point for the console application.
              //
  
              #include "stdafx.h"
              #include&lt;iostream&gt;
              #include&lt;string&gt;
              #include &lt;conio.h&gt;
              #include &lt;fstream&gt;
              #include &lt;sstream&gt; 
              #include &lt;iomanip&gt;
  
              using namespace std;
  
              class Main
              {
                  private:
                  string Name;
                  string Address;
                  char test;
  
                  public:
                  virtual void getdata()
                  {
                      ofstream outfile("TEMP.txt");
                      cout &lt;&lt;"Enter Name: " ;
                      cin&gt;&gt;test;
                      getline(cin, Name); //cin &gt;&gt; Name;
                      cout &lt;&lt; endl;
                      cout &lt;&lt; "Enter Address: ";
                      getline(cin, Address);// cin &gt;&gt; Address;
  
                      outfile &lt;&lt; Name
                      &lt;&lt; endl
                      &lt;&lt; Address;
                  }
                  virtual void setdata()
                      {
                      cout &lt;&lt; "\n\nName: " &lt;&lt; test+Name;
                      cout &lt;&lt; "\nAddress: " &lt;&lt; Address;
                      }
  
  
                  virtual void remove()
                  {
  
  
  
                  }
                  };
  
  
              //----------------------------------------------------------
  
              class Teacher : public Main
              {
              private:
  
                  int T_Number;
                  string T_CNIC;
  
              public:
                  void getdata()
                  {
                  ofstream outfile("Teacher.txt");
                  Main::getdata();
                  cout &lt;&lt; "Enter CNIC: ";
                  getline(cin,T_CNIC);//cin &gt;&gt; T_CNIC;
                  cout &lt;&lt; "Enter Contact Number: " &lt;&lt; endl;
                  cin &gt;&gt; T_Number;
  
                  outfile &lt;&lt; T_CNIC
                      &lt;&lt; endl
                      &lt;&lt; T_Number;
  
                  cout &lt;&lt; "Data entered" &lt;&lt; endl;
  
                  }
  
                  void setdata()
                  {
                      Main::setdata();
                      cout &lt;&lt; "\nTeacher CNIC: " &lt;&lt; T_CNIC;
                      cout &lt;&lt; "\nTeacher Contact Number: " &lt;&lt; T_Number;
                      }
                  };
              //------------------------------------------------------------
  
              class Student : public Main
              {
  
              private:
  
              int S_Number;
              int S_ID;
              public:
              void getdata()
              {
                  ofstream outfile("Student.txt");
                  Main::getdata();
                  cout &lt;&lt; "Enter ID: ";
                  cin &gt;&gt; S_ID;
                  cout &lt;&lt; "Enter Contact Number: ";
                  cin &gt;&gt; S_Number;
  
                  outfile &lt;&lt; S_ID
                      &lt;&lt; endl
                      &lt;&lt; S_Number;
  
                  cout &lt;&lt; "Data entered" &lt;&lt; endl;
  
              }
              void setdata()
              {
                  Main::setdata();
                  cout &lt;&lt; "\nStudent Unique ID: " &lt;&lt; S_ID;
                  cout &lt;&lt; "\nStudent Contact Number: " &lt;&lt; S_Number;
              }
  
              };
  
              class Employee : public Main
              {
              private:
              int E_Number;
              string E_CNIC;
              public:
              void getdata()
              {
                  ofstream outfile("Employee.txt");
                  Main::getdata();
                  cout &lt;&lt; "Enter Employee CNIC: ";
                  getline(cin,E_CNIC);//cin &gt;&gt; E_CNIC;
                  cout &lt;&lt; "Enter Contact Number: ";
                  cin &gt;&gt; E_Number;
  
                  outfile &lt;&lt; E_CNIC
                      &lt;&lt; endl
                      &lt;&lt; E_Number;
  
                  cout &lt;&lt; "Data entered" &lt;&lt; endl;
  
                  ;}
              void setdata()
              {
                  Main::setdata();
                  cout &lt;&lt; "\nEmployee Unique ID: " &lt;&lt; E_CNIC;
                  cout &lt;&lt; "\nEmployee Contact Number: " &lt;&lt; E_Number;
  
              }
              };
  
  
  
              //-----------------------------------------------------------------------------------------
  
              int _tmain(int argc, _TCHAR* argv[])
              {
              Main* myarr[100];
  
              int var = 0;
  
              int choice;
                  char input;
              start:
                  printf("===============MAIN MENU===============");
                  printf("\n[1]Insert\n[2]Search\n[3]Delete\n[4]Exit\n");
                  printf("=======================================");
                  printf("\n\nEnter Your Choice: ");
                  cin &gt;&gt; choice;
  
                  switch(choice)
                  {
  
                  case 1:
                      cout &lt;&lt; "\nEnter data for Student or Teacher or Employee (s/t/e)? ";
                      cin &gt;&gt; input;
                  do 
                  {
  
  
                      if( input=='t' )
                      myarr[var] = new Teacher;
                      else if (input == 'e')
                      myarr[var] = new Employee;
                      else
                      myarr[var] = new Student;
                      myarr[var++]-&gt;getdata();
                      cout &lt;&lt; " Enter another (y/n)? ";
                      cin &gt;&gt; input;
                      if(input == 'n')
                      {
                          goto start;
                      }
                  }
                  while( input =='y');
                      for(int j=0; j&lt;var; j++)
                      myarr[j]-&gt;setdata(); 
                      cout &lt;&lt; endl;
                      break;
  
                  case 2:
                      return 0;
                      break;
  
                  case 3:
                      return 0;
                      break;
  
                  case 4:
                      cout &lt;&lt; "Are you sure you want to quit? " &lt;&lt; endl;
                      getch();
                      break;
  
                  default:
                      cout &lt;&lt; "Choose from 1 - 4 ONLY!!" &lt;&lt; endl;
                      goto start;
                      break;  
  
  
  
                  }   
              return 0;
              }
  </code></pre>