0

Как я могу создать массив структур внутри которой будут массивы других структур? Например: есть class students , мне нужна новая stuct theclass внутри которой будет одним из параметров передоваться массив с учениками. Те структура theclass у него есть буква и массив со студентами. Или как можно по-другому реализовать? (скорее всего я максимально непонятно изложил идею, все равно спасибо) '

#include <iostream>
#include <string>


using namespace std;

class Human
{
public:
    string name;
    string surname;
    char sex;
    int day;
    int month;
    int year;
    Human(string name, string surname, char sex, int day, int month, int year )
    {
        this -> name = name;
        this -> surname = surname;
        this -> sex = sex;
        this -> day = day;
        this -> month = month;
        this -> year = year;
    };
};

class Student: public Human
{
public:
    string grade;
    Student (string name, string surname, char sex, int day, int month, int year, string grade) : Human(name, surname, sex, day, month, year)
    {
        this -> grade = grade;

    };
};

struct TheClass
{
    Student students[10];
    TheClass (Student students[10])
    {
        this-> Student students[10]= students[10];
    };

};

'

1
  • как реализовать что? Цель вашу опишите. Наверняка вы начали писать какой то код, и появились затруднения. Покажите кусок кода, а то действительно не очень понятно в чем сложность 23 дек 2019 в 7:02

1 ответ 1

1

Вот пример с массивами трех разных видов.

#include<iostream>
#include<string>
#include <vector>

using namespace std;

class A
{
private:
    string name;
    int age;

public:

    void getName()
    {
        cout << "Name is : " << name << "\n";
    }

    void getAge()
    {
        cout << "Age is : " << age << "\n";
    }

    void setName(string _name)
    {
        name = _name;
    }

    void setAge(int _age)
    {
        age = _age;
    }

};

class B
{
public:
    A students[4];
};


class BB
{
private:
    A *students;
public:
    BB(int count) {
        students = new A[count];
    }
};

class BBB
{
private:
    vector<A> students;
public:
    void add(string _name, int _age) {
        A temp;
        temp.setAge(_age);
        temp.setName(_name);
        students.push_back(temp);
    }
};



int main() {

    B test;
    test.students[0].setAge(20);
    test.students[0].setName("Boris");
    test.students[0].getName();
    test.students[0].getAge();

    BB t(4);
    BBB t2;
    t2.add("Boris2",10);

    return 0;
}

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.