assignment help

this assignment is using polymorphism and inheritance.

Requirements:
1. The “is a” relationship is modeled by public inheritance.
2. The “has a” relationship is modeled by composition.
3. The project contains four classes, i.e., Shape, Point, Circle, and RTriangle.
4. All classes must have a default constructor.
5. Class Point, Circle, and RTriangle must have a constructor initializer, which takes as many parameter as
needed to initialize the data members.
6. Class Shape must have a protected data member area of type double.
7. All classes must have a public method called calculate_area().
8. Class Point must have two public data members x and y, both of type int.
9. Class Circle must have one protected data member named center that is an object or a pointer to an object
of class Point.
10. Class Circle must have one protected data member radius of type double.
11. Class RTriangle must have three protected data members hypotenuse, side1 and side2, all of type int.
12. Class Shape is the base class for Circle, and RTriangle.
13. The constructor initializer for RTaiangle must validate the three sides by the equation that
hypotenuse^2=side1^2 + side2^2.
14. The main driver must declare an object of class Circle, and an object of class RTriangle, as show in the
figure, and print out the corresponding data members and the area of each object.
_____________________________________________________________________________
I've done almost everything listed here, in separate files. There are 7 files(headers and.cpp)

But what im confused about is Rtriangle file and Circle file.
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
#include "Rtriangle.h"

Rtriangle::Rtriangle()
{
	hypotenuse = 0;
	side1 = 0;
	side2 = 0;
}
Rtriangle::	Rtriangle(int s1,int s2, int h)
{
	hypotenuse = h;
	side1 = s1;
	side2 = s2;

        //theres suppose to be an if, else statement here validating the three sides? 
 
}

int Rtriangle::getside1()
{
	return side1;
}

int Rtriangle::getside2()
{
	return side2;
}
int Rtriangle::gethypotenuse()
{
	return hypotenuse;
}

double Rtriangle::calculate_area()	//overriding calc area from shape
{
	double area;
	area = ((side1 * side2)/2);
	return area;
}

void Rtriangle::print()
{
	cout << "\t Side1 of RT:" << side1 << endl;
	cout << "\t Side2 of RT:" << side2 << endl;
	cout << "\t Hypotenuse of RT:" << hypotenuse << endl;
	cout << "\t RTraiangle Area:" << calculate_area() << endl;
}


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>
#include "Shape.h"
#include "Point.h"
using namespace std;

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle: public Shape
{
protected:
	double radius;
	Point *center;              

public:
	Circle()[output][/output]
	{
		radius = 0;
	}
	Circle(double r, Point *cent) 
	{
		radius = r;
		center = cent;
	}

	void setradius(double r)
	{
		radius = r;
	}

	double getradius()
	{
		return radius;
	}

	Point getCenter()
	{
		
	}

	virtual double calculate_area()
	{
		double pi= 3.14159;
		double area;
		area = pi*(radius * radius);
		return area;
	}

	void print()
	{
		cout << "\t Circle at: " << center << endl; //get a weird output of CCCCCCC here
		//center.print();
		cout << "\t Radius: " << radius << endl;
		cout << "\t Circle Area: " << calculate_area() << endl;
	}
};
#endif 

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
//MAIN
#include <iostream>
#include "Circle.h"
#include "Rtriangle.h"
using namespace std;

void main()
{
	Circle circ;
	Rtriangle rtri(3,4,5);
	Point p;

	Shape* shapeptr;

	shapeptr = new Circle;
	//shapeptr = new Rtriangle;

	shapeptr->calculate_area();

	p.setvalues(3,2);
	circ.setradius(15);

	cout << "Custom Object Values:" << endl;
	cout << "\t ";
	p.print();
	circ.print();
	rtri.print();

	delete shapeptr;
	//system("pause");
}


basically my questions are how I will use virtual with calculate_area() in all the class files, I know we have to use pointers in main, but i am uncertain of some things. and if anyone could clarify, it would be greatly appreciated .
And also need help with the if,else in the Rtriangle contructor initializer.
Hi

I think the calculate_area() method should be a pure virtual method, because the base class can not implement it for different type of shape, each sub-class has to implement this method in order to calculate the correct type of area, latter you may also add Rectangular or other shape. The super class, base class, in your case is just an abstract class.

In your TODO list the #13, I do not think it is enough to just calculate
c^2 = a ^2 + b^2 you may also have to validate the triangular against the cosine theorem. However, you may ask your teacher what he really wants :-)
Last edited on
Topic archived. No new replies allowed.