Tuesday, September 7, 2010

static data member in c++ | How to use Static Data Member

,
Static data member:-The static is a keyword and are used to retain value of a variable.
When a variable is declared as static it is initialised to zero
.A static function or data element is only recognised inside the scope of the current.
if the local variable is declared with static keyword.it allows the last value of the variable to be retained between continuation calls to that function.
A static data  item is helpful when all the objects of the class should share a common data.
The satic data variable is accessible within the class,but its value remains through the whole program.


class sumnum
{
private:
static int c;
int num;
public:
void input()
{
cout<<"\nEnter number:";

cin>>num;
c=c+num;
}
void sum()
{
cout<<<"the sum"<
}
};
int sumnum:: c=0;
int main()
{
class sumnum a,b,c;
clrscr();
a.input();
b.input();
c.input();
a.sum();
return 0;
}
Read more →

Classes in Cplusplus | How to use Class in C++

,
A class is a group of Objects that have the identical properties,common behavior, and shared relationship.
The entire group of data and code of an object can be built as a user-defined data type using class.
Objects are nothing but variable of type class.
A class is a model and not a true structure of the object
.However it shares the property names or operations with other objects of the class.


#include<iostream.h>
#include<conio.h>
 Class p
{
Private :
Char n [20];
Public :
Float h ;
Float w ;
} ;
Int main ()
{
Clrscr ()
p a;
//a.n =”Sanjay”;// not accessible
a.h = 5.5;
a.w = 38;
cout <<”H : “<
cout<<”W: “<
return 0;
}

OUTPUT

Height : 5.5
Weight : 38

Read more →

How to use functions in Cplusplus

,
Why we are use Function?

Function is a block of task, In simple word we can say that it's block which we contain instruction or procedure. Which is very useful when we need some procedure or instruction block which execute process and less of development time for a software or solution any kind C and C++ program.


Types of Function

1. Parameterize Function
2. Non Parameterize Function
3. Friend Function
4. Virtual Function

Guidelines for using function.
1. Always define prototype
2. When we use friend function use friend keyword
3. In virtual function use keyword virtual

Example of Function in C plus plus


#include<iostream.h>
#include<conio.h>
Int main()
{
Clrscr();
Float sum (int,float);
Int a=20;
Float b=2.5;
Cout <<”sum=”<
Return 0;
}
Float sum (int  x,float  y)
{
Return (x+y);
}

OUTPUT
Sum = 22.5

Read more →