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

From Script | Spoken-Tutorial
Revision as of 12:20, 6 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 data memebers

Members declared as static are the static data members.

The keyword specifies that the member is shared by all instance of the class.

Open stat.cpp Let us see an example.

I have already typed the code on the editor.

I will explain it.

Point the cursor

stat.cpp

Please note that I have saved the file with the name stat.cpp

Let me explain the code now.

Highlight

class statex

{

This is a class named statex.
Highlight

private:

int x;

public:

static int sum;

Here, we have declared x as non-static and sum as static variable.
Highlight

static int sum;

statex()

{

sum++;

x=sum;

}

Function statex is public function.

In this we have incremented sum.

Then value of sum is stored in x.

Highlight

static void stat()

{

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

}

Here we have static function stat.

We print the sum.

Highlight

void number()

{

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

}

};

Then we have number function.

We print the x here.

Then class is closed.

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;

Here o1 are the object of class statex.
statex::stat(); Static function stat is accessed here.

Using the class name and scope resolution operator.

statex o2,o3; We have o2, o3 and o4 objects of class statex.
o2.number();

o3.number();

Now we call the number function using object o1, o2, o3 and o4.
Highlight

return 0;

This is the return statement.
Click on save Now Click on Save.
Let us execute.
Open the terminal

Ctrl, Alt and T keys simultaneously

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

g++ stat.cpp -o st

To execute

Type

./st

To compile the program type

g++ stat.cpp -o st

To execute

Type

./st

Highlight

Output

Here we see,

Result is: 1

Number is: 2

Number is: 3

Highlight

Output

Let me explain the output now:

First the value of sum is 1.

Then sum is incemented now the value is 2.

Like this it is incremented till 3.

On the editor


Let us go back to our program.

Now i will create one more object of class statex as o5.

Type

statex o5;

Type statex o5;

Now click on save and let us execute and see.

On the terminal Come back to the terminal.

Compile and execute as before.

Highlighted

Output

Result is: 2

Number is: 3

Number is: 4

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

Let us come back to the slides.

Slide 7

Summary

In this tutorial, we have seen,

static keyword.

Static data member.

Static function.

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