c++ very basic problem

hi I'm just learning the c++ . I have following doubt . I write a simple code but when i compile it shows errors . my code is following:

code :
#include<iostream>
using namespace std ;
class abc
{
public:
int x,y,z ;
public: abc()
{ x=10,y=20;z=30 ;}

};
int main()
{
abc* a;
a->x = 50;
cout<<a->x;
}

it compiled successfully but when i run it it shows segmentation fault .
can anybody explain me why it is showing segmentation fault ?

Than i just change the access modifier in code like following :
code :
private :
int x,y,z ;
When i compile it it shows the following error :

xyz.cpp: In function ‘int main()’:
xyz.cpp:6: error: ‘int abc::x’ is private
xyz.cpp:14: error: within this context
xyz.cpp:6: error: ‘int abc::x’ is private
xyz.cpp:15: error: within this context

Now my questions are following :
what is the difference between object pointer and class pointer ?

How to declare and use object pointer and class pointer ?

Is there any possible way to access the private member of class without using member function ?

Can we use object pointer or class pointer to access the private data member of class ?

Please explain with some example .
folowing code is true:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std ;
class abc
{
public:
int x,y,z;
abc()
{ x=10;y=20;z=30 ;}

};
int main()
{
abc *a=new abc;
a->x = 50;
cout<<(*a).x;
system ("pause");
}

if you wish access to private members of class you have to use function for acess to them
if you wish declare pointer you have to reserved memory first

with private members :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std ;
class abc
{
private:
int x,y,z;
public:
abc()
{ x=10;y=20;z=30 ;}
int change ()
 {
        x=50;
        cout << x << endl;
        }
};
int main()
{
abc *a=new abc;
(*a).change ();
system ("pause");
}
Thanx Dear but I still did not get the concept of object pointer and class pointer . please explain them for me
Well, that's easy: there is no such thing as class pointers and "object pointers" are pointers.
closed account (S6k9GNh0)
1. abc *a; a->x = 50; - This code will not give a compiler warning/error but is incorrect. Pointer a is assigned to an undefined location and assigning it data without changing the location of that pointer to one that is known will result in undefined behavior. This is most likely causing the segmentation fault.

2. abc() { x=10;y=20;z=30 ;} - This code functions correctly but has an easy and often missed optimization to it. You can use the constructor's initializer list like so: abc() : x(10), y(20), z(30) {}

http://www.cplusplus.com/forum/articles/17820/

3. system("pause"); - http://cplusplus.com/forum/articles/11153/


Here is a more proper example to do what you originally wanted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;

class abc
{
public:
	int x,y,z ;

	abc()
	: x(10), y(20), z(30)
	{}

};

int main()
{
	abc* a = new abc;
	a->x = 50;
	cout<<a->x;
	delete a;
	std::cin.ignore();
}

Last edited on
closed account (S6k9GNh0)
What the hell? My post time moved ahead ten minutes and is now ahead of everyone....
Topic archived. No new replies allowed.