Programming II Course 7 - Static PDF 2024
Document Details
Uploaded by GenialAntigorite2219
Transilvania University of Brașov
2024
Bianca Iacob
Tags
Summary
This document is a lecture on static variables in C++. It covers various features, examples, and use cases of static elements. The document is from the Transilvania University of Brasov, 2024.
Full Transcript
Programming II Course 7 - static Bianca Iacob Transilvania University of Brasov 2024 Bianca Iacob Transilvania University of Brasov 2024 1 / 34 Contents 1 static 2 Singleton Pat...
Programming II Course 7 - static Bianca Iacob Transilvania University of Brasov 2024 Bianca Iacob Transilvania University of Brasov 2024 1 / 34 Contents 1 static 2 Singleton Pattern Bianca Iacob Transilvania University of Brasov 2024 2 / 34 static static Bianca Iacob Transilvania University of Brasov 2024 3 / 34 static static The static keyword in C++ has several uses but primarily controls the lifespan and visibility of variables and functions. It allows variables to retain values between function calls, restricts visibility at file or class level, and enables shared members within classes. Bianca Iacob Transilvania University of Brasov 2024 4 / 34 static Static elements are allocated storage space only once during the lifetime of the program in the static storage area. The lifetime of a static variable is given by the lifetime of the program. Bianca Iacob Transilvania University of Brasov 2024 5 / 34 static We can have: Static variables Static member variables inside a class Static methods belonging to a class Bianca Iacob Transilvania University of Brasov 2024 6 / 34 static Static variables Initialized only once. The value is retained through function calls. Bianca Iacob Transilvania University of Brasov 2024 7 / 34 static Example static variable void counter() { static int count = 0; cout