Lecture 7: Statics Flashcards

1
Q

Static variables

A

Lifetime: point of initalization til the end of the program

Declared and initialized only once

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

If static var is not initialized explicitly, it is initialized to

A

0 (of that data type)

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

Difference between const and static variables

A

Constant variables –> retain same value throughout the scope
Lifetime: initialization til end of scope
Access: initialization til end of scope
Static variables –> retain same memory location throughout the entire program
Lifetime: initialization til end of program
Access: only in the scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Static data member/
static class member
A
Part of class, but not part of any individual object
	Shared by all instances of the class, instead of belonging to a single one
	Declared inside class
	Initialized outside class
	Created even no object of the class is created
	Remains in memory even after all objects of the respective class are deleted
Used to store information that is required by all objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can a private static member be accessed outside the class?

A

Yes, but only when it is being initialized

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

Methods of accessing a static data member or function

A
  1. Like a normal data member or function(through any created object)
  2. Using scope resolution operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Static member function

A
a member function of a class that does not require being called by any individual object
can only access static data members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why can a static member function only access static data members?

A

This pointer is not implicitly passed to static member functions –> cannot access the data members of whatever object we are trying to call them through
If pointer of the object is passed explicitly, then function can access data members

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

Can regular member functions access static data members?

A

Yes

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

Alternative to using static data members

A

Global variables.

Accessible to all, so go against data encapsulation

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