Slxxxx
2021-06-29 11:26
采纳率: 100%
浏览 46

C++继承题,要代码ballball了

创建一个名为Person的类,该类包含以下数据:Lastname、Eirstname、Address、City、State、Zip、Phone

创建第二个名为Customer的类,该类继承Person并包含以下数据:

o可以设置的唯一字符串值。如果在创建客户时未提供,则应自动生成。)

o电子邮件地址

List(一个布尔变量,指示它们是否是邮件列表的成员)

所有的顾客都是人,但人不一定是顾客。

为这两个类编写所有适当的构造函数、访问器和mutator函数。

所有类都必须有一个空构造函数、各种各样的构造函数排列(根据需要)以及任何必要的析构函数。

创建一个名为Customer的文本文件。txt,文件的每一行都包含

个人客户的信息。各个字段应该用逗号分隔,并且您应该用至少20条记录填充文档。

使用文件流从文件中读取数据,将每行的数据存储到Customer类的单个实例中。

存储在动态分配的数组中创建的客户类的每个实例。换句话说,您应该有一个动态分配的数组,其中包含Customer类的所有实例。客户中的每一行数据都应该有一个客户类实例。txt文件。

将数组传递给函数,以便在格式良好的表中打印出所有客户信息。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qfl_sdu 2021-06-29 23:53
    已采纳

    Customer.txt内容如下

    1000000001,1,Zhang,San,Xingfudajie 4-21,New York,Youta,aaa,123456789
    1000000002,1,Zhou,San,Xingfudajie 5-21,New York,Youta,aaa,223456789
    1000000003,1,Wu,San,Xingfudajie 6-21,New York,Youta,aaa,323456789
    1000000004,1,Zheng,San,Xingfudajie 7-21,New York,Youta,aaa,423456789
    1000000005,1,Wang,San,Xingfudajie 8-21,New York,Youta,aaa,523456789
    1000000006,1,Liu,San,Xingfudajie 4-44,New York,Youta,aaa,623456789
    1000000007,1,Tang,San,Xingfudajie 4-23,New York,Youta,aaa,723456789
    1000000008,1,Han,San,Xingfudajie 4-18,New York,Youta,aaa,823456789
    1000000009,1,Dong,San,Xingfudajie 3-22,New York,Youta,aaa,923456789
    1000000010,1,Gao,Yi,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000011,1,Gao,Yang,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000012,1,Gao,Hao,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000013,1,Gao,Tian,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000014,1,Gao,Xiang,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000015,1,Gao,Mei,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000016,1,Gao,Li,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000017,1,Gao,Huan,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000018,1,Gao,Juan,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000019,1,Gao,Ya,Xingfudajie 7-21,New York,Youta,aaa,103456789
    1000000020,1,Gao,Song,Xingfudajie 7-21,New York,Youta,aaa,103456789
    

    代码如下:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <time.h>
    using namespace std;
    
    class People
    {
    private:
        string Lastname;
        string Firstname;
        string Address;
        string City;
        string State;
        string Zip;
        string Phone;
    public:
        People(){}
        People(string ln,string fn,string addr, string ct,string state,string zip,string phone)
        {
            Lastname = ln;
            Firstname = fn;
            Address = addr;
            City = ct;
            State = state;
            Zip = zip;
            Phone = phone;
        }
        ~People(){}
        //setter
        void setLastName(string ln){Lastname = ln;}
        void setFirstName(string fn){Firstname = fn;}
        void setAddress(string a){Address = a;}
        void setCity(string ct){City = ct;}
        void setState(string st){State = st;}
        void setZip(string zip){Zip = zip;}
        void setPhone(string ph){Phone = ph;}
    
        //getter
        string getLastName(){return Lastname;}
        string getFirstName(){return Firstname;}
        string getAddress(){return Address;}
        string getCity(){return City;}
        string getState(){return State;}
        string getZip(){return Zip;}
        string getPhone(){return Phone;}
    };
    
    class Customer : public People
    {
    private:
        string CustomerNmb;
        bool MailList;
    public:
        Customer(){}
        Customer(string ln,string fn,string addr, string ct,string state,string zip,string phone,string nmb,bool b):People(ln,fn,addr,ct,state,zip,phone)
        {
            CustomerNmb = nmb;
            MailList = b;
        }
        ~Customer(){}
        //setter
        void setCustomNmb(string nmb){CustomerNmb = nmb;}
        void setMailList(bool b){MailList = b;}
        //getter
        string getCustomNmb()
        {
            if (CustomerNmb.empty())
            {
                //自动生成一个Custom Number
                char buf[11]={0};
                srand((unsigned int)time(0));
                for (int i = 0;i<10;i++)
                {
                    buf[i] = '0'+ rand()%10;
                }
                CustomerNmb = buf;
            }
            return CustomerNmb;
        }
        bool getMailist(){return MailList;}
    
        friend istream & operator>>(istream &is,Customer &c)
        {
            char buf[200] = {0};
            is.getline(buf,200);//读入一行数据
            string s = buf;
            int startpos = s.find(",",0);
            if(startpos < 0) return is;
            //第一个元素Custom nmb
            c.setCustomNmb(s.substr(0,startpos));
    
            //第二个元素是否在列表中
            int endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            string blist = s.substr(startpos,endpos - startpos);
            startpos = endpos;
            if (blist.compare("1")==0)
                c.setMailList(true);
            else
                c.setMailList(false);
    
            //第三个元素Lastname
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            c.setLastName(s.substr(startpos,endpos - startpos));
            startpos = endpos;
    
            //第四个元素
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            c.setFirstName(s.substr(startpos,endpos - startpos));
            startpos = endpos;
    
            //第五个元素address
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            c.setAddress(s.substr(startpos,endpos - startpos));
            startpos = endpos;
    
            //第六个元素city
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            c.setCity(s.substr(startpos,endpos - startpos));
            startpos = endpos;
    
            //第七个元素state
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            c.setState( s.substr(startpos,endpos - startpos));
            startpos = endpos;
    
            //第八个元素zip
            endpos = s.find(",",startpos+1);
            if(endpos < 0) return is;
            startpos +=1;
            c.setZip(s.substr(startpos,endpos - startpos));
            startpos = endpos;
            
            //第9个元素phone
            startpos +=1;
            endpos = s.length();
            if(endpos < 0) return is;
            c.setPhone(s.substr(startpos,endpos - startpos));
    
            return is;
        }
    
        void display()
        {
            cout << CustomerNmb << "," << MailList << "," << getLastName() <<","<<getFirstName()<< ",";
            cout << getAddress()<<"," << getCity() << "," << getState()<<"," << getZip() << "," << getPhone() << endl; 
        }
    };
    //读文件
    Customer* ReadFile(const char* name,int &nmb)
    {
        int size= 40;
        nmb = 0;
        Customer* a = new Customer[size]; //申请40个空间
        ifstream is(name);
        if (!is.is_open())
        {
            cout << "文件打开失败" << endl;
            return 0;
        }
        while(!is.eof())
        {
            Customer cus;
            is >> cus;
            if(nmb >= size)
            {
                size = size * 2;
                Customer* pt = new Customer[size];
                for (int i=0;i< nmb;i++)
                {
                    pt[i] = a[i];
                }
                delete[] a;
                a = pt;
            }
            a[nmb++] = cus;
        }
        is.close();
        return a;
    }
    
    
    
    int main()
    {
        int nmb = 0;
        Customer* pp = 0;
        pp = ReadFile("Customer.txt",nmb);  //读取文件
        for (int i = 0; i< nmb;i++)
        {
            pp[i].display();
        }
    
        delete[] pp;
        pp = 0;
        system("pause");
        return 0;
    }
    
    点赞 1 评论
  • CSDN专家-link 2021-06-29 11:47

    mutator函数是干啥的

    各种各样的构造函数排列---排列啥意思?

    #include <stdio.h>
    #include <stdlib.h>
    #include<String>
    #include <time.h>
    #include <iostream>
    using namespace std;
    
    typedef struct _PERSON_DATA
    {
        string Lastname;
        string Firstname;
        string Address;
        string City;
        string State;
        string Zip;
        string Phone;
    }PERSON_DATA;
    static int pnum = 0;
    class Person
    {
        private:
            PERSON_DATA data;
        public:
            Person() {}
            Person(const PERSON_DATA &d)
            {
                data = d;
            }
    		PERSON_DATA getData() {return data;};
            void setData(PERSON_DATA d) {data = d;}
            void print() {cout<<data.Lastname<<"\t"<<data.Firstname<<"\t"<<data.Address<<"\t"<<data.City<<"\t"<<data.State<<"\t"<<data.Zip<<"\t"<<data.Phone<<"\t";}
    };
    class Customer : public Person
    {
        private:
            string num;
            string mail;
            int list;
        public:
            Customer() {char a[10];itoa(pnum,a,10);pnum++;num = a;}
            Customer(string n,string m,int l,PERSON_DATA data) : Person(data)
            {
                num = n;
                mail = m;
                list = l;
            }
        string getnum() {return num;}
        string getmail() {return mail;}
        int getlist() {return list;}
        void setnum(string n) {num = n;}
        void setmail(string m) {mail = m;}
        void setlist(int l) {list = l;}
        void print() {Person::print();cout<<num<<"\t"<<mail<<"\t"<<list<<endl;}
    };
    Customer *pC = NULL;
    int cnum = 0;
     
    void print(Customer *p,int n)
    {
        cout<<"Lastname\tFirstname\tAddress\tCity\tState\tZip\tPhone\tnum\tmail\tlist"<<endl;
        for(int i=0;i<n;i++)
            p[i].print();
    }
    int main()
    {
        pC = new Customer[1000];
        FILE *fp;
        fp = fopen("Customer.txt","r");
        char buf[1000] = {0};
        while (fgets(buf,1000,fp))
        {
            PERSON_DATA data;
            string num,mail;
            int list;
            sscanf(buf,"%s %s %s %s %s %s %s %s %s %d",data.Lastname,data.Firstname,data.Address,data.City,data.State,data.Zip,data.Phone,num,mail,&list);
            pC[cnum].setData(data);
            pC[cnum].setnum(num);
            pC[cnum].setmail(mail);
            pC[cnum].setlist(list);
            cnum++;
        }
        fclose(fp);
        print(pC,cnum);
    }
    
    点赞 评论

相关推荐 更多相似问题