Object relationship

18.4 - Struct


A struct is an aggregate data type, which means you can combine multiple related data types into one struct. For example, you might have a struct called Date which stores the day, the month, and the year information. The struct might look like this:

struct Date
{
    unsigned int day;
    unsigned int month;
    unsigned int year;
};

Note that just like a class, a struct definition ends with a semicolon (;). Forgetting it spells disaster for you.

Structs can actually store member functions just like class. But generally structs are used only for storing data. The only difference between a struct and a class is that the former's members are public by default while the latter's members are private by default.

Your task is to create a struct called Restaurant which has the following members:

  • name (string)
  • year_opened (unsigned int)
  • rating (unsigned int)

Sample input code

int main()
{
    Restaurant kfc {"KFC", 1952, 11};

    std::cout << "Name:\t" << kfc.name << std::endl;
    std::cout << "Year opened:\t" << kfc.year_opened << std::endl;
    std::cout << "Rating:\t" << kfc.rating << "/10" << std::endl;
}

Sample output

Name:   KFC
Year opened:    1952
Rating: 11/10
#include <iostream> // Define your Restaurant struct here!