CPP_Basics Flashcards

1
Q

what are static member variables in C++?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Are static members associated with class objects ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to define static members?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are static member definitions subject to access control?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is static const integral type? when it should be initialised

A

Static const integral type is a element which is shared among all objects and whose value cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is static member functions of a class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Does static member functions have a this pointer?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can static members functions access non static members?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Public member functions can access private members of the same object as well as other objects

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is constructor?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what are the special rules for constructors?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is default constructor?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how asserts work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

in constructors if you have default intialisation and member initialisation which will take precedence?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are destructors?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

when are destructors called?

A

When an object goes out of scope normally, or a dynamically allocated object is explicitly deleted using the delete keyword, the class destructor is automatically called (if it exists) to do any necessary clean up before the object is removed from memory. For simple classes (those that just initialize the values of normal member variables), a destructor is not needed because C++ will automatically clean up the memory for you

However, if your class object is holding any resources (e.g. dynamic memory, or a file or database handle), or if you need to do any kind of maintenance before the object is destroyed, the destructor is the perfect place to do so, as it is typically the last thing to happen before the object is destroyed

17
Q

what are the rules for naming destructors?

A

Destructor naming

Like constructors, destructors have specific naming rules:
1) The destructor must have the same name as the class, preceded by a tilde (~).
2) The destructor can not take
arguments.
3) The destructor has no return type

Note that rule 2 implies that only one destructor may exist per class, as there is no way to overload destructors since they can not be differentiated from each other based on arguments.

Generally you should not call a destructor explicitly (as it will be called automatically when the object is destroyed), since there are rarely cases where you’d want to clean up an object more than once. However, destructors may safely call other member functions since the object isn’t destroyed until after the destructor executes.

18
Q

Instantiating a const class object

A

instantiated class objects can also be made const by using the const keyword. Initialization is done via class constructors

const Date date1; // initialize using default constructor
const Date date2(2020, 10, 16); // initialize using parameterized constructor
const Date date3 { 2020, 10, 16 }; // initialize using parameterized constructor (C++11)

Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the const-ness of the object. This includes both changing member variables directly (if they are public), or calling member functions that set the value of member variables

19
Q

what is const member function?

A

A const member function is a member function that guarantees it will not modify the object or call any non-const member functions (as they may modify the object).

To make getValue() a const member function, we simply append the const keyword to the function prototype, after the parameter list, but before the function body:

20
Q

can we have a const constructors?

A

Note that constructors cannot be marked as const. This is because constructors need to be able to initialize their member variables, and a const constructor would not be able to do so. Consequently, the language disallows const constructors

21
Q

Overloading const and non-const function

A
Finally, although it is not done very often, it is possible to overload a function in such a way to have a const and non-const version of the same function:
#include 

class Something
{
private:
std::string m_value;

public:
Something(const std::string &value=””) { m_value= value; }

const std::string&amp; getValue() const { return m_value; } // getValue() for const objects
std::string&amp; getValue() { return m_value; } // getValue() for non-const objects }; The const version of the function will be called on any const objects, and the non-const version will be called on any non-const objects:
int main()
{
	Something something;
	something.getValue() = "Hi"; // calls non-const getValue();
const Something something2;
something2.getValue(); // calls const getValue();

return 0; }
22
Q

whats the difference between struct and class in C++

A
Now that we’ve talked about access specifiers, we can talk about the actual differences between a class and a struct in C++. A class defaults its members to private. A struct defaults its members to public.
That’s it!
(Okay, to be pedantic, there’s one more minor difference -- structs inherit from other classes publicly and classes inherit privately. We’ll cover what this means in a future chapter, but this particular point is practically irrelevant since you should never rely on the defaults anyway).
23
Q

what is a constructor?

A
Aconstructoris 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).
Unlike normal member functions, constructors have specific rules for how they must be named:
	1. Constructors must have the same name as the class (with the same capitalization)
Constructors have no return type (not even void)