I try to make a good OOP (obj oriented prog) project.
my program is an MFC program but I want to talk about the code itself
Well, the Idea is to take as base class "Shape"
"Shape" has some pure virtual functions.
now I have a problem, I dont no how to set the hierarchy ..
Shape is the abstract class
now every Shape is made with Points.
Point
Line (2 Points)
Circle ( 1 Point + radius)
Triangle (3 Points or 3 Lines ??)
Quad ( 4 Points or 4 Lines ?? + Quad has its own VIRTUAL functions)
Square ( inherites from Quad)
and so on...
now I also use operator overloading, so it become difficult to use the Virtual function (like draw()) ...
Im so confuse, here is what I made so far, I know its a mess but I will be great to get some Tips from you to make it smooth!
Code:
#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point {
public:
int x, y;
Point() {
x = 0;
y = 0;
}
Point(int a,int b) {
x = a;
y = b;
}
};
#endif POINT_H
Code:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.h"
#include "Point.h"
#include <iostream>
using namespace std;
double const PI = 3.1412;
class Circle : public Shape {
private:
Point p1;
int radius;
public:
Circle(Point p, int r) {
p1.x = p.x;
p1.y = p.y;
radius = r;
}
~Circle();
void setX(int xcor) { p1.x = xcor; }
void setY(int ycor) { p1.y = ycor; }
int getX() const { return p1.x; }
int getY() const { return p1.y; }
int getR() const { return radius; }
virtual double area() const{ return PI * radius * radius; }
virtual double perimeter() const { return PI * 2 * radius; }
virtual void draw() const {
cout << "Circle: center(" << getX() << "," << getY() <<
") with Radius(" << getR() << ")" << endl;
}
};
#endif CIRCLE_H
Code:
#ifndef SHAPE_H
#define SHAPE_H
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual void draw() const = 0;
};
#endif SHAPE_H
Code:
#ifndef LINE_H
#define LINE_H
#include "Shape.h"
#include "Point.h"
#include <iostream>
#include <math.h>
using namespace std;
class Line : public Shape {
protected:
Point X;
Point Y;
public:
Line(Point p1, Point p2)
{
X.x = p1.x;
X.y = p1.y;
Y.x = p2.x;
Y.y = p2.y;
}
Point getX() const { return X; };
Point getY() const { return Y; };
void setX(Point newX) { X.x = newX.x; X.y = newX.y; }
void setY(Point newY) { Y.x = newY.x; Y.y = newY.y; }
virtual double area() const { return 0.0; }
virtual void draw() const {
cout << "Line: (" << getX().x << "," << getX().y << ") to (" << getY().x << "," << getY().y << ")" << endl;
}
virtual double perimeter() const { return sqrt((Y.x - X.x) + (Y.y - X.y)); }
};
#endif LINE_H