包含基类/派生类对象的c++数组

C++ Array containing base/derived class objects

本文关键字:c++ 数组 对象 基类 派生 包含      更新时间:2023-10-16

我正试着把一个任务放在一起,却卡在了一点上。问题是用Student派生类创建Person类。然后重载<<和>>运算符。最后,创建检查程序来创建包含20个人的数组,并继续加载Person或Student。在任何时候,我们都可以打印到目前为止的结果——Person输出是Name char*/Age int/Parents *char[2], Student输出是Name char*/Age int/ID int

我的问题是与数组点-我不知道如何实现这一点,现在我被困在:

  • 指向person的指针数组
  • 如果是人/学生我们选择
  • istream获取数据

下面是主要代码部分:

#include <iostream>
#include <conio.h>
#include "Header.h"
using namespace std;
int main()
{
    char choice;
    Person* Tablica[20];
    Person* temp;
    int i = 0;
    while (1)
    {
        cout << "choices:" << endl;
        cout << "(p)erson, (s)tudent, s(h)ow, (e)nd" << endl;
        choice = _getch();
        if (choice == 'h' || choice == 'H'){
            for (int n = 0; n < i; n++){
                cout << *Tablica[n] << endl;
            }
        }
        if (choice == 'e' || choice == 'E'){ break; }
        if (choice == 'p' || choice == 'P'){
            temp = new Person;
            cin >> *temp;
            Tablica[i] = temp;
            cout << *Tablica[i] << endl;
            i++;
        }
        if (choice == 'S' || choice == 's'){
            temp = new Student;
            cin >> *temp;
            Tablica[i] = temp;
            cout << *Tablica[i] << endl;
            i++;
        }
    }
    system("PAUSE");
    return 0;
}

我能够加载第一个人/学生,然后代码中断没有错误。所以我想问的是,你能看一下代码给我指出正确的方向吗?

免责声明:我们必须使用数组,不能使用向量等。是的,conio.h也在那里,它必须留在那里……显然我是初学者。

:

#include <iostream>
class Person
{
public:
    Person();
    Person(const Person&);
    Person(char* n, int a, char* Parent1, char* Parent2);
    char* getName();
    int getAge();
    char* getDad();
    char* getMum();
    virtual ~Person();
    virtual Person operator=(const Person &);
    virtual Person operator+(const Person &);
    virtual Person operator+=(Person &);
    virtual void write(std::ostream&);
    virtual void read(std::istream&);
    friend std::istream& operator>>(std::istream&, Person &);
    friend std::ostream& operator<<(std::ostream&, Person &);
protected:
    char* name;
    int age;
    char* ParentName[2];
};
class Student : public Person
{
public:
    Student();
    Student(const Student&);
    Student(char* name, int age, int id);
    virtual ~Student();
    int ident();
    Student operator=(const Student &);
    Student operator+(const Student &);
    Student operator+=(Student &);
    virtual void write(std::ostream&);
    virtual void read(std::istream&);
    friend std::istream& operator>>(std::istream&, Student &);
    friend std::ostream& operator<<(std::ostream&, Student &);
private:
    int ID;
};

#include "Header.h"
Person::Person(){
    name = 0;
    age = 0;
    ParentName[0] = 0;
    ParentName[1] = 0;
}
Person::Person(const Person & other)
{
    name = other.name;
    age = other.age;
    ParentName[0] = other.ParentName[0];
    ParentName[1] = other.ParentName[1];
}
Person::Person(char* n, int a, char* Parent1, char* Parent2){
    name = n;
    age = a;
    ParentName[0] = Parent1;
    ParentName[1] = Parent2;
}
Person::~Person(){}
char* Person::getName(){ return name; }
int Person::getAge(){ return age; }
char* Person::getDad(){ return ParentName[0]; }
char* Person::getMum(){ return ParentName[1]; }
Person Person::operator=(const Person & other){
    name = other.name;
    age = other.age;
    ParentName[0] = other.ParentName[0];
    ParentName[1] = other.ParentName[1];
    return *this;
}
Person Person::operator+=(Person & other){
    int i;
    i = strlen(name) + strlen(other.name) + 4;
    char * temp = new char[i];
    strcpy_s(temp, i, name);
    strcat_s(temp, i, " - ");
    strcat_s(temp, i, other.name);
    name = temp;
    Person wynik(name, age, ParentName[0], ParentName[1]);
    return wynik;
}
Person Person::operator+(const Person & other){
    int i;
    i = strlen(name) + strlen(other.name) + 4;
    char * temp = new char[i];
    strcpy_s(temp, i, name);
    strcat_s(temp, i, " - ");
    strcat_s(temp, i, other.name);
    Person wynik(temp, age, ParentName[0], ParentName[1]);
    return *this;
}
void Person::write(std::ostream& os)
{
    os << "Osoba: name = " << this->getName() << ", wiek = " << this->getAge() << ", rodzice: " << this->getDad() << ", " << this->getMum();
}
std::ostream& operator<<(std::ostream& os, Person & other){
    other.write(os);
    return os;
}
void Person::read(std::istream& is)
{
    char* name;
    name = new char;
    std::cout << "name: " << std::endl;
    is >> name;
    std::cout << "age: " << std::endl;
    int age;
    is >> age;
    std::cout << "dad: " << std::endl;
    char* dad;
    dad = new char;
    is >> dad;
    std::cout << "mum: " << std::endl;
    char* mum;
    mum = new char;
    is >> mum;
    Person p(name, age, dad, mum);
    *this = p;
}
std::istream & operator>>(std::istream & is, Person & os){
    os.read(is);
    return is;
}
Student::Student() : Person(){}
Student::Student(const Student& student) : Person(student){
    ID = student.ID;
}
Student::Student(char* name, int age, int id) : Person(name, age, 0, 0){
    ID = id;
}
Student::~Student(){}
Student Student::operator=(const Student & student){
    Person::operator=(static_cast<Person const&>(student));
    ID = student.ID;
    return *this;
}
Student Student::operator+=(Student & student){
    Student wynik(*this);
    wynik.Person::operator=(wynik.Person::operator+=(student));
    return wynik;
}
Student Student::operator+(const Student& student)
{
    Person::operator+(static_cast<Person const&>(student));
    return *this;
}
void Student::write(std::ostream& os)
{
    os << "Student: name = " << this->getName() << ", age = " << this->getAge() << ", legitymacja: " << this->ident() << std::endl;
}
int Student::ident(){ return ID; }
std::ostream& operator<<(std::ostream& os, Student & other){
    other.write(os);
    return os;
}
void Student::read(std::istream& is)
{
    char* name;
    name = new char[20];
    std::cout << "name: " << std::endl;
    is >> name;
    std::cout << "age: " << std::endl;
    int age;
    is >> age;
    std::cout << "ID: " << std::endl;
    int id;
    is >> id;
    Student s(name, age, id);
    *this = s;
}

std::istream & operator>>(std::istream & is, Student & st){
    st.read(is);
    return is;
}

可以编译吗?

Table[i] = *temp;

Table是指向Person的指针数组

temp是指向Person的指针

试图将对象放入保存指针的数组中。解引用*temp会给你一个对象——你需要一个指向对象的指针,所以不要在那里解引用它。我希望编译器会抱怨这个…不是吗?

另外,检查您的第二个if语句-它说(choice == 'S' || choice == 'p'),这可能不是您的意思。如果choice == 'p'…

两个if块都将执行

Person::read()中,您只分配了一个字符作为名称。