Showing posts with label Cplusplus Programming Concepts. Show all posts
Showing posts with label Cplusplus Programming Concepts. Show all posts

Friday, 21 February 2014

Static Data Member and Static Member Functions




STATIC DATA MEMBER

As we know Each object of a particular specified class consist of its own data . But if we declared Data item is as static data item then this static data item or we can simply say static variable or static member function will be available for global access for all objects of class . It means if you have declared a variable for member function as static then this can be accessed by all objects of class .

I mean to say if you have declared a variable as static then you have assigned it value '5'  through Obj1 object of class . Then if you have created a new object Obj2 and accessing value of static variable that it is still holding value '5' if it is accessed through Obj2 .


A Static Variable is visible or accessible only within the class or only by members of class But its lifetime is throughout the program means it can hold the same value throughout the lifetime of program


DECLARATION :-

                                  Static int stvar;

INITIALIZATION :- 

                
*                                     int abc : : stvar =10

        In this Stvar is initialized with value '10' and abc is class name . 

*              int abc : :  : stvar ;
In this abc is classname and now stvar will be assigned '0' value



SIMPLE PROGRAM TO SHOW USE OF STATIC VARIABLE


#include<iostream.h>
#include<conio.h>

class static_var
{

static int count;    //static member of class
public :


void incr_staticvar()
{
count++;
}

 void outputc()
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function : : count;

void main()
{
clrscr();
static_var obj1,obj2,obj3,obj4;

obj1.incr_staticvar();
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();

cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";

obj1.outputc ( );
obj2.outputc ( );
obj3.outputc ( );
obj4.outputc ( );


      getch();
}

OUTPUT :-




STATIC MEMBER FUNCTION



The Word Static means fixed . To  make a member function static you have to precede the word static to it .

Facts About Static Member Functions :-

*  The main usage of static functions is they are made to access static members of class or static variables of class that are declared in same class in which static member functions are declared

* These can be called outside class or in main function without using Object of class.



HOW TO CALL STATIC MEMBER FUNCTION


CLASS_NAME  : :  NAME_OF_FUNCTION

SIMPLE PROGRAM TO SHOW USE OF STATIC MEMBER FUNCTIONS


#include< iostream.h >
#include< conio.h >

class static_function
{

int a;
static int count;    //Declaration of static Data member of class 

public :

static_function( )    // Constructor that is initializing variable a to 0
{
a=0;
}

void non_static( )    // public member function that is incrementing a variable
{
a++;
}

void outputn ( )  // public member function to cout value of a
{
cout<<"A :-  "<<a<<endl;
}

void incr_staticvar( )   // public member function to increment static variable
{
count++;
}

static void outputc( )  // public member function to cout value of static variable
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function :: count;

void main( )
{
clrscr();
static_function obj1,obj2,obj3,obj4;   //creation of four object of class static_function

cout<<"Initially ";
static_function : : outputc();     // calling static function

obj1.incr_staticvar();    //calling public funtion to increment static variable
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();



cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";
static_function ::outputc();

obj1.non_static();    // function to increment a variable
obj2.non_static();
obj3.non_static();
obj4.non_static();

cout<<cout<<"After Increment of Variable 'A' by Four Different objects i :-";
obj1.outputn();
obj2.outputn();
obj3.outputn();
obj4.outputn();

      getch();

}

OUTPUT :-



Tuesday, 20 August 2013

Data Types in C++


C++ Supports a variety of Data types and programmer can select the appropriate type as per the need of application. Data Type is generally tells or specifies the type of data that we are going to store in the variable or Type of value a function will return when called at RunTime.

Wednesday, 19 June 2013

Introduction To C++ Programming Language

C++ is object oriented programming language . But in this we can do Structured Programming also. Initially named "C with Classes" . C++ was developed By Bjarne Stroustrup at Bell-Laboratories in 1980s . Stroustrup take the best feature of simula 67 and C, and designed a language which support object oriented programming feature.

Tuesday, 18 June 2013

Difference Between C programming and C++ programming Language

DIFFERENCE BETWEEN PROCEDURAL AND OBJECT ORIENTED PROGRAMMING

1.         Follows Procedural Programming Approach . It is also called 
Structured programming approach. In Structured programming approach 
the program has well defined structure

Thursday, 11 April 2013

Monday, 17 December 2012