| | |
Passing Class Objects, Instances
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
Passing Class Objects, Instances
--------------------------------------------------------------------------------
I have a project creating a cartesian point, then a line segment, then a line comparison. Each has its own class .h and .cpp file. Main just starts the program to CartesianPoint test, in the point class. My question is after I create an instance of the point class and create a point, say P1, how can I then pass this object, (an instance of the point class) to the LineSegment class, to find slope and such. Line inherits LineSegment which inherits CartesianPoint. I have no idea what I'm doing.
Thanks
Heer's the code so far. I need help passing a CartesianPoint object to LineSegment, and I need help learning how to manipulate it. I believe this is called aggregation and/or composition, I have spent three days now just to find the terms of what I need to learn how to do. Instead of learning how to do it.
------------------------------------------------------
include <iostream>
#include "CartesianPoint.h"
#include "LineSegment.h"
#include "Line.h"
#include "stdafx.h"
using namespace std;
double main()
{
CartesianPoint::test();
LineSegment::test();
return (0);
} // end main
-----------------------------------------
#ifndef CARTESIANPOINT_H
#define CARTESIANPOINT_H
#include "stdafx.h"
class CartesianPoint {
public:
CartesianPoint();
CartesianPoint(double xx, double yy);
double getx();
double gety();
void set(double xx, double yy);
static void test();
void print();
//void print(double pOne, double pTwo, double pThree, double pFour);
virtual ~CartesianPoint();
protected:
double x;
double y;
}; // end clas CartesianPoint
#endif
--------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
#include "stdafx.h"
#include "CartesianPoint.h"
#include "LineSegment.h"
#include "Line.h"
using namespace std;
CartesianPoint::CartesianPoint()
{
x = 0;
y = 0;
}
CartesianPoint::CartesianPoint(double xx, double yy)
{
x = xx;
y = yy;
}
CartesianPoint p1;
CartesianPoint p2;
CartesianPoint p3;
CartesianPoint p4;
void CartesianPoint::test(){
p1.set(1.2,2.2);
p2.set(8.2,68.2);
p3.set(9.8,50.2);
p4.set(11.2,6.2);
p1.print();
p2.print();
p3.print();
p4.print();
p1.print();
}
void CartesianPoint::set(double xx,double yy)
{
x = xx;
y = yy;
}
double CartesianPoint::getx()
{
double temp;
temp = x;
return temp;
}
double CartesianPoint::gety()
{
double temp;
temp = y;
return temp;
}
void CartesianPoint::print()
{
cout<<"The point is: ("<<getx()<<", "<<gety()<<")"<<endl<<endl;
}
CartesianPoint::~CartesianPoint()
{
}
--------------------------------------------
#ifndef LINESEGMENT_H
#define LINESEGMENT_H
#include "CartesianPoint.h"
#include "Line.h"
class CartesianPoint;
//class Line;
class LineSegment : public CartesianPoint {
public:
LineSegment(); // default constructor
LineSegment(CartesianPoint V1, CartesianPoint V2);
LineSegment(CartesianPoint V1, CartesianPoint V2,
CartesianPoint V3, CartesianPoint V4);
double getMidPoint();
CartesianPoint midPoint();
double getPerpendicularBisector();
double perpendicularBisector();
double getSlope();
void printLine();
static void test();
virtual ~LineSegment();
protected:
double m;
double b;
CartesianPoint p1;
CartesianPoint p2;
CartesianPoint p3;
CartesianPoint p4;
// double m1;
// double m2;
// double slopeLine1;
// double slopeLine2;
// Cartesian X1;
// Cartesian X2;
// Cartesian Y1;
// Cartesian Y2;
}; // end clas LineSegment
#endif
---------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
#include "stdafx.h"
#include "CartesianPoint.h"
#include "LineSegment.h"
using namespace std;
LineSegment::LineSegment()
{
m = 0;
b = 0;
}
LineSegment::LineSegment(CartesianPoint V1, CartesianPoint V2)
{
p1 = V1;
p2 = V2;
m = 0;
b = 0;
}
LineSegment::LineSegment(CartesianPoint V1, CartesianPoint V2,
CartesianPoint V3, CartesianPoint V4)
{
p1 = V1;
p2 = V2;
p3 = V3;
p4 = V4;
m = 0;
b = 0;
}
CartesianPoint p1;
// LineSegment L1(CartesianPoint(1,1), CartesianPoint(1,1);
void LineSegment::test()
{
}
double LineSegment::getSlope()
{
double temp;
temp = m;
return temp;
}
void LineSegment::printLine()
{
}
LineSegment::~LineSegment()
{
}
-----------------------------------------------------
--------------------------------------------------------------------------------
I have a project creating a cartesian point, then a line segment, then a line comparison. Each has its own class .h and .cpp file. Main just starts the program to CartesianPoint test, in the point class. My question is after I create an instance of the point class and create a point, say P1, how can I then pass this object, (an instance of the point class) to the LineSegment class, to find slope and such. Line inherits LineSegment which inherits CartesianPoint. I have no idea what I'm doing.
Thanks
Heer's the code so far. I need help passing a CartesianPoint object to LineSegment, and I need help learning how to manipulate it. I believe this is called aggregation and/or composition, I have spent three days now just to find the terms of what I need to learn how to do. Instead of learning how to do it.
------------------------------------------------------
include <iostream>
#include "CartesianPoint.h"
#include "LineSegment.h"
#include "Line.h"
#include "stdafx.h"
using namespace std;
double main()
{
CartesianPoint::test();
LineSegment::test();
return (0);
} // end main
-----------------------------------------
#ifndef CARTESIANPOINT_H
#define CARTESIANPOINT_H
#include "stdafx.h"
class CartesianPoint {
public:
CartesianPoint();
CartesianPoint(double xx, double yy);
double getx();
double gety();
void set(double xx, double yy);
static void test();
void print();
//void print(double pOne, double pTwo, double pThree, double pFour);
virtual ~CartesianPoint();
protected:
double x;
double y;
}; // end clas CartesianPoint
#endif
--------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
#include "stdafx.h"
#include "CartesianPoint.h"
#include "LineSegment.h"
#include "Line.h"
using namespace std;
CartesianPoint::CartesianPoint()
{
x = 0;
y = 0;
}
CartesianPoint::CartesianPoint(double xx, double yy)
{
x = xx;
y = yy;
}
CartesianPoint p1;
CartesianPoint p2;
CartesianPoint p3;
CartesianPoint p4;
void CartesianPoint::test(){
p1.set(1.2,2.2);
p2.set(8.2,68.2);
p3.set(9.8,50.2);
p4.set(11.2,6.2);
p1.print();
p2.print();
p3.print();
p4.print();
p1.print();
}
void CartesianPoint::set(double xx,double yy)
{
x = xx;
y = yy;
}
double CartesianPoint::getx()
{
double temp;
temp = x;
return temp;
}
double CartesianPoint::gety()
{
double temp;
temp = y;
return temp;
}
void CartesianPoint::print()
{
cout<<"The point is: ("<<getx()<<", "<<gety()<<")"<<endl<<endl;
}
CartesianPoint::~CartesianPoint()
{
}
--------------------------------------------
#ifndef LINESEGMENT_H
#define LINESEGMENT_H
#include "CartesianPoint.h"
#include "Line.h"
class CartesianPoint;
//class Line;
class LineSegment : public CartesianPoint {
public:
LineSegment(); // default constructor
LineSegment(CartesianPoint V1, CartesianPoint V2);
LineSegment(CartesianPoint V1, CartesianPoint V2,
CartesianPoint V3, CartesianPoint V4);
double getMidPoint();
CartesianPoint midPoint();
double getPerpendicularBisector();
double perpendicularBisector();
double getSlope();
void printLine();
static void test();
virtual ~LineSegment();
protected:
double m;
double b;
CartesianPoint p1;
CartesianPoint p2;
CartesianPoint p3;
CartesianPoint p4;
// double m1;
// double m2;
// double slopeLine1;
// double slopeLine2;
// Cartesian X1;
// Cartesian X2;
// Cartesian Y1;
// Cartesian Y2;
}; // end clas LineSegment
#endif
---------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
#include "stdafx.h"
#include "CartesianPoint.h"
#include "LineSegment.h"
using namespace std;
LineSegment::LineSegment()
{
m = 0;
b = 0;
}
LineSegment::LineSegment(CartesianPoint V1, CartesianPoint V2)
{
p1 = V1;
p2 = V2;
m = 0;
b = 0;
}
LineSegment::LineSegment(CartesianPoint V1, CartesianPoint V2,
CartesianPoint V3, CartesianPoint V4)
{
p1 = V1;
p2 = V2;
p3 = V3;
p4 = V4;
m = 0;
b = 0;
}
CartesianPoint p1;
// LineSegment L1(CartesianPoint(1,1), CartesianPoint(1,1);
void LineSegment::test()
{
}
double LineSegment::getSlope()
{
double temp;
temp = m;
return temp;
}
void LineSegment::printLine()
{
}
LineSegment::~LineSegment()
{
}
-----------------------------------------------------
> I have no idea what I'm doing.
Clearly. You have serious design problems by using inheritance instead of containment for this project. It's much simpler if CartesianPoint (aside from being more concisely named) is a self-contained class, then Line and LineSegment contain CartesianPoint (henceforth to be referred to as Point) objects:
Much easier, and more intuitive.
Clearly. You have serious design problems by using inheritance instead of containment for this project. It's much simpler if CartesianPoint (aside from being more concisely named) is a self-contained class, then Line and LineSegment contain CartesianPoint (henceforth to be referred to as Point) objects:
C++ Syntax (Toggle Plain Text)
class Point { int _x, _y; public: Point(int x, int y); void set(int x, int y); int getx() const; int gety() const; } class Line { Point _start, _finish; public: Line(const Point& start, const Point& finish); void print() const; } // etc...
![]() |
Similar Threads
- Passing arrays of objects to functions (C++)
- Array of Class Objects? (C++)
- Passing Class Objects, Instances (C)
- Problem Passing a Class Object to a Queue (C++)
Other Threads in the C++ Forum
- Previous Thread: Programming development, help needed!
- Next Thread: pixel scanning
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





