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
        




Copyright © 2024 www.includehelp.com. All rights reserved.