Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Static Components

Automatic Variables vs. Static Variables in C++


Automatic Variables
• Created and destroyed #include <iostream>
using namespace std;
automatically during program
execution void fun()
• Repeatedly created and {
int var = 99;
destroyed as needed cout << "var = " << ++var << endl;
• Example code showcasing }
automatic variables:
int main()
{
for(int i = 0; i < 5; i++)
fun();
}
Behavior of Automatic Variables
• Variable var is created and destroyed within the fun function
• Output will always be:
var = 100
var = 100
var = 100
var = 100
var = 100
Static Variables
• Exist continuously throughout #include <iostream>
using namespace std;
the program execution
• Declared using the static void fun()
keyword {
static int var = 99;
• Example code showcasing static cout << "var = " << ++var << endl;
variables: }

int main()
{
for(int i = 0; i < 5; i++)
fun();
}
Behavior of Static Variables
• Variable var is created and initialized once during program
initialization
• Preserves its value between subsequent function invocations
• Output will be:
var = 100
var = 101
var = 102
var = 103
var = 104
Class Instances
• Each object created from a class #include <iostream>
using namespace std;
is an instance of that class
class Class {
• Instances encapsulate all public:
components (fields and int val;
void print()
functions) of the class {
cout << val << endl;
• Attempting to access non- }
existent components outside the };

instance leads to compilation int main()


errors {
Class::val = 0;
• Example code with a compilation Class::print();

error: }
Static Components
• Static components exist #include <iostream>
using namespace std;

throughout program execution class Class {


public:

• Shared among all instances of static int Static;


int NonStatic;

the class
void print()
{
cout << "Static = " << ++Static << ", NonStatic = " << NonStatic << endl;

• Example code showcasing static };


}

components: int Class::Static = 0;

• Output will be: int main()


{
Class instance1, instance2;
• Static = 1, NonStatic = 10
instance1.NonStatic = 10;
• Static = 2, NonStatic = 20 instance2.NonStatic = 20;
instance1.print();
instance2.print();
}
#include <iostream>
using namespace std;
class Class {

Static Class Variables


public:
static int Counter;
Class()
{
++Counter;
• Can be used as counters of class };
~Class()
instances {
--Counter;
• Incremented in constructor, if(Counter == 0)
cout << "Bye, bye!" << endl;
decremented in destructor };
void HowMany()
• Example code showcasing static {
cout << Counter << " instances" << endl;
class variables: }
};
• Output will be int Class::Counter = 0;
int main()
2 instances so far {
4 instances Class a;
Class b;
Bye, bye! cout << Class::Counter << " instances so far" << endl;
Class c;
Class d;
d.HowMany();
#include <iostream>
using namespace std;
class Class {

Static Class Variables


static int Counter;
public:
Class() {
++Counter;
};

• variable private and static at the ~Class()


{

same time --Counter;


if(Counter == 0)

• Output will be: };


cout << "Bye, bye!" << endl;

2 instances void HowMany()


{
4 instances cout << Counter << " instances" << endl;
}
Bye, bye! };
int Class::Counter = 0;
int main()
{
Class a;
Class b;
b.HowMany();
Class c;
Class d;
d.HowMany();
}
#include <iostream>
using namespace std;

Static Class Functions


class Class {
static int Counter;
public:
Class()
{
++Counter;
• Behave similarly to static variables };
~Class()

• Can be invoked without creating {


--Counter;
class instances if(Counter == 0)
cout << "Bye, bye!" << endl;

• Example code showcasing static };


static void HowMany()

class functions: {
cout << Counter << " instances" << endl;

• Output };
}

0 instances int Class::Counter = 0;


int main()

2 instances {
Class::HowMany();
4 instances Class a;
Class b;
Bye, bye! b.HowMany();
Class c;
Class d;
d.HowMany();
}
Static vs. non-static components
• To analyze the interactions between static
and non-static components, we'll consider
four scenarios:

• Static component accessing a static


component
• Static component accessing a non-static
component
• Non-static component accessing a static
component
• Non-static component accessing a non-static
component
• Let's dive into each scenario and understand
the implications
Static → Static Interaction
#include <iostream> • Output:
using namespace std;
static
class Test { Static
public:
static void funS1() {
cout << "static" << endl; • Both static functions
}
static void funS2() {
can access each
funS1(); other successfully.
}
};
• This interaction is
possible since static
int main() { functions exist
Test object;
Test::funS2();
throughout the
object.funS2(); program's execution.
}
Static → Non-Static Interaction
#include <iostream>
using namespace std;
• This interaction is not possible
because a static function cannot
class Test {
public: directly access a non-static
void funN1() {
cout << "non-static" << endl;
function.
}
static void funS1() {
• The non-static function funN1 is
funN1(); // Compilation Error associated with an object and
};
}
cannot be accessed without an
object instance.
int main() {
Test object;
Test::funS1();
object.funS1();
}
Non-Static → Static Interaction
#include <iostream>
using namespace std;
• Output:
• static
class Test {
public: • A non-static function can
static void funS1() {
cout << "static" << endl;
successfully invoke a static
} function.
void funN1() {
funS1(); • This interaction is possible
};
} because static functions exist
before any object instances are
int main() { created.
Test object;
object.funN1();
}
Non-Static → Non-Static Interaction
• This interaction is
straightforward and always
possible.
• Non-static functions can invoke
other non-static functions within
the same class without any
restrictions.

You might also like