使用继承和运算符重载或异常处理的通讯簿 我必须用C++编写一个通讯录,并且应该包括类和继承类。主类包含名称和地址,继承的教师类包含教师电话号码,CNIC和员工类包含电话和CNIC,学生类包含学生ID和电话。我用C++编写了一个程序,但总是会写以前的数据。我被困在这里了。我需要一个骷髅计划或简单的算法来指导我前进

使用继承和运算符重载或异常处理的通讯簿 我必须用C++编写一个通讯录,并且应该包括类和继承类。主类包含名称和地址,继承的教师类包含教师电话号码,CNIC和员工类包含电话和CNIC,学生类包含学生ID和电话。我用C++编写了一个程序,但总是会写以前的数据。我被困在这里了。我需要一个骷髅计划或简单的算法来指导我前进,c++,inheritance,C++,Inheritance,这是我的密码 // 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<iostr

这是我的密码

            // 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;
            }

然后我会尝试声明它受保护,而不是调用main::getdata,直接将数据写入文件。看看这是否有效。

没有区别。数据仍然被过度写入,一半存储在继承的类文件中,另一半存储在主类文件中。我该怎么解决这个问题