Class Object Composition printing out values problem
May 21, 2011 at 6:20pm UTC
Line2D Points is made up of Point2D x and y
Line2D.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifndef LINE2D__H
#define LINE2D__H
#include <iostream>
#include <fstream>
#include <string.h>
#include <ostream>
#include <sstream>
#include <cstdio>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include "Point2D.h"
using namespace std;
class Line2D
{
friend ostream& operator <<(ostream&, const Line2D&);
private :
//Declarations
Point2D pt1;
Point2D pt2;
protected :
//Declarations
double length;
//Functions
void setLength();
public :
//Constructor
Line2D();
Line2D(Point2D,Point2D);
//Get Methods
Point2D getPt1();
Point2D getPt2();
double getScalarValue();
//Set Methods
void setPt1(Point2D);
void setPt2(Point2D);
};
#endif
Line2D.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <iostream>
#include <fstream>
#include <ostream>
#include <iomanip>
#include <string.h>
#include <sstream>
#include <cstdio>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <math.h>
#include "Line2D.h"
#include "Point2D.h"
using namespace std;
Line2D::Line2D()
{
}
Line2D::Line2D(Point2D pt1, Point2D pt2)
{
this ->pt1=pt1;
this ->pt2=pt2;
setLength();
length = getScalarValue();
}
void Line2D::setLength()
{
length = sqrt( pow(pt1.getX()-pt2.getX(),2) + pow(pt1.getY()-pt2.getY(),2) );
}
Point2D Line2D::getPt1()
{
return pt1;
}
Point2D Line2D::getPt2()
{
pt2.getX();
pt2.getY();
return pt2;
}
void Line2D::setPt1(Point2D pt1)
{
this ->pt1=pt1;
setLength();
}
void Line2D::setPt2(Point2D pt2)
{
this ->pt2=pt2;
setLength();
}
double Line2D::getScalarValue()
{
return length;
}
ostream& operator <<(ostream &out, const Line2D &l2d)
{
out << l2d.pt1 << l2d.pt2 << l2d.length << endl;
return out;
}
Point2D.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#ifndef POINT2D__H
#define POINT2D__H
#include <iostream>
#include <ostream>
#include <fstream>
#include <string.h>
#include <sstream>
#include <cstdio>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctype.h>
using namespace std;
class Point2D
{
friend ostream& operator <<(ostream&, const Point2D&);
private :
protected :
//Declarations
int x;
int y;
double distFrOrigin;
//Function
void setDistFrOrigin();
public :
//Constructor
Point2D();
Point2D(int ,int );
//Set Methods
void setX(int );
void setY(int );
//Get Methods
int getX();
int getY();
double getScalarValue();
};
#endif
Point2D.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <iostream>
#include <ostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <sstream>
#include <cstdio>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <math.h>
#include "Point2D.h"
using namespace std;
//Constructor
Point2D::Point2D()
{
x=0;
y=0;
}
Point2D::Point2D(int x, int y)
{
this ->x=x;
this ->y=y;
setDistFrOrigin();
}
//Set Method
void Point2D::setX(int x)
{
this ->x=x;
setDistFrOrigin();
}
void Point2D::setY(int y)
{
this ->y=y;
setDistFrOrigin();
}
//Get Method
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
double Point2D::getScalarValue()
{
return distFrOrigin;
}
//Function
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
ostream& operator <<(ostream &out, const Point2D &p2d)
{
out << "[" << setw(4) << p2d.x << "," << setw(4) << p2d.y << "]" << setw(3) << "" << p2d.distFrOrigin << endl;
return out;
}
Main.cpp Partial Codes
1 2 3 4
Point2D p1(-9,-9);
Point2D p2(-99,-99);
Line2D l(p1,p2);
cout << l.getPt1();
I get this output:
[ -9, -9] 12.728
What's wrong here?
I just wanted to get Point1 Value, which is -9, -9
but it gave me Point2D's distFrOrigin too.
How can I solve this?
I'm stuck at this for quite awhile.
Need help and advise
Thanks in advance!
Topic archived. No new replies allowed.