I read several similar posts, and I either did not understand what they had to offer, or they didn't seem to apply. I'm new here and trying my best to follow the rules.
We're learning c++ in the last 2 weeks of the course and its on the final in 40 hours :) , so I am a beginner. I am familiar with C.
Here is the part of the assignment that is relevant:
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()
My Implementation of:
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 << ")";
}
The part of the Question I am stuck on
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 ;
My Implementation of:
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 (this is what wont compile)
#include <iostream>
#include "GeometricShape.hpp"
GeometricShape::GeometricShape(Point coord)
{
Point p = new Point(coord.getX(),coord.getY());
}
int main()
{
return 0;
}
Error message when compiled on Linux Ubunto 18.04.3 (university lab remotely accessed, assignments required to compile on linux in the lab)
Error message:
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());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~