📖 Scenario: You are creating a simple program to store information about a book and its author. The author has a name and an age. The book has a title, a year of publication, and an author.
🎯 Goal: Build a program that uses nested structures to store and display information about a book and its author.
📋 What You'll Learn
Create a structure called Author with two members: name (string) and age (int).
Create a structure called Book with three members: title (string), year (int), and author (of type Author).
Create a variable myBook of type Book and assign the exact values: title = "The Great Adventure", year = 2020, author name = "John Smith", author age = 45.
Print the book's title, year, author's name, and author's age exactly as shown in the final output.
💡 Why This Matters
🌍 Real World
Nested structures help organize related data clearly, like storing book and author information together.
💼 Career
Understanding nested structures is important for managing complex data in software development, such as databases, file formats, and APIs.
Progress0 / 4 steps
1
Create the nested structures
Define a structure called Author with members name (string) and age (int). Then define a structure called Book with members title (string), year (int), and author (of type Author).
C++
Hint
Use struct keyword to define each structure. The Book structure should include an Author type member.
2
Create a variable with exact values
Inside main(), create a variable called myBook of type Book. Assign title to "The Great Adventure", year to 2020, author.name to "John Smith", and author.age to 45.
C++
Hint
Use dot notation to assign values to nested members like myBook.author.name.
3
Print the book and author details
Use cout to print the book's title, year, author's name, and author's age in this exact format: Title: The Great Adventure Year: 2020 Author: John Smith Author Age: 45
C++
Hint
Use cout with << to print each line exactly as shown.
4
Display the final output
Run the program and ensure it prints exactly: Title: The Great Adventure Year: 2020 Author: John Smith Author Age: 45
C++
Hint
Make sure your output matches exactly, including spaces and line breaks.
Practice
(1/5)
1. What is the main purpose of using nested structures in C++?
easy
A. To allow structures to inherit functions
B. To create multiple inheritance between classes
C. To increase the size of a structure unnecessarily
D. To group related data logically inside another structure
Solution
Step 1: Understand nested structures concept
Nested structures let you put one structure inside another to group related data logically.
Step 2: Compare options
Only To group related data logically inside another structure correctly describes grouping data logically. Other options describe unrelated concepts.
Final Answer:
To group related data logically inside another structure -> Option D
Quick Check:
Nested structures = group data logically [OK]
Hint: Think of nested structures as folders inside folders [OK]