Structures, Namespaces, and Classes Flashcards
aggregate construct
allows to manipulate several data items as a single whole
struct Date{
int month;
int day;
int year;
};
structure definition
does a structure have to have a semicolon at the end of its definition
yes
elements of a structure
members or member variables
is a structure definition executable
no
can members of the same structure have the same name?
no
can members of different structures have the same name?
yes
assume myDataType is a struct. what is happening in the line below?
myDataType myVar;
declaration of the variable myVar of type myDataType
formal namespace definition
provide a scope for identifiers (variables, functions, etc.) within their declarative region.
namespaces are used to…
systematize code in logical groups, preventing naming conflict, especially if there are multiple libraries with single names in your code base.
how do we access structure variables outside of a structure?
StructureObjectName.VariableName
by default, the members in a structure are
public
can a structure variable be initialized at declaration?
yes
can you declare a structure object Date that has variables month, day, and year like this: Date object = {10, 31, 2035}?
yes, because you are assigning the variables when you declare the object
if you declare Date object = {10, 31}, and structure Date has three member variables, what will happen?
variable one and two will be set to 10 and 31, while variable three will be set to 0 because no number was passed in
if you declare Date object {10, 31, 2025, 20}, and structure Date has three member variables, what will happen?
an error will occur because there were more numbers passed in than variables that could be assigned
can structure objects be directly compared?
no
how do we see if two structure objects are the same?
compare each variable of one object to the same variable of the second object. If all of the variables are equal, then the objects are equal
what can a member be in a structure?
a basic type (double, int, bool, etc.), or a type of structure
what is the definition of namespace?
a tool that gives a structure identifiers
what is an identifier?
something that gives a variable its type (int, double, and bool are all identifiers)
look at the code below. what is the output?
namespace n1{
int age = 30; }
namespace n2{
int age = 20;}
cout «_space;age «_space;endl;
there will be an error because the program does not know which age to print
look at the code below. what is the output?
namespace n1{
int age = 30; }
namespace n2{
int age = 20;}
cout «_space;n1::age «_space;endl;
the output is 30 because the age variable is specified by namespace n1
why do we use namespace std?
it has a library of many useful things, such as cout, cin, and string