Round 3, Heads Up
Posted: Tue Feb 11, 2003 2:16 pm
Third Program, three files, two are headers and one is main. Objective is to create an object that uses class Point to create a line. Here is class point:<br><br>// point.h<br>#ifndef POINT_H<br>#define POINT_H<br>#include <iostream.h><br>// Point class declaration.<br>class Point { <br>        private:<br>                int _x, _y;<br>        public: <br>                Point() { _x = _y = 0;} <br>                Point(int x, int y) <br>                {<br>                        _x = x; <br>                        _y = y; <br>                } <br>                ~Point() {} <br>                void setPoint(int x,int y) {_x = x; _y = y;} <br>                void setX(int x){_x = x;} <br>                void setY(int y){_y = y;} <br>                Point getPoint(){return *this;}<br>                int getX(){return _x;}<br>                int getY(){return _y;}<br>                void showPoint(){<br>                cout << "Point: X = " << _x << "; Y = " << _y << endl;<br>                } <br>}; <br>#endif<br><br><br>Here is the class I have to make, Line:<br><br>#ifndef LINE_H<br>#define LINE_H<br>#include <iostream.h><br>#include <math.h><br>#include "point.h"<br>//Line Class declaration<br>class Line <br>{<br>        private:<br>                Point beginning;<br>                Point ending;<br>                double length;<br>                void calcLength ();<br>        public:<br>                //Constructors<br>                Line ();<br>                Line (int, int);<br>                Line (int, int, int, int);<br>                void setPoints (int, int, int, int);<br>                void setPoints (Point, Point);<br>                void setBeginningPoint (int, int);<br>                void setBeginningPoint (Point);<br>                Point getBeginningPoint ();<br>                void setEndingPoint (int, int);<br>                void setEndingPoint (Point);<br>                Point getEndingPoint ();<br>                double getLength ();<br>                void showLine ();<br>};<br>#endif<br>/****************************************************************/<br>Line::Line ()<br>{<br>        beginning.setPoint (0, 0);<br>        ending.setPoint (0, 0);<br>        length = 0;<br>}<br>/****************************************************************/<br>Line::Line (int endx, int endy)<br>{<br>        beginning.setPoint (0, 0);<br>        ending.setPoint (endx, endy);<br>        calcLength ();<br>}<br>/****************************************************************/<br>Line::Line (int beginx, int beginy, int endx, int endy)<br>{<br>        beginning.setPoint (beginx, beginy);<br>        ending.setPoint (endx, endy);<br>        calcLength ();<br>}<br>/****************************************************************/<br>void Line::setPoints (int beginx, int beginy, int endx, int endy)<br>{<br>        beginning.setX (beginx);<br>        beginning.setY (beginy);<br>        ending.setX (endx);<br>        ending.setY (endy);<br>}<br>/****************************************************************/<br>void Line::setPoints (Point begin, Point end)<br>{<br>        beginning = begin;<br>        ending = end;<br>}<br>/****************************************************************/<br>void Line::setBeginningPoint (int beginx, int beginy)<br>{<br>        beginning.setX (beginx);<br>        beginning.setY (beginy);<br>}<br>/****************************************************************/<br>void Line::setBeginningPoint (Point begin)<br>{<br>        beginning = begin;<br>}<br>/****************************************************************/<br>Point Line::getBeginningPoint ()<br>{<br>        beginning.getPoint ();<br>}<br>/****************************************************************/<br>void Line::setEndingPoint (int endx, int endy)<br>{<br>        ending.setX (endx);<br>        ending.setY (endy);<br>}<br>/****************************************************************/<br>void Line::setEndingPoint (Point end)<br>{<br>        ending = end;<br>}<br>/****************************************************************/<br>Point Line::getEndingPoint ()<br>{<br>        ending.getPoint ();<br>}<br>/****************************************************************/<br>double Line::getLength ()<br>{<br>        return length;<br>}<br>/****************************************************************/<br>void Line::showLine ()<br>{<br>        cout << "Beginning " << beginning.showPoint;<br>        cout << "Ending " << ending.showPoint;<br>}<br>/****************************************************************/<br>void Line::calcLength ()<br>{<br>        double difX = 0;<br>        double difY = 0;<br>        difX = ending.getX - beginning.getX;<br>        difY = ending.getY - beginning.getY;<br>        length = sqrt( pow(difX, 2) + pow(difY, 2));<br>}<br>/****************************************************************/<br><br>Just so you can see the entire program, here is main:<br><br>// lineMain.cpp<br>#include <iostream.h><br>#include "point.h"<br>#include "line.h"<br>void main()<br>{<br>        double sumLength;<br>        Line l1,l2(1,1,4,5),l3(3,4),l4,l5,l6,l7;<br>        cout.precision(2);<br>        cout.setf(ios::fixed|ios::showpoint);<br>        cout << "This program will create lines.\n\n";<br>        cout << "This is line 1\n";<br>        l1.showLine();<br>        cout << "This is line 2\n";<br>        l2.showLine();<br>        cout << "This is line 3\n";<br>        l3.showLine();<br>        l4.setPoints(1,1,4,5);<br>        cout << "This is line 4\n";<br>        l4.showLine();<br>        l5.setBeginningPoint(2,2);<br>        l5.setEndingPoint(5,6);<br>        cout << "This is line 5\n";<br>        l5.showLine();<br>        l6.setPoints(l2.getBeginningPoint(),l2.getEndingPoint());<br>        cout << "This is line 6\n";<br>        l6.showLine();<br>        l7.setBeginningPoint(l5.getBeginningPoint());<br>        l7.setEndingPoint(l5.getEndingPoint());<br>        cout << "This is line 7\n";<br>        l7.showLine();<br>        sumLength = l1.getLength() + l2.getLength() + l3.getLength() +<br>                                l4.getLength() + l5.getLength() + l6.getLength() +<br>                                l7.getLength();<br>        cout << "The sum of the line lengths is: " << sumLength << endl;<br>}<br><br><br>Current problems are in the final two functions defined in Line.h:<br><br>error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)<br>error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)<br>error C2296: '-' : illegal, left operand has type 'int (__thiscall Point::*)(void)'<br>error C2297: '-' : illegal, right operand has type 'int (__thiscall Point::*)(void)'<br>error C2296: '-' : illegal, left operand has type 'int (__thiscall Point::*)(void)'<br>error C2297: '-' : illegal, right operand has type 'int (__thiscall Point::*)(void)'<br>Error executing cl.exe.<br> <p></p><i></i>