需要一些帮助来澄清我试图完成 C++ 的一段代码
Need some help clarifying a piece of code which im trying to complete C++
下面是我要完成的 "rhombus.cpp" 文件的代码。但首先我想强调这个区域“Rhombus::Rhombus(Vertex point, int radius) : Shape(point){”..我的问题是在编译器中这里到底发生了什么? : Shape(point) 对我来说是全新的,所以我不确定该怎么做,尤其是当我要在 int main 中调用它时。如果我在 //place your code here and add comments 的评论下放置的内容在我所做的事情中是正确的?非常感谢您的帮助!
#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);
};
行
Rhombus::Rhombus(顶点,整数半径)
是您的 class 的构造函数。它告诉您可以 instantiate/create 像
这样的菱形对象
int main(){
Vertex v;
Rhombus rhomb(v, 3);
这种对象创建通常称为实例化。
额外的形状
Rhombus::Rhombus(Vertex point, int radius): Shape(point)
描述如何创建菱形对象。即通过在 Shape
中调用接受 Vertex
参数的构造函数。然后它运行构造函数中的其余代码。这是必需的,因为 Rhombus 继承了 Shape,正如您将在 rhombus.h
中找到的那样。
您需要阅读的关键内容是继承。这可能很早就涵盖了。
下面是我要完成的 "rhombus.cpp" 文件的代码。但首先我想强调这个区域“Rhombus::Rhombus(Vertex point, int radius) : Shape(point){”..我的问题是在编译器中这里到底发生了什么? : Shape(point) 对我来说是全新的,所以我不确定该怎么做,尤其是当我要在 int main 中调用它时。如果我在 //place your code here and add comments 的评论下放置的内容在我所做的事情中是正确的?非常感谢您的帮助!
#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);
};
行 Rhombus::Rhombus(顶点,整数半径)
是您的 class 的构造函数。它告诉您可以 instantiate/create 像
这样的菱形对象int main(){
Vertex v;
Rhombus rhomb(v, 3);
这种对象创建通常称为实例化。
额外的形状
Rhombus::Rhombus(Vertex point, int radius): Shape(point)
描述如何创建菱形对象。即通过在 Shape
中调用接受 Vertex
参数的构造函数。然后它运行构造函数中的其余代码。这是必需的,因为 Rhombus 继承了 Shape,正如您将在 rhombus.h
中找到的那样。
您需要阅读的关键内容是继承。这可能很早就涵盖了。