Shared Flashcard Set

Details

C++ | Static
Making you as confused as me
12
Computer Science
Undergraduate 3
12/11/2024

Additional Computer Science Flashcards

 


 

Cards

Term
class Object {
public:
static int counter;
};

int main() {
cout << Object::counter;
}

 

Definition

ERROR

static variables need to be initialized, outside of the class definition

 

Term
class Object {
public:
static int counter = 0;
};

int main() {
cout << Object::counter;
}

 

Definition

ERROR

static variables NEED to be defined OUTSIDE class definition

Term
class Object {
public:
static int counter = 0;
};

Object::counter = 0;

int main() {
cout << Object::counter;
}

 

Definition

ERROR,

need to state data type

Term
class Object {
public:
static int counter = 0;
};

int Object.counter = 0;

int main() {
cout << Object::counter;
}

 

Definition

ERROR,

need to use :: scope

Term
class Object {
public:
static int counter;
};

int Object::counter = 0;

int main() {
cout << Object::counter;
}

 

Definition

0

WORKS!!!

Term
class Object {
public:
static int counter;
}P0;

int Object::counter = 0;

int main() {
cout << P0::counter;
}

 

Definition

ERROR, instances need to use .counter

Term
class Object {
public:
static int counter;
}P0;

int Object::counter = 0;

int main() {
cout << P0.counter;
}

 

Definition

0

WORKS!!!

 

 

Term
class Object {
public:
static int counter;
Object() {counter++;}
};

int Object::counter = 0;

int main() {
Object obj, obj2, obj3;
cout << obj.counter << endl;
obj.counter++;
cout << obj2.counter << endl;
Object::counter++;
cout << obj3.counter << endl;
}

 

Definition

3

4

5

Term
class Object {
public:
static int counter;
int id;
Object() {id = counter++;}
};

int Object::counter = 0;

int main() {
Object obj, obj2, obj3;
cout << obj.id << endl;
obj.id++;
cout << obj2.id << endl;
Object::counter++;
cout << obj3.id << endl;
}

 

Definition

0

1

2

Term
class Object {
public:
static int counter;
int id;
Object() {id = counter++;}
};

int Object::counter = 0;

int main() {

cout << Object::counter << endl;
Object objs[100];
cout << Object::counter << endl;
}

 

Definition

0

100

Term
class Object {
public:
static int counter;
int id;
};

int main() {
Object obj;
}

 

Definition

Compiles

counter wasnt referenced yet so no issues in runtime. counter needs to be initialized.

Term
class Object {
public:
static int counter;
int id;
Object() {
id = counter++;
}
};

int Object::counter = 0;

class Subject {
public:
static Object obj;
};


int main() {
Subject s1;
s1.obj.id = 1;
}

 

Definition

ERROR

Subject::obj wasnt initialized

 

Object Subject::obj;

 

Supporting users have an ad free experience!