2
Answers

Inheritance

Administrator

Administrator

21y
1.6k
1
I have 3 classes, myClass1, myClass2, and myBaseClass. The simplified code looks like this public class myClass1: myBaseClass { public int myClassInt = 0; public myClass1() { IncreaseTheInteger(); } } public class myClass2: myBaseClass { public int myClassInt = 0; public myClass2() { IncreaseTheInteger(); } } public class myBaseClass { public myBaseClass() { } public void IncreaseTheInteger() { // increase the calling class's integer by 5 myClassInt += 5; // ERROR: myBaseClass doesn't know what myClassInt is. } } As you can see, the 2 derived classes (myClass1 and myClass2) both have a public integer named myClassInt. Both of these classes inherit from myBaseClass which contains a function called IncreaseTheInteger(). As I showed above, what I want to do is this: // in the myBaseClass::IncreaseTheInteger function myClassInt += 5; The problem here is that myBaseClass doesn't know what myClassInt is. To say it simply, what I want to be able to do is allow 2 classes to be able to use a function in a third class. Is this possible?
Answers (2)