Static members in C++

Ratings:
(4)
Views:199
Banner-Img
  • Share this blog:

Static Members

The members of class can be declared as static in C++ Static members are of  2 types static data member static member function  

Static Data Member in C++

A variable declared inside class with static keyword is called static data member. static data member is a global data member, which is global to class or more than one object Static member memory is allocated when finest object of class is create or when it accessed first time. Static data member must be initialized outside the class.   Static Members in C++ Example class Alpha  - - > data type { static int z; int x; int y; }; void main() { Alpha alpha1; Alpha alpha2; Alpha alpha3; }    

Syntax of declaring static data member in C++

static data type variable-name;  

Syntax of defining static data member in C++

definition is given outside the class datatype <class-name> :: <member-name>=[name] int –0 flat,double – 0.0 char- - - \o(null)  

Static data member is accessed with class name or object name

Example : class Beta { public : int x; - - > on creation of object static int y; - - > on creation of first object (accessed fines+time with class name) }; int Beta :: y=0; void main() { cout<<Beta :: y; - - > 0 cout<<Beta :: x; - - > X Beta  Beta1; cout<<Beta1.x; -  - > }  Example  # include<iostream.h> class circle { float r; static float pi; public : void read(); void find_area(); }; float circle :: pi=3.147; void circle :: read() { cout<<”\n input r”; cin>>r; } void circle :: find_area() { float area = pi*r*r; cout<<”\n Area of circle is ”<<area; } void main() { circle circle1; circle circle2; circle1.read(); circle2.read(); circle1.find_area(); circle2.find_area(); }   find output ?

Example

# include<iostream.h> class Alpha { public : int x; static int y; }; int alpha :: y; void main() { alpha alpha1; alpha1.x=10; alpha1.y=20; alpha alpha 2; alpha2.x=30; alpha2.y=40; cout<<”\n alpha1.x”<<”\t”<<alpha1.y; cout<<”\n alpha2.x”<<”\t”<< alpha2.y  

Static member functions in C++

A member of class can be defined as static static member function is a global function this member function is accessed with name without creating object class Alpha { public : static int x; int y; }; int Alpha :: x=10; void main() { cout<<Alpha::x; cout<<alpha<<y; } class Alpha { static int x; }; int alpha :: x=10; void main() { cout<<alpha ::x; }

  • static member function access only static members of the but cann’t access non static members

  syntax static retur_type function-name([paramenters]) { statements; }

Syntax of calling static member function in C++

class-name :: function-name # include<iostream.h> { class alpha { public: void func1() { cout<<”\n inside non static member function”; } static void fun2() { cout<<”\n inside static member funciton”; }}; void main() { alpha:: func1();// erro (Non static member can’t access class name) alpha:: fun2(); } o/p: inside static member function

 Example

# include<iostream.h> # include<conio.h> class alpha { static int x; public : static void fun1() { cout<<”\n x=” <<x; o/p : x=10 cout << “\n y=” <<y; // error (static member function cannot access not static members o class); }}; int alpha :: x=10; void main () { int alpha :: x=10; void main() { alpha:: fun1() } Static member function can access non static member of class by creating object.      

//Count of object or finding how many objects of class are created.

Example #include <iostream.h> class student { int rno; char name[10]; static int count; public: student() { count=count +1; } static void print –count() { cout<<”\n count is “<<count; } void read() { cout<<”\n Input roll no; cin>>rno; cout<<”\n Input name; cin>>name; void print() { cout<<”\n Rollno”<<rno; cout<<”\n Name”<<name; }}; int student:: count; void main() { student:: print_count(); student student1; student student2; student::print _count(); student1.read(); student2.read(); student1.print(); student2.print(); }  

Non static Member function can access static member and non- static Members.

 

Example

# include<iostream.h> # include <conio.h> class account { int accno; char name[10]; float balance; static float rate; public: void read() { cout <<”\n Input accno”; cin >>accno; cout<<”\n Input customer name”; cin >>name; cout<<”\n Input balance”; cin >>balance; } void print() { cout<<”\n Account No”<<accno; cout <<”\n Name”<<name; cout <<”\n Balance”<<balance; cout<<”\n Rate”<<rate; } void change _rate(float r) { rate=r; }}; float account :: rate=1.5f; //definition void main() { clrscr(); account account1; account account2; account1.read(); account2.read(); account1.print(); account2.print(); account:: change_rate(1.2); account1.print(); account2.print(); }

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox