Code Snippets C/C++ Code Snippets

C++ program to access public data member inside main using object name.

By: IncludeHelp, On 07 OCT 2016

In this program we will learn how to access public data member/ public variable of class inside main function using object name?

In this example we are defining a class named sample which has a public data member/ public variable int x which is going to be access inside the main function using sample class’s object objSample.

C++ program (Code Snippet) – Accessing public data member (Variable) inside main function using Object Name

Let’s consider the following example:

/*C++ program to access public data member 
inside main using object name*/

#include <iostream>

using namespace std;

class Sample{
	public:
		int x;
};

int main(){
	Sample objSample;
	objSample.x=20;
	cout<<"value of x: "<<objSample.x<<endl;
	return 0;
}

Output

            value of x: 20
        


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.