Constructor inheritance.

I googled this topic but everything I came across wasn't what I wanted. I am trying out the example posted in classes I tutorial but using constructors. My first guess was constructors will be inherited. Upon some google research, I found out that they aren't but can be manually inherited. My question is, how can I use variable rect (class rectangle) and tri(class triangle) to assign values for length and width (width is also assumed as height for triangle) using the constructor dimensions(){}?

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
class dimensions
{
protected:
	float length;
	float width;
public:
	dimensions(void){};
};
class rectangle : public dimensions
{
public:
	float rectarea()
	{
		return length * width;
	}
};

class triangle : public dimensions
{
public:
	float triarea()
	{
		return length * width / 2;
	}
};
dimensions::dimensions(void)
{
	std::cout << "Please enter a value for length ";
	std::cin >> length;
	std::cout << "Please enter a value for width ";
	std::cin >> width;
}
When derived objects are created the base constructor is called first automatically. So if you just created an object of triangle/rectangle in this example the dimensions constructor will get called and let you assign the values.

Not 100% sure if this is what you are asking for though.
Last edited on
Topic archived. No new replies allowed.