المعين الموجه للكائنات في C++
سؤال
أواجه مشكلات مع المهمة التي تم تكليفي بها، والمشكلة هي أنني لا أستطيع العثور على أي موارد بأي حال من الأحوال مشابهة للكود الذي تم إعطائي إياه.لقد قرأت العديد من المستندات محاولًا العثور على أوجه التشابه ولكن لم أجد أي شيء مفيد.
أحتاج إلى مساعدة في محاولة فهم هذا الرمز وكيفية استخدامه لإنشاء المعين. الأشياء الوحيدة التي لا أستطيع فهمها هي كيفية إنشاء شكل معين ينتمي إلى فئة الشكل.قم بتطبيق النقطه الوسطى على المعين ثم قم بإضافة القمم باستخدام طريقة Push_back. لسوء الحظ، يجب استخدام طريقة الدفع للخلف هذه، لقد فشلت في الاختبار باستخدام drawLine(10,10,40,10);إلخ لرسم الخطوط حيث أردت.
سأقوم بطحن هذا لمدة أسبوع قوي لذا يجب أن أرد بسرعة.
//This is the rhombus.cpp file
#include "rhombus.h"
Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
}
Rhombus shape1(20, 20);
shape1.plotVertices();
}
void Rhombus::plotVertices()
{
//vertices.push_back(Vertex(centroid.getX(), centroid.getY() + radius));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
}
// This is the rhombus.h file
#include "shape.h"
class Rhombus : public Shape
{
int radius;
void plotVertices();
Rhombus(Vertex point, int radius = 10);
int area();
int perimeter();
};
// This is the shape.cpp file
#include "shape.h"
Shape::Shape(Vertex point) : centroid(point)
{
// constructs a shape
}
void Shape::drawShape()
{
list<Vertex>::iterator current = vertices.begin();
list<Vertex>::iterator previous = vertices.begin();
while(current!=vertices.end())
{
Console::gotoXY((*current).getX(),(*current).getY());
cout << "*";
if(current!=vertices.begin())
drawLine((*current).getX(),(*current).getY(), (*previous).getX(), (*previous).getY());
previous = current;
current++;
}
previous = vertices.begin();
//Debug assertion error here.
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
}
void Shape::drawLine(int x1, int y1, int x2, int y2)
{
bool steep = (abs(y2 - y1) > abs(x2 - x1));
if(steep)
{
swap(x1, y1);
swap(x2, y2);
}
if(x1 > x2)
{
swap(x1, x2);
swap(y1, y2);
}
int dx = x2 - x1;
int dy = abs(y2 - y1);
float error = dx / 2.0f;
int ystep = (y1 < y2) ? 1 : -1;
int y = y1;
int maxX = x2;
for(int x=x1; x<maxX; x++)
{
if(steep)
{
Console::gotoXY(y,x);
cout << "*";
}
else
{
Console::gotoXY(x,y);
cout << "*";
}
error -= dy;
if(error < 0)
{
y += ystep;
error += dx;
}
}
}
double Shape::round(double x)
{
if (ceil(x+0.5) == floor(x+0.5))
{
int a = (int) ceil(x);
if (a%2 == 0)
return ceil(x);
else
return floor(x);
}
else
return floor(x+0.5);
}
void Shape::outputStatistics()
{
}
// This is the shape.h file
#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
هي بالفعل فئة فرعية من Shape
(class Rhombus : public Shape
) لذا عليك فقط إنشاء مثيل لـ Rhombus
لكي يحدث كل السحر.
Shape
يتم تعريفه بحيث Vertex
مرت عليه ( كما point
المعلمة) تستخدم لتهيئة centroid
متغير المثيل تلقائيا؛حتى تتمكن من استخدامها centroid
باعتبارها النقطه الوسطى لأي عملية تتطلب البيانات ذات الصلة بالنقطه الوسطى، سواء من الداخل Shape
أو من داخل إحدى فئاتها الفرعية مثل Rhombus
.
وبالمثل، القائمة vertices
متاح ل Shape
وجميع فئاتها الفرعية كمتغير مثيل.إذا نظرت إلى بقية الكود (على سبيل المثال. Shape::drawShape
)، ستلاحظ كيف vertices
تم استخدامه لمعالجة رؤوس الشكل الحالي.
ما عليك القيام به هنا مشابه تمامًا لذلك.على سبيل المثال،
Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
}
// create vertices for a rhombus with horizontal and vertical diagonals
vertices.push_back(Vertex(point.getX() - radius, point.getY()));
vertices.push_back(Vertex(point.getX(), point.getY() - radius));
vertices.push_back(Vertex(point.getX() + radius, point.getY()));
vertices.push_back(Vertex(point.getX(), point.getY() + radius));
}
عندما تكون في الداخل Rhombus::Rhombus
(المنشئ)، أنت بالفعل داخل المعين الذي تم إنشاؤه للتو؛ليس عليك إنشاء ملف Rhombus
الكائن مرة أخرى.عليك فقط "تزيين" المثيل عن طريق إضافة القمم وتحديد النقطه الوسطى (وهو ما تم القيام به بالفعل).
فقط تخيل أنك تقوم بإنشاء ملف Rhombus
من أصل Shape
;تحتاج إلى إنشاء 4 رؤوس وإضافتها إلى البنية التي تتتبع قائمة جميع القمم.