Skip to content

Instantly share code, notes, and snippets.

@avipars
Last active April 24, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avipars/c89e7e15a7d6dc240e7ff38dbed22015 to your computer and use it in GitHub Desktop.
Save avipars/c89e7e15a7d6dc240e7ff38dbed22015 to your computer and use it in GitHub Desktop.
OOP
/*
Avraham Parshan -
Course: C++ Workshop
Excersice: 1, Week 7
Date: 4/7/2022
*/
#include "Point.h"
#include <cmath> //sqrt function
// constructors
Point::Point() : x(0), y(0) {} // initialize empty constructor
Point::Point(const float &myX, const float &myY) : x(myX), y(myY) {} // we made a coordinate with 2 points
//copy constructor
Point::Point(const Point &other): x(other.x), y(other.y){}
// setters
void Point::setX(float myX)
{
x = myX;
}
void Point::setY(float myY)
{
y = myY;
}
// getters
float Point::getX() const
{
return x;
}
float Point::getY() const
{
return y;
}
//distance between two points, comes in handy for other classes (such as shapes) - AKA DISTANCE - my naming scheme is more clear in my opinion, so I elected to keep it
float Point::calculateDistance(Point &otherPoint) const //i passed a reference to the other point so it doesn't need to copy the point over
{
// point distance formula
//x2 - x1 + y2 - y1
float left = pow((otherPoint.getX() - x), 2); //square both parts
float right = pow((otherPoint.getY() - y), 2);
return sqrt(left + right); // and square root the whole equation to get distance
}
// overloaded extraction operator
istream &operator>>(istream &is, Point &pt)
{
char garbage; // to get rid of the comma and parenthesis
float myX,myY; // to get the x and y values
is >> garbage >> myX >> garbage >> myY >> garbage; // get the x and y values
pt.setX(myX); // set the x value
pt.setY(myY); // set the y value
return is;
}
/*
sample run is in main.cpp
*/
/*
Avraham Parshan -
Course: C++ Workshop
Exercise: 1, Week 7
Date: 4/11/2022
*/
#include "Rectangle.h"
Rectangle::Rectangle() : Shape(4) {} // rect with 4 points
//checks if rectangle = square
bool Rectangle::isSpecial() const
{
//need to check that all 4 sides are equal first
float A = points[0].calculateDistance(points[1]); //a to b
float B = points[1].calculateDistance(points[2]); //b to c
float C = points[2].calculateDistance(points[3]); //c to d
float D = points[3].calculateDistance(points[0]); //d to a
//it might not look clean, but I opted to go this route because CPP uses short-circuit eval, if the first case is false, it will not check the second - which is good for efficiency
if((A == B) && (B == C) && (C == D) && (D == A)) //check if all 4 sides are equal - can also be a rhombus/diamond
{
//then if diagonals have the same length (or could have checked the angles, but this is more straightforward)
float AC = points[0].calculateDistance(points[2]);
float BD = points[1].calculateDistance(points[3]);
return (AC == BD); //if and only if all 4 sides are equal and the diagonals are equal, it is a square!
}
else
{
return false; //if not a square, return false
}
}
//square = rectangle, then print
void Rectangle::printSpecial() const
{
if (isSpecial()) //only print if it is in fact special!
{
cout << "Square with side length " << points[0].calculateDistance(points[1]) << endl; //all sides are equal, so can pick a side arbitrarily
}
}
// returns the area of triangle
float Rectangle::area() const
{
//width * length = area
float width = points[0].calculateDistance(points[1]);
float length = points[1].calculateDistance(points[2]);
return width * length; //if its a square, the width and length are the same (Side^2)
}
/*
sample run is in main.cpp
*/
/*
Avraham Parshan -
Course: C++ Workshop
Exercise: 1, Week 7
Date: 4/11/2022
*/
#include "Shape.h"
//rule of 5
Shape::Shape() : points(nullptr), numOfPoints(0) {} // empty constructor values are initialized inline
Shape::Shape(const int &numPts) : points(nullptr), numOfPoints(numPts) // standard constructor
{
cout << "Enter values of " << numOfPoints << " points:" << endl;
points = new Point[numOfPoints]; // dynamic allocation of new points
for(int i = 0; i < numOfPoints; ++i) //fill the point with user input
{
cin >> points[i]; //input the points via parent class (x,y) - istream
}
}
//deep copy
Shape::Shape(const Shape &other) : numOfPoints(other.numOfPoints) // copy constructor
{
if(numOfPoints <= 0) { //if there are no points - return and don't copy
return;
}
points = new Point[numOfPoints]; // new points array
for (int i = 0; i < numOfPoints; ++i) // cycle through to copy X and Y
{
points[i] = other.points[i]; // copy the points
}
}
//shallow copy
Shape::Shape(Shape &&other) : points(other.points), numOfPoints(other.numOfPoints) // move ctor
{
other.numOfPoints = 0; //moving the other shape to 0
other.points = nullptr; //moving the other shape to nullptr
}
Shape::~Shape() // destructor - will go to child one - because of the virtual declaration
{
if (points != nullptr) //no need to free something that never existed
{
delete points; //free the points array
numOfPoints = 0;
points = nullptr;
}
}
//setter
void Shape::setNumOfPoints(int numPoints)
{
(numPoints >= 0) ? numOfPoints = numPoints : numOfPoints = 0; //if numPoints is negative, set to 0
}
//getter
int Shape::getNumOfPoints() const
{
return numOfPoints;
}
//output overloaded operator
ostream &operator<<(ostream &os, const Shape &shape)
{
os << "points: "; //output the points
for(int i = 0; i < shape.numOfPoints; ++i)
{
os << '(' << shape.points[i].getX() << "," << shape.points[i].getY() << ')'; //(x,y)
os << ' '; //space for next point
}
return os;
}
/*
sample run is in main.cpp
*/
/*
Avraham Parshan -
Course: C++ Workshop
Exercise: 1, Week 7
Date: 4/10/2022
*/
#include "Point.h"
#pragma once
class Shape
{
protected:
// class fields - are inherited by all shapes
Point *points;
int numOfPoints;
public:
Shape(); // empty constructor
Shape(const int &numPts); // constructor with number of points
Shape(const Shape &); // copy constructor
Shape(Shape &&); // move ctor
virtual ~Shape(); // virtual destructor
//setter
void setNumOfPoints(int);
//getter
int getNumOfPoints() const;
//output operator
friend ostream &operator<<(ostream &, const Shape &);
//pure virtual functions - must be implemented in child classes
virtual float area() const = 0;
virtual bool isSpecial() const = 0;
virtual void printSpecial() const = 0;
};
/*
sample run is in main.cpp
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment