首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >包含基类/派生类对象的C++数组

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

Stack Overflow用户
提问于 2014-05-30 18:12:15
回答 1查看 122关注 0票数 0

我正试着在这里安排一个任务,却被困在了一个点上。问题是用学生派生类创建Person类。然后重载<<和>>运算符。最后,创建检查程序,创建由20个人组成的数组,并继续加载任何人或学生。在任何时候,我们都可以打印我们所拥有的--到目前为止,人的输出是Name char*/Age int/Parents *char[2],,学生输出是Name char*/Age int/ID int.

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

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

以下是主要代码部分:

代码语言:javascript
运行
复制
#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也在那里,它必须保持……显然我是初学者。

人:

代码语言:javascript
运行
复制
#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;
};

班级

代码语言:javascript
运行
复制
#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;
}
EN

Stack Overflow用户

回答已采纳

发布于 2014-05-30 18:24:37

这是编译吗?

代码语言:javascript
运行
复制
Table[i] = *temp;

Table是指向Person的指针数组。

temp是指向Person的指针

您正在尝试将一个对象放入一个包含指针的数组中。解除引用*temp为您提供了一个对象--您需要一个指向对象的指针,所以不要在那里取消引用。我希望编译器会抱怨.是吗?

另外,检查您的第二条if语句--它写的是(choice == 'S' || choice == 'p'),这可能不是您的意思。如果选择if‘p’.

Person::read()中,您只为name分配了一个字符。结果可能会很糟糕..。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23960915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档