gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:51:04 26 4
gpt4 key购买 nike

<分区>

我正在尝试在这里布置一项作业,但卡在了一点上。问题是用 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;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com