Question:
C++ class question, only way to explain this is an example.?
2013-07-10 13:50:47 UTC
I do not know how to explain this in words so here is an example:

class ExampleClass
{
//what ever definitions
}

Class AnotherClass : public ExampleClass
{
int someInt;
AnotherClass()
{
someInt = 1;//just a random value
}
}

ExampleClass *examplePointer = new AnotherClass();

examplePointer->someInt = 2;//error: ExampleClass does not have variable someInt :(
(end of example code)

Do you have any suggestions on how to over come this?
Six answers:
Demian
2013-07-11 07:29:18 UTC
You can do this



ExampleClass* examplePointer = new AnotherClass();



( (AnotherClass*) examplePointer )->someint = 2;



Cast ExampleClass to AnotherClass to access the someInt attribute
max v
2013-07-11 02:54:52 UTC
there are 2 problems:



===someInt is not in Example class===

you have an Example class pointer.

Example class doesn't have someInt.



===possible problem===

AnotherClass has someInt but it looks like it's private. i'm guessing you accidentally removed the "public:" keyword. all class members are private by default. (because you can't even construct AnotherClass with the constructor private).







===possible solution===

usually people write "getter" and "setter" functions to access private variables (and keep them private). what you can do, is make a virtual function in the parent class (ExampleClass). then override it in the child class (AnotherClass) by simply defining it



---code---

class ExampleClass

{

//what ever definitions

public:

virtual void setSomeInt(int value) = 0; // pure virtual function

}



Class AnotherClass : public ExampleClass

{

private:

int someInt;

public: // this should have been here

AnotherClass()

{

someInt = 1;//just a random value

}



void setSomeInt(int value)

{

someInt = value;

}

}



ExampleClass *examplePointer = new AnotherClass();



examplePointer->setSomeInt(2); // use the setter function

----------







now any class that inherits from ExampleClass has to override the virtual function "setSomeInt" with it's own implementation/behavior.



ExampleClass does not have it's own implementation of "setSomeInt", so it's pure virtual. this is indicated by "= 0;".

so you can only create an instance of ExampleClass by creating an instance of it's child class (AnotherClass) like you did with "ExampleClass *examplePointer = new AnotherClass();", because ExampleClass incomplete (an abstract class).







if you want to be able create an instance of ExampleClass directly. you need to provide a default implementation for the virtual function in ExampleClass, like:



---code---

virtual void setSomeInt(int value) // do nothing ("= 0;" has simply been replaced with {})

{



}

----------







===additional details===

you need to create a getter "int getSomeInt()" function for which you need to do the same. then you will be able to read the value as well:



---code---

if (examplePointer->isAnotherClass())

{

std::cout << examplePointer->getSomeInt(); // use the getter

}

----------
2016-05-20 15:31:30 UTC
Object Orientated (OO) concepts are a little tricky to explain without gettinga bit complex. A class (in c++) is the definition of a data structure or concept. The class defines the behaviour and attributes that are shared by all instances of the class. They act as a kind of template when instancing objects of the class. Classes can be extended by polymorphic inheritance, you derive a new class from a base class and add any additional methods or data attributes require to fulfill its functionality. The polymorphic behaviour allows the application to automatically call the required methods without knowing what kind of object it is dealing with. It deals with eth base class calls and the object itself (or rather the compiler via the vtable) will ensure that the correct method relating to that class is called. Clayton - I think you misunderstood the question.
_Object
2013-07-10 14:42:51 UTC
You're failing to inherit a private member with public inheritance.

Declare the integer public or protected, or inherit with private inheritance, but the derived child class "shouldn't touch its parent's privates".
?
2013-07-10 13:53:33 UTC
3rd line "//what ever definitions" how can I know what is the question trying to make?



post all the codes so we can identify
2013-07-10 13:53:58 UTC
It is called polymorphism. http://www.cplusplus.com/doc/tutorial/polymorphism/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...