Q:

C++ program to declare, define and access public static data member.

belongs to collection: C++ programs on various topics

0

C++ program to declare, define and access public static data member.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program (Code Snippet) – Declare, Define and Access a Public Static Data Member inside main()

Let’s consider the following example:

/*C++ program to declare, define and access 
public static data member.*/

#include <iostream>

using namespace std;

class Sample{
	public:
		//declaration of static data member
		static int x;
};

//defining static data member of class
int Sample::x=0;

int main(){
	
	//accessing static data member
	cout<<"value of x: "<<Sample::x<<endl;
	
	//modify the value of x
	Sample::x=100;
	cout<<"Modified value of x: "<<Sample::x<<endl;
	
	return 0;
}

Output

            value of x: 0 
            Modified value of x: 100

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to print ASCII value of a character.... >>
<< C++ program to access public data member inside ma...