使用继承和运算符重载或异常处理的通讯簿

Address Book using Inheritance and Operator Overloading or Exception handling

本文关键字:异常处理 重载 继承 运算符      更新时间:2023-10-16

我必须使用C++制作地址簿,并且应该包括类和继承的类。主类包含姓名和地址,继承的教师类包含教师电话号码和 CNIC,员工类包含电话和 CNIC,学生类包含学生 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 << "nnName: " << 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]Insertn[2]Searchn[3]Deleten[4]Exitn");
                printf("=======================================");
                printf("nnEnter 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(),而是直接将数据写入文件。 看看这是否有效。