Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Add details on class member object

+2 votes
asked May 2, 2021 by Siti Nursehah (140 points)
I would like to ask how to add details on class member object with array.

1 Answer

0 votes
answered May 5, 2021 by Peter Minarik (86,240 points)
  • What programming language are we talking about here?
  • What do you mean by "detail on class member object with array"
  • Can you share some code (even if it doesn't work) so we can have a better understand if what you're trying to achieve?
If you're trying to add a member to a class that has a type of an array of another class, then you could do something like this:
#include <iostream>

class Detail
{
public:
    std::string text;
};

class Person
{
public:
    static const size_t detailSize = 5;
    Detail details[detailSize];
};

int main()
{
    Person person;
    person.details[0] = Detail { "Name" };
    person.details[1] = Detail { "Age" };
    person.details[2] = Detail { "Date of birth" };
    person.details[3] = Detail { "Place of birth" };
    person.details[4] = Detail { "Address" };
    
    for (int i = 0; i < Person::detailSize; i++)
        std::cout << "Detail [" << i << "] : " << person.details[i].text << std::endl;
}
But using std::vector<Detail> would be a better choice than a fixed-size array for many scenarios.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...