Help with pure virtual functions/interface

Sep 3, 2019 at 4:41am
This is a homework problem, there is much more to the code but I have shortened it down to show one specific example, but I am having the problem in all classes.

The instructions are: Write a program that has a Polygon interface that has abstract functions area() and perimeter(). Implement classes for Triangle, Quadrilateral... (etc), which implement this interface with the obvious meanings for area() and perimeter() functions. Also, implement classes Isosceles Triangle ... (etc) which have the proper inheritance relationships. I also need to implement inheritance and polymorphism in my code.

My code:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

class Polygon
{
public:
	virtual double area() = 0;
	virtual double perimeter() = 0;
};

class Triangle : public Polygon
{
public:
	Triangle() { base = 0, height = 0, side1 = 0, side2 = 0; }
	Triangle(double b, double s1, double s2) { b = base, s1 = side1, s2 = side2; }
	double area() override
	{
		return ((base * height) / 2);
	}
	double perimeter() override
	{
		return (base + side1 + side2);
	}

protected:
	double base, height, side1, side2;
};

class IsoscelesTriangle : public Triangle
{
public:
	IsoscelesTriangle() { base = 0, side = 0; }
	IsoscelesTriangle(double b, double s) { b = base, s = side; }
	double perimeter() override
	{
		return ((2 * side) + base);
	}

protected:
	double side;
};


int main()
{
	double baseIso, sideIso;
	std::cout << "Please enter a base: ";
	std::cin >> baseIso;
	std::cout << "Please enter a side: ";
	std::cin >> sideIso;
	IsoscelesTriangle userIsosceles(baseIso, sideIso);
	std::cout << "The area is: " << userIsosceles.area() << "\n";
	std::cout << "The perimeter is: " << userIsosceles.perimeter() << "\n";

	system("pause");

	return 0;
}


I believe that the problem is: either I made the constructors in the classes wrong, I am doing something wrong when I create the objects in my main function, or I am doing something wrong when calling the area() and perimeter() functions. .

The problem that I am having is that whenever I run the program, I always have (what I believe to be) the default constructor values. When I call the area function, I always return 0, and when I call the perimeter function, I get -1.85119e+62.
Last edited on Sep 3, 2019 at 4:43am
Sep 3, 2019 at 4:48am
IsoscelesTriangle(double b, double s) { b = base, s = side; }
Oops.

EDIT: To clarify (though I'm sure you know this), these assignments are backwards. You have a similar problem in Triangle.

-Albatross
Last edited on Sep 3, 2019 at 4:49am
Sep 3, 2019 at 4:51am
> Triangle(double b, double s1, double s2) { b = base, s1 = side1, s2 = side2; }
Your assignments are the wrong way round.
You don't assign height.
Sep 3, 2019 at 5:03am
Unrelated to your question, though I hope it's helpful nonetheless: I feel like pointing out that you don't strictly speaking need a side variable in IsocelesTriangle. You already have side1 and side2 in your Triangle class. Isosceles is just a special case wherein those two are equal (and would be initialized as such).

Now, if Triangle was abstract and required whatever inherits from it to implement a few member functions that provided the lengths of each side, that would be a different story.

-Albatross
Sep 3, 2019 at 5:10am
Oh wow! Such a simple mistake!

Thank you both so much for the help! I really appreciate it.

Also, thank you for pointing that out Albatross. Now that I think about it, it does seem sort of redundant to create another side variable in IsoscelesTriangle. Really appreciate you pointing that out! It really was helpful.
Topic archived. No new replies allowed.