Advanced-C++/C2/Static-Members/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Static in C++

Author: Ashwini Patil

Keywords: static, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on static members in C++.
Slide 2


In this tutorial we will learn,

Static keyowrd.

Static data memeber.

Static member functions.

We will do this with the help of examples.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.04

gcc and g++ compiler v. 4.6.1

Slide 4 Let us start with the introduction to static.

Static means fixed, which cannot change.

The word justifies its meaning.

If a variable is defined as static, it means that it is initialized.

It will remain in the memory till the end of the program.

Static variables are initialized to zero before the first object is created.

Slide 5 Static functions

A static function may be called by itself without depending on any object.

To access a static function we use,

classname :: staticfunction();

Open stat.cpp Let us see an example on static memebers.

I have already typed the code on the editor.



Point the cursor

stat.cpp

Note that I have saved the file with the name static.cpp

Let me explain the code now.

Highlight

#include <iostream>

This is our headerfile as iostream
Highlight

using namespace std;

Here we are using the std namespace
Highlight

class statex

{

Then we have class statex.
Highlight

private:

int x;

public:

static int sum;

In this we have a non-static variable as x declared as private.

Then we have a static variable sum declared as public.

Highlight

static int sum;

statex()

{

sum++;

x=sum;

}

This is our constructor statex.

In this we have incremented sum.

Then the value of sum is stored in x.

Highlight

static void stat()

{

cout << "Result is: " << sum<<"\n";

}

Here we have a static function stat.

In this we print the sum.

Highlight

void number()

{

cout << "Number is: " << x<<"\n";

}

};

Then we have function number.

Here we print the number x

The class is closed here.

Highlight

int statex::sum=0;


To declare the static variable globally we use scope resolution operator.

To access a static variable we write as:

datatype classname::static var name.

Now the storage is allocated to variable sum.

It is initialized to 0.

Highlight

int main()

This is our main function.
Highlight

statex o1, o2, o3;

Here we create objects of class statex.

As o1, o2 and o3.

Highlight

o1.number();

o2.number();

o3.number();

Then we call the function number using o1, o2 and o3.



Highlight

statex::stat();


Static function stat is accessed here.

Using the class name and scope resolution operator.

Highlight

cout<< “Now static var sum is ” <<o1.sum<< “\n”;

Here we print the static variable sum.
Highlight

return 0;

This is the return statement.
Let us execute the program.
Open the terminal

Ctrl, Alt and T keys simultaneously

Please open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

g++ static.cpp -o stat

To execute

Type

./stat

To compile the program type

g++ static.cpp -o stat

To execute

Type

./stat

Highlight

Output

Here we see,

Number is: 0

Number is: 1

Number is: 2

Number is: 3

Now static var sum is 3

Highlight

Output

Now I will explain the output:

Let me resize the window.

On the editor and terminal First the value of number is 0 ie x is 0.

The first object gives the value as 0

Then we have the value as 1 ie x =1

The second object gives the value as 1

And the third object gives the value as 2

Then we call the stat function which gives the value of sum.

Result is sum.

Here sum is incemented and stored in x.

Hence this will gived the value as 3.

Thus the final output we have is.

Static var sum is 3.

Edit

statex 01, 02, 03, 04;

Now let us create another object here as o4.
On the editor


I will call the function number usign the object o4.

Click on Save

On the terminal Let us execute.

Press the arrow key twice.

Again Press the arrow key twice.

Highlighted

Output

You can see that result is 4.

Result is 4

Now static var sum is 4

As the 4th object is created.

Switch back to the slides This brings us to the end of this tutorial.

Come back to the slides.

Slide 7

Summary

Let us summarize:

In this tutorial, we have seen,

static keyword.

Static variable

eg. static int sum;

Static function.

eg. static void stat()

Slide 6

Assignment

As an assignment

Create a class that declares a static variable.

Decrement the variable.

And print the value.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to,

contact@spoken-tutorial.org

Slide Number 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of Indiaabout:startpage

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini