我读过几篇类似的文章,我要么不明白它们能提供什么,要么它们似乎不适用。我是新来的,尽我最大努力遵守规则。
我们在课程的最后两周学习c++,40小时后进入期末考试:),所以我是个初学者。我对C很熟悉。
以下是任务中与此相关的部分:
Part 1 [20 points]
Reading material:
Copy constructor: https://www.geeksforgeeks.org/copy-constructor-in-cpp/
References (the & symbol): https://www.geeksforgeeks.org/references-in-c/
------------------------------------------------------------------------------------------------------
Write a class Point representing a point in 2d.
The class will have the following public methods:
// constructor - sets the coordinates to be x,y
Point( int x, int y)
// copy constructor - creates a new point with the same
coordinates
Point(Point ©)
// getters
int getX()
int getY()
// setters
void setX()
void setY()
// prints to screen the coordinates in the format (x,y)
void print()我的执行情况:
Point.hpp
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1);
~Point();
Point (const Point &p2);
int getX();
int getY();
void setX(int x2);
void setY(int y2);
void print();
};Point.cpp
#include "Point.hpp"
Point::Point(int x1, int y1)
{
x = x1;
y = y1;
}
Point::Point (const Point &p2)
{
x = p2.x;
y = p2.y;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
void Point::setX(int x2)
{
x = x2;
}
void Point::setY(int y2)
{
y = y2;
}
void Point::print()
{
cout << "(" << x << "," << y << ")";
}--问题的一部分--我被困在上了
Part 2 [20 points]
Reading material:
Abstract classes and pure virtual methods:
https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/
---------------------------------------------------------------------------------------------------
Write an abstract class GeometricShape representing an abstract geometric
shape. The class will have the following public methods:
// Constructor: gets a coordinate. The purpose of the
coordinate depends on the specific shape
GeometricShape(Point coord)
// returns the area of the shape
// returns 0 as default. To be implemented in each
concrete shape
virtual float getArea() { return 0 ; }
// returns the perimeter of the shape
// returns 0 as default. To be implemented in each
concrete shape
virtual float getPerimeter() { return 0 ; }
// virtual method. To be implemented in each concrete
method
virtual void print() = 0 ;我的执行情况:
GeometricShape.hpp
#include <iostream>
#include "Point.hpp"
using namespace std;
class GeometricShape {
private:
Point point;
public:
// Parameterized Constructor
GeometricShape(Point coord);
virtual float getArea();
virtual float getPerimeter();
virtual void print() = 0;
};GeometricShape.cpp (,这是不会编译的东西)
#include <iostream>
#include "GeometricShape.hpp"
GeometricShape::GeometricShape(Point coord)
{
Point p = new Point(coord.getX(),coord.getY());
}
int main()
{
return 0;
}在Linux Ubunto 18.04.3 (远程访问的大学实验室,在实验室中编译所需的任务)上编译时的错误消息
错误消息:
gsg27@csil-cpu2:~/sfuhome/cmpt-125/5$ g++ GeometricShape.cpp
GeometricShape.cpp: In constructor ‘GeometricShape::GeometricShape(Point)’:
GeometricShape.cpp:9:11: error: conversion from ‘Point*’ to non-scalar type ‘Point’ requested
Point p = new Point(coord.getX(),coord.getY());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~发布于 2019-12-06 00:21:59
你眼前的问题是
Point p = new Point(coord.getX(),coord.getY());没有道理。new表达式返回指向动态分配对象的指针。所以,你写的代码应该是
Point *p = new Point(coord.getX(),coord.getY());但是,您不需要指针,也不需要动态分配的对象:您只是尝试初始化一个数据成员。所以你的构造函数应该是
GeometricShape::GeometricShape(Point coord) : point(coord) {}因为您已经拥有数据成员GeometricShape::point,并且Point类有一个副本构造函数。这样写起来会更平常
GeometricShape::GeometricShape(Point const &coord) : point(coord) {}因为你不需要复制两份原件,但这里没什么大不了的。
发布于 2019-12-06 00:37:27
在接受了上述所有帮助并咨询了我的教授之后:
它应该按以下方式实现,并且工作正常
#include <iostream>
#include "Point.hpp"
using namespace std;
class GeometricShape {
private:
Point point;
float area;
float perimeter;
public:
// Parameterized Constructor
GeometricShape(Point coord) : point(coord.getX(), coord.getY()) // initialize the data field
{
area = 0;
perimeter = 0;
}
virtual float getArea();
virtual float getPerimeter();
virtual void print() = 0;
}; 注意,因为它是一个抽象类,所以作为初学者,我觉得让所有的一个文件都不是hpp和cpp文件不那么让人费解。
https://stackoverflow.com/questions/59205063
复制相似问题