OOP/inheritance question

Hey all,

I'm trying to get an inheritance system down with a base abstract class and a bunch of classes derived from this one class.

The abstract class has a protected boolean, and when one particular type of derived class is constructed, I want the boolean to be set to false. However, when I do this, I get an "unresolved external" error, and I'm not quite sure what that means.

Also, I want to make sure I have the concepts down: derived classes do not inherit constructors, but they can have their own constructors that can mess around with anything declared protected or public in the derived class, correct?

Thanks so much! Your help is much, much appreciated.
They DO inherit constructors, and they can modify anything protected or public in the base class, and anything the derived class.
All you have to do to initialize a variable with a value is use a constructor right...? Just do this
1
2
Class::Class() : booleanFlag(false)
{ }


In the future please post your code however. It doesn't really help if you say you're getting an error and don't show any code...
Last edited on
I get an "unresolved external" error, and I'm not quite sure what that means.


999 times out of 1000, it means you wrote a function prototype, but never gave that function a body.

Read the rest of the error and it will tell you which function is the culprit.
Yup, that was it. I wrote prototypes and no body. Noted. Thanks for the clarification!

Sorry ascii! I'll be sure to put code up next time.
Don't worry ;) As long as you got your program working its all good!
firedraco wrote:
They DO inherit constructors ...
They don't so much inherit the parents' constructors as they have the ability to invoke them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class Base {
 public:
	Base() {
		cout << "Base Constructor\n";
	}
	
	void baseMethod() {
		cout << "Base Method\n";
	}
};

class Sub : public Base {
	int val;

 public:
	Sub(int i) {
	 //will automatically invoke Base()
		cout << "Sub Constructor\n";
		val = i;
	}
	
	void subMethod() {
		cout << "Sub (" << val << ") Method\n";
	}
};

//main function
int main() {
	Base b;
	// Sub s; //Not inheritance (error: "no appropriate default constructor available")
	Sub s(1);
	
	b.baseMethod();
	s.baseMethod(); //Inheritance (no error)
	s.subMethod();
	
	return 0;
}
Base Constructor
Base Constructor
Sub Constructor
Base Method
Base Method
Sub (1) Method
Last edited on
Topic archived. No new replies allowed.