2012-04-29 5 views
0

C++を使用してアドレス帳を作成する必要があり、クラスと継承クラスを含める必要があります。メインクラスには名前と住所が含まれており、教師クラスには教師の電話番号とCNICが含まれ、従業員クラスには電話とCNICが含まれ、Studentクラスには学生IDと電話番号が含まれます。私はC++でプログラムを作ったが、それは前のデータを書き込むことが常に終わるだろう。私はここで立ち往生している。頭蓋骨プログラムや簡単なアルゴリズムが必要です。継承と演算子のオーバーロードまたは例外処理を使用したアドレス帳

これは私のコードです。

  // 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<iostream> 
      #include<string> 
      #include <conio.h> 
      #include <fstream> 
      #include <sstream> 
      #include <iomanip> 

      using namespace std; 

      class Main 
      { 
       private: 
       string Name; 
       string Address; 
       char test; 

       public: 
       virtual void getdata() 
       { 
        ofstream outfile("TEMP.txt"); 
        cout <<"Enter Name: " ; 
        cin>>test; 
        getline(cin, Name); //cin >> Name; 
        cout << endl; 
        cout << "Enter Address: "; 
        getline(cin, Address);// cin >> Address; 

        outfile << Name 
        << endl 
        << Address; 
       } 
       virtual void setdata() 
        { 
        cout << "\n\nName: " << test+Name; 
        cout << "\nAddress: " << 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 << "Enter CNIC: "; 
       getline(cin,T_CNIC);//cin >> T_CNIC; 
       cout << "Enter Contact Number: " << endl; 
       cin >> T_Number; 

       outfile << T_CNIC 
        << endl 
        << T_Number; 

       cout << "Data entered" << endl; 

       } 

       void setdata() 
       { 
        Main::setdata(); 
        cout << "\nTeacher CNIC: " << T_CNIC; 
        cout << "\nTeacher Contact Number: " << T_Number; 
        } 
       }; 
      //------------------------------------------------------------ 

      class Student : public Main 
      { 

      private: 

      int S_Number; 
      int S_ID; 
      public: 
      void getdata() 
      { 
       ofstream outfile("Student.txt"); 
       Main::getdata(); 
       cout << "Enter ID: "; 
       cin >> S_ID; 
       cout << "Enter Contact Number: "; 
       cin >> S_Number; 

       outfile << S_ID 
        << endl 
        << S_Number; 

       cout << "Data entered" << endl; 

      } 
      void setdata() 
      { 
       Main::setdata(); 
       cout << "\nStudent Unique ID: " << S_ID; 
       cout << "\nStudent Contact Number: " << S_Number; 
      } 

      }; 

      class Employee : public Main 
      { 
      private: 
      int E_Number; 
      string E_CNIC; 
      public: 
      void getdata() 
      { 
       ofstream outfile("Employee.txt"); 
       Main::getdata(); 
       cout << "Enter Employee CNIC: "; 
       getline(cin,E_CNIC);//cin >> E_CNIC; 
       cout << "Enter Contact Number: "; 
       cin >> E_Number; 

       outfile << E_CNIC 
        << endl 
        << E_Number; 

       cout << "Data entered" << endl; 

       ;} 
      void setdata() 
      { 
       Main::setdata(); 
       cout << "\nEmployee Unique ID: " << E_CNIC; 
       cout << "\nEmployee Contact Number: " << 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 >> choice; 

       switch(choice) 
       { 

       case 1: 
        cout << "\nEnter data for Student or Teacher or Employee (s/t/e)? "; 
        cin >> input; 
       do 
       { 


        if(input=='t') 
        myarr[var] = new Teacher; 
        else if (input == 'e') 
        myarr[var] = new Employee; 
        else 
        myarr[var] = new Student; 
        myarr[var++]->getdata(); 
        cout << " Enter another (y/n)? "; 
        cin >> input; 
        if(input == 'n') 
        { 
         goto start; 
        } 
       } 
       while(input =='y'); 
        for(int j=0; j<var; j++) 
        myarr[j]->setdata(); 
        cout << endl; 
        break; 

       case 2: 
        return 0; 
        break; 

       case 3: 
        return 0; 
        break; 

       case 4: 
        cout << "Are you sure you want to quit? " << endl; 
        getch(); 
        break; 

       default: 
        cout << "Choose from 1 - 4 ONLY!!" << endl; 
        goto start; 
        break; 



       } 
      return 0; 
      } 

答えて

0

次に、保護されたと宣言し、main :: getdata()を呼び出す代わりに、ファイルに直接データを書き込んでみます。それが動作するかどうかを確認します。

+0

違いはありません。データはまだ上書きされており、継承されたクラスファイルの半分とメインクラスファイルの半分に格納されています。どのように修正するのですか? – Moomal

関連する問題