CPP_Basics Flashcards
what are static member variables in C++?
Member variables of a class can be made static by using the static keyword. Unlike normal member variables, static member variables are shared by all objects of the class
class Something { public: static int s_value; };
int Something::s_value = 1;
int main()
{
Something first;
Something second;
first.s_value = 2; std::cout << first.s_value << '\n'; std::cout << second.s_value << '\n'; return 0; }
This program produces the following output:
2
2
Because s_value is a static member variable, s_value is shared between all objects of the class. Consequently, first.s_value is the same variable as second.s_value. The above program shows that the value we set using first can be accessed using second!
Are static members associated with class objects ?
No! static members are not associated with class objects
Static members exists even if no objects of the class have been instantiated
Much like global variables, they are created when the program starts, and destroyed when the program ends
Consequently, it is better to think of static members as belonging to the class itself, not to the objects of the class. Because s_value exists independently of any class objects, it can be accessed directly using the class name and the scope resolution operator (in this case, Something::s_value)
How to define static members?
When we declare a static member variable inside a class, we’re telling the compiler about the existence of a static member variable, but not actually defining it (much like a forward declaration). Because static member variables are not part of the individual class objects (they are treated similarly to global variables, and get initialized when the program starts), you must explicitly define the static member outside of the class, in the global scope
Are static member definitions subject to access control?
Note that this static member definition is not subject to access controls: you can define and initialize the value even if it’s declared as private (or protected) in the class
what is static const integral type? when it should be initialised
Static const integral type is a element which is shared among all objects and whose value cannot be changed.
what is static member functions of a class?
static member functions are not attached to any particular object
Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope resolution operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended.
Does static member functions have a this pointer?
No Static member functions do not have a this pointer.
irst, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it – the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.
can static members functions access non static members?
static member functions can directly access other static members (variables or functions), but not non-static members. This is because non-static members must belong to a class object, and static member functions have no class object to work with
Public member functions can access private members of the same object as well as other objects
One nuance of C++ that is often missed or misunderstood is that access control works on a per-class basis, not a per-object basis. This means that when a function has access to the private members of a class, it can access the private members of any object of that class type that it can see.
In the above example, copyFrom() is a member of DateClass, which gives it access to the private members of DateClass. This means copyFrom() can not only directly access the private members of the implicit object it is operating on (copy), it also means it has direct access to the private members of DateClass parameter d! If parameter d were some other type, this would not be the case
what is constructor?
A constructor is a special kind of class member function that is automatically called when an object of that class is instantiated. Constructors are typically used to initialize member variables of the class to appropriate default or user-provided values, or to do any setup steps necessary for the class to be used (e.g. open a file or database).
what are the special rules for constructors?
Unlike normal member functions, constructors have specific rules for how they must be named:
Constructors must have the same name as the class (with the same capitalization)
Constructors have no return type (not even void)
what is default constructor?
A constructor that takes no parameters (or has parameters that all have default values) is called a default constructor. The default constructor is called if no user-provided initialization values are provided
how asserts work?
An assert statement is a preprocessor macro that evaluates a conditional expression at runtime. If the conditional expression is true, the assert statement does nothing. If the conditional expression evaluates to false, an error message is displayed and the program is terminated.
in constructors if you have default intialisation and member initialisation which will take precedence?
if a default initialization value is provided and the constructor initializes the member via the member initializer list, the member initializer list will take precedence
what are destructors?
A destructor is another special kind of class member function that is executed when an object of that class is destroyed. Whereas constructors are designed to initialize a class, destructors are designed to help clean up