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
in a class, all members are
private
when you declare an object of a class, do you have direct access to the variables?
no, because they are private
how do we access private variables of a class?
using public return type functions
what is a mutator function?
a function that modifies the state of an object
what is an example of a mutator function?
a void type function
what is an accessor function?
a function that does not modify the state of any object, it only returns objects
what are some examples of accessor functions?
int functions, double functions, and bool functions because they all have a return type
should you write a function that is both a mutator and accessor function?
no, because it is inconvenient to the end user
what is a constructor?
a function with the same name as the class that does not have a return type
what happens when you overload a constructor?
there is more than one constructor in a class, but they take a different number of parameters, different types of parameters, or both
what happens to the default constructor when you create a constructor for a class?
it no longer exists
what is a default constructor?
the constructor provided by VS Code when you create a class
how many parameters does the default constructor take?
zero
is a constructor declared in the public or private section of a class?
public
what is polymorphism?
when the name of the function is the same, but the two functions have different parameters or parameter types
how do you initialize inheritence?
class NameofChildClass : (public, private, or protected) NameofParentClass
what is simple inheritence?
only one level of inheritence
what is multiple inheritence?
when a child class is derived from more than one parent class
can you access a member function of a class without an object?
no
can you access a friend function without an object?
yes
what does & mean in c++?
& is a reference to the object
how many objects can a friend function access the private members of?
more than 1
what member of a class can access private variable without the . operator?
a static member
what is multilevel inheritence?
when the parent class you are deriving from is already derived from a class. The class you have just created has a grandparent class.
if a member/member function is public in the parent class, can it be accessed in the child class?
yes
if a member/member function is protected in the parent class, can it be accessed in the child class?
yes
if a member/member function is private in the parent class, can it be accessed in the child class?
no