Private members of a base class can only be seen by it's own members. You must declare x of obj as protected in order for inherited classes to see x.
private: Only the members of the same class that declared x can see this member. protected: Only inherited classes and the base class can see protected members.
class obj{
protected:
int x;
public:
obj(int);
};
obj::obj(int x1): x(x1){
}
class someclass: public obj{
private:
public:
someclass(int);
void somefuntion();
};
someclass::someclass(int y):obj(y){
}
someclass *someclass1 = new someclass;//all i needed was this line I think
someclass::somefunction(){
x += 1;
}
main(){
someclass1->somefunction();
return 0;
}
Your problem is here: Where is someclass1 pointing? We don't don't know because you haven't allocated any memory for the someclass1 pointer. Basically there is no someclass object at which the someclass1 pointer is pointing to.
EDIT
someclass *someclass1 = new someclass;//all i needed was this line I think
Didn't see this line.
never allocate globally, it can lead to some serious problems which are difficult to debug. Put your allocating code inside the main scope. Also do not forget do deallocate when you're done =)
Doesn't the someclass constructor call in line 23 require an argument? But yes, you were getting a segmentation violation because someclass1 was not allocated, not because of the member variable.
Unless something has changed recently, the default constructor is only automatically generated when NO constructors are declared for the class. Since the single-argument constuctor is declared, the default constructor should not be generated.
Are you saying my code shouldn't compile? It does, well it seems to anyway.
Also CppSpartan in your first post you said i shouldn't allocate globally. Could you elaborate a little on that?
I've just spent ages ripping everything out of my main function to allocate globally because I've got quite a few classes all accessing each other and this seemed like the best way to do it.
edit: Oh sorry yeah your right didn't write that part out right. It has an argument in my code and compiles ok.:)
It's generally a bad practice because it makes the program more complex. When having several classes that access each other you can solve this in several ways.
1 pass a reference, to each class you want to access, into your constructor and store it as a member.
2 pass a reference to a class that encapsulate our classes.