Thread: Project help needed

  1. #1
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90

    Project help needed

    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

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    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!
    What are you confused about?

  3. #3
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    well I dont know if what Im doing is ok ...
    here when I try to make Quads...

    Code:
    #ifndef QUAD_H
    #define QUAD_H
    #include "Line.h"
    #include <iostream>
    
    
    using namespace std;
    
    
    class Quad : public Line {
    protected:
    	Line l1, l2, l3, l4;
    public:
    	Quad(Line a, Line b, Line c, Line d) {
    		l1 = a;
    		l2 = b;
    		l3 = c;
    		l4 = d;
    	}
    	~Quad();
    	virtual void setl1(Line newl) { l1 = newl; }
    	virtual void setl2(Line newl) { l2 = newl; }
    	virtual void setl3(Line newl) { l3 = newl; }
    	virtual void setl4(Line newl) { l4 = newl; }
    	virtual Line getl1() const { return l1; }
    	virtual Line getl2() const { return l2; }
    	virtual Line getl3() const { return l3; }
    	virtual Line getl4() const { return l4; }
    	virtual double area() const = 0;
    	virtual double perimeter() = 0;
    	virtual void draw() const {
    		cout << "QUAD" << endl;
    	}
    	friend ostream& operator<<(ostream &os, const Quad &obj) {
    		os << obj.getl1() << "," << obj.getl2() << ","
    			<< obj.getl3() << "," << obj.getl4() << endl;
    		return os;
    	}
    };
    
    
    #endif QUAD_H

  4. #4
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    I dont know If my setteres and getters are ok, if I build this class ok, its all seem so confusing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partner needed for project
    By C_ntua in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 08-19-2010, 11:48 AM
  2. HELP needed in VC++ Project!!!
    By yeya in forum Windows Programming
    Replies: 0
    Last Post: 09-20-2006, 04:32 AM
  3. Help Needed with project
    By adgav in forum Linux Programming
    Replies: 1
    Last Post: 03-03-2006, 12:13 PM
  4. help needed regarding my project
    By mitto123 in forum C Programming
    Replies: 2
    Last Post: 02-02-2006, 01:04 AM