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

标签 c++ arrays

<分区>

我正在尝试在这里布置一项作业,但卡在了一点上。问题是用 Student 派生类创建 Person 类。然后重载 << 和 >> 运算符。最后创建检查程序以创建 20 人的数组并继续加载 Person 或 Student。在任何时候我们都可以打印我们目前所拥有的 - Person 输出是 Name char*/Age int/Parents *char[2],学生输出为 Name char*/Age int/ID int

我的问题是数组点 - 我不知道如何实现它,现在我被困住了:

  • 指向人的指针数组
  • 我们选择它的人/学生
  • 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;
}

我能够加载第一人称/学生,然后代码中断而不会出错。 所以我想问的是,您能否查看代码并为我指明正确的方向?

免责声明:我们必须使用数组,不能使用 vector 等。是的,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 block 都会执行

并且在 Person::read() 中,您只为名称分配了一个字符。那很可能结局很糟糕......

关于包含基类/派生类对象的 C++ 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960915/

相关文章:

c++ - 初始化nullptr_t时,0与其他整数如何区分?

java - 访问循环中创建的各个 JTextField 的值

javascript - 将对象转换为数组 - 维护命名键

c++ - 方法调用后字节大小不正确

c++ - 关于非重复 rand 函数

c++ - 使用 STL 的图形(列表 vector ,即邻接列表)- C++

c++ - OpenMP 未使用 Raspberry Pi 2 上的所有可用内核

Visual Studio Express 2012 中的 C++ 程序不能使用 .csv 但可以使用 .txt

c++ - 是否可以将自定义小部件添加到 QListView 中?

JavaScript 从虚线版本数组中找到最高版本