0

Below is the code to a "rhombus.cpp" file which i am to complete. But first i would like to highlight this area " Rhombus::Rhombus(Vertex point, int radius) : Shape(point){".. my question is what exactly occurs here when in the compiler? the : Shape(point) is completely new to me so i am unsure of what to do, especially when i am to call this in the int main. And if what i have placed under the comment of //place your code here and add comments, etc is correct in what ive done? Your help is much appreciated!

#include "rhombus.h"

Rhombus::Rhombus(Vertex point, int radius) : Shape(point){
// constructs a Rhombus of radius around a point in 2D space
if((radius>centroid.getX()/2) || (radius>centroid.getX()/2))
{
    cout << "Object must fit on screen." << endl;
    system("pause");
    exit(0);
}
// place your code here and add comments that describe your understanding of        what is happening
this->radius = radius;
plotVertices();
}

int Rhombus::area()
{
// returns the area of a Rhombus
return 0;
}

int Rhombus::perimeter()
{
// returns the perimeter of a Rhombus
return 0;
}

void Rhombus::plotVertices()
{
// a formula for rotating a point around 0,0 is
// x' = x * cos(degrees in radians) - y * sin(degrees in radians)
// y' = y * cos(degrees in radians) + x * sin(degrees in radians)
// the coordinates for point A are the same as the centroid adjusted for the    radius
// the coordinates for point B are determined by rotating point A by 90 degrees
// the coordinates for point C are determined by rotating point A by 180 degrees
// the coordinates for point C are determined by rotating point A by 270 degrees
// remember that 0,0 is at the top left, not the bottom left, corner of the console

// place your code here and add comments that describe your understanding of what is happening
}

Rhombus.h

#include "shape.h"

class Rhombus : public Shape
{
int radius;

void plotVertices();
public:
Rhombus(Vertex point, int radius = 10);
int area();
int perimeter();
};

Shape.h

enter #pragma once
#include "console.h"
#include "vertex.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
using namespace std;

#define PI 3.14159265358979323846

class Shape
{
list<Vertex>::iterator itr;
protected:
list<Vertex> vertices;
Vertex centroid;
void drawLine(int x1, int y1, int x2, int y2);
Shape(Vertex point);
double round(double x);
public:
void drawShape();
virtual int area() = 0;
virtual int perimeter() = 0;
virtual void outputStatistics();
void rotate(double degrees);
void scale(double factor);
};
Satain
  • 9
  • 1
  • 7
  • Does rhombus.h include a shape.h? Also look at the line `class rhombus` in rhombus.h. You should look in your literature for inheritance. – Captain Giraffe Apr 15 '15 at 16:31
  • Hi and yes it does, ive edited my post to include it, i can also attach the shape.cpp if necessary. inheritance is a topic ive barely touched upon but ill have to look into it online. – Satain Apr 15 '15 at 16:42

1 Answers1

1

The line Rhombus::Rhombus(Vertex point, int radius)

Is the constructor for your class. It tells you you can instantiate/create a Rhombus object like

int main(){
     Vertex v;
     Rhombus rhomb(v, 3); 

This object creation is often called instantiation.

The extra Shape

 Rhombus::Rhombus(Vertex point, int radius): Shape(point)

Describes how to create the Rhombus object. Namely by calling a constructor in Shape that accepts a Vertex argument. Then it runs the rest of the code in the constructor. This is needed because Rhombus inherits Shape as you will find in rhombus.h.

The key stuff you need to read up on is inheritance. This is likely covered quite early.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • Thanks Captain Giraffe, you made it look so simple i almost feel slightly embarrased. But thanks again for including the terminology, i shall now have a further look into this as im required to create numerous shapes with featured scaling and rotation included too. nightmare. Thank you again! – Satain Apr 15 '15 at 17:17