Already have an account? Get multiple benefits of using own account!
Login in your account..!
Remember me
Don't have an account? Create your account in less than a minutes,
Forgot password? how can I recover my password now!
Enter right registered email to receive password!
Static Data Members:
A data member inside the class can construct as static data member. There are few
guidelines to be followed when declaring static data member.
The following program will give clear difference between static data count and regular data number members. The static data is common to all objects but regular data is unique to its object.
class item
{int number; static int count; public:
void getdata()
{++count;
number=count;
}
void putdata(void)
{cout<<"Count is "< cout<<"Number is "< } }; int item::count=0; int main() { clrscr(); item x,y,z; //Three object produced from class item x.getdata(); y.getdata(); z.getdata(); x.putdata(); The count is 3 and number is 1 y.putdata(); The count is 3 and number is 2 z.putdata(); The count is 3 and number is 3 return 0; } Static Member Function: The static member function can be specified like static data member. It works asc a static member data except the static member data is used in static member function. Rules of Static Member function are given below: The static member function can read static data member only from its class. The static member function is called using class name not object name. class_name::static_function_name( ); It can also be called using object.static_fun( ). The given program will illustrate how the static member function is worked. class find {static int count; int code; public: static void showcount(void) {cout<<"Count is " < } void setcode(void) {code = ++count; } void setcount(void) { cout<<"Code is " < } }; int find ::count=0; int main() {clrscr(); find x,y,z; x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2 z.setcode(); //Code and count is 3 find::showcount(); //Count is 3 x.setcount(); // Code is 1 for object x. y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z. x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6 //The value of code will increase from its previous value of that object. x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x. return 0; } Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
cout<<"Number is "< } }; int item::count=0; int main() { clrscr(); item x,y,z; //Three object produced from class item x.getdata(); y.getdata(); z.getdata(); x.putdata(); The count is 3 and number is 1 y.putdata(); The count is 3 and number is 2 z.putdata(); The count is 3 and number is 3 return 0; } Static Member Function: The static member function can be specified like static data member. It works asc a static member data except the static member data is used in static member function. Rules of Static Member function are given below: The static member function can read static data member only from its class. The static member function is called using class name not object name. class_name::static_function_name( ); It can also be called using object.static_fun( ). The given program will illustrate how the static member function is worked. class find {static int count; int code; public: static void showcount(void) {cout<<"Count is " < } void setcode(void) {code = ++count; } void setcount(void) { cout<<"Code is " < } }; int find ::count=0; int main() {clrscr(); find x,y,z; x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2 z.setcode(); //Code and count is 3 find::showcount(); //Count is 3 x.setcount(); // Code is 1 for object x. y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z. x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6 //The value of code will increase from its previous value of that object. x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x. return 0; } Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
};
int item::count=0;
int main()
{ clrscr();
item x,y,z; //Three object produced from class item x.getdata();
y.getdata();
z.getdata();
x.putdata(); The count is 3 and number is 1
y.putdata(); The count is 3 and number is 2 z.putdata(); The count is 3 and number is 3
return 0;
Static Member Function:
The static member function can be specified like static data member. It works asc a static member data except the static member data is used in static member function.
Rules of Static Member function are given below:
The given program will illustrate how the static member function is worked.
class find
{static int count;
int code;
public:
static void showcount(void)
{cout<<"Count is " < } void setcode(void) {code = ++count; } void setcount(void) { cout<<"Code is " < } }; int find ::count=0; int main() {clrscr(); find x,y,z; x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2 z.setcode(); //Code and count is 3 find::showcount(); //Count is 3 x.setcount(); // Code is 1 for object x. y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z. x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6 //The value of code will increase from its previous value of that object. x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x. return 0; } Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
void setcode(void)
{code = ++count;
void setcount(void)
{ cout<<"Code is " < } }; int find ::count=0; int main() {clrscr(); find x,y,z; x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2 z.setcode(); //Code and count is 3 find::showcount(); //Count is 3 x.setcount(); // Code is 1 for object x. y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z. x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6 //The value of code will increase from its previous value of that object. x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x. return 0; } Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
} }; int find ::count=0; int main() {clrscr(); find x,y,z; x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2 z.setcode(); //Code and count is 3 find::showcount(); //Count is 3 x.setcount(); // Code is 1 for object x. y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z. x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6 //The value of code will increase from its previous value of that object. x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x. return 0; } Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
int find ::count=0;
{clrscr();
find x,y,z;
x.setcode(); // Code and count is 1. y.setcode(); //Code and count is 2 find::showcount(); //Count is 2
z.setcode(); //Code and count is 3 find::showcount(); //Count is 3
x.setcount(); // Code is 1 for object x.
y.setcount(); // Code is 2 for object y. z.setcount(); // Code is 3 for object z.
x.setcode(); // Code and count is 4. y.setcode(); //Code and count is 5 z.setcode(); //Code and count is 6 find::showcount(); //Count is 6
//The value of code will increase from its previous value of that object.
x.setcount(); // Code is 4 for object x. y.setcount(); / Code is 5 for object x. z.setcount(); // Code is 6 for object x.
Since the count is declared as static it has only one copy irrespective of number of objects created. When static count function called again and again it will increase count for all the objects created simultaneously therefore the count will be uniform for all object, but for the data member code it is unique for each object created because it is not a static data member. Therfore from the above example there will only one value for count data member irrespective of number of objects constructed since it is a static data member, where as for code data member the value will be different for each object.
Automatic Variables Automatic variables are variable which are explained within the functions. They lose their value when the function terminates. It can be accessed only in th
Normal 0 false false false EN-US X-NONE X-NONE
develop a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with
At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes
Virtual Functions The keyword virtual was previously used to resolve ambiguity for a class derived from two classes, both having a common ancestor. These classes are known as v
A: Use p = new T[n] and delete[] p: Fred* p = new Fred[100]; ... delete[] p; Any time you allocate an array of objects through new (generally with the [n] in the n
Define Commonly Used Built-in Library Functions? Comprise opened a file pointer you will desire to use it for either input or output. The C language supplies a set of functions
There are formal ways of reducing Boolean expressions in order to minimize the logic circuit. The two elementary ways of minimization are using Boolean expressions/De Morgan Theore
algorithm to find out all the factors of given positive integers
Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec
Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!
whatsapp: +1-415-670-9521
Phone: +1-415-670-9521
Email: [email protected]
All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd