0
0
C++programming~5 mins

Defining structures in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a structure in C++?
A structure in C++ is a user-defined data type that groups related variables of different types under one name. It helps organize data like a real-life object with multiple properties.
Click to reveal answer
beginner
How do you define a simple structure named Person with name and age?
You define it like this:<br>
struct Person {<br>  std::string name;<br>  int age;<br>};
Click to reveal answer
beginner
How do you create a variable of a structure type and access its members?
First, declare a variable of the structure type, then use the dot . operator to access members.<br>Example:<br>
Person p;<br>p.name = "Alice";<br>p.age = 30;
Click to reveal answer
intermediate
Can structures in C++ have functions inside them?
Yes! Structures in C++ can have functions (called member functions) just like classes. This lets you group data and behavior together.
Click to reveal answer
intermediate
What is the difference between a structure and a class in C++?
The main difference is default access:<br>- In a structure, members are public by default.<br>- In a class, members are private by default.<br>Otherwise, they are very similar.
Click to reveal answer
How do you define a structure in C++?
AUsing the keyword <code>class</code> only
BUsing the keyword <code>function</code>
CUsing the keyword <code>define</code>
DUsing the keyword <code>struct</code> followed by the structure name and members
What operator is used to access members of a structure variable?
AColon (:) operator
BDot (.) operator
CComma (,) operator
DArrow (->) operator
What is the default access level of members in a C++ structure?
APublic
BProtected
CPrivate
DPackage
Can a structure in C++ contain functions?
AOnly if declared as <code>class</code>
BNo, structures can only have variables
CYes, structures can have member functions
DOnly in C, not in C++
Which of the following is a correct way to declare a structure variable?
ABoth A and B
Bstruct Person p;
CPerson p;
DNone of the above
Explain how to define a structure in C++ and create a variable of that structure.
Think about grouping related data under one name and how to use it.
You got /5 concepts.
    Describe the differences and similarities between structures and classes in C++.
    Focus on access and behavior.
    You got /4 concepts.