Thread: getting results from different constructors

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    getting results from different constructors

    I'm having a problem with the thirs case of my program...I'm trying to get a value for y only from two sets of coordinates. This works fine in the first two cases since I'm calling findY from slopeAndIntercept...but when I get into the third case my constructors get mixed up...any help?

    Code:
    // THIS IS THE .H FILE
    #include <iostream>
    using namespace std;
    
    //File: Line.h
    
    class Line
    {
    public:
    	//constructors
    	Line( double newSlope = 0, double newIntercept = 0);
    	Line( double newx1, double newy1, double newx2, double newy2 );
    
    	void output1(); //output for slope and y-intercept
    	void output2(); //output for the coordinates
    	
    	double findY(double x); // find y-coord from given x-coord
    
    private:
    	double slope;
    	double intercept;
    	double x1, y1, x2, y2;
    };
    
    #include "line.cpp"
    Code:
    //LINE .CPP
    Line::Line( double newSlope, double newIntercept )
    		: slope(newSlope), intercept(newIntercept){}
    
    Line::Line( double newx1, double newy1, double newx2, double newy2 )
    		: x1(newx1), y1(newy1), x2(newx2), y2(newy2){}
    
    void Line::output1(){
    
    	cout << "Initializing slope of line to: " << slope << endl;
    	cout << "Initializing y-intercept to:   " << intercept << endl;
    }
    
    void Line::output2(){
    	cout << "\n( x1, y1 ) = ( " << x1 << ", " << y1 << " )" << endl;
    	cout << "( x2, y2 ) = ( " << x2 << ", " << y2 << " )" << endl;
    }
    
    double Line::findY(double x)
    {
    	if ( (slope != 0) && (intercept != 0) ){
    			return (slope*x + intercept);
    	} else {
    		slope = (y2 - y1)/(x2 - x1);
    		intercept = (y1 - slope*(x1));
    		return (slope*x + intercept);
    	}
      
    }
    Code:
    //MAIN.CPP
    #include "Line.h"
    
    int main()
    {
    	double x = 0; 
    	double y = 0;
        
    		cout << "\n----------<Case1>---------\n" << endl;
    
    		Line slopeAndIntercept(1, 2), coordinates(2, 4, 3, -1); 
    		
    		slopeAndIntercept.output1();
    		coordinates.output2();
    
    		x = 12;
    		cout << "\nCalculating y-coordinate when x = " << x << endl;
    		y = slopeAndIntercept.findY(x);
    		cout << "When x = " << x << " , y = " << y << endl;
    
    		cout << "\n----------<Case2>---------\n" << endl;
    
    		Line slopeAndIntercept1(4, -1), coordinates1(0, 0, 0, 0); 
    		
    		slopeAndIntercept1.output1();
    
    		x = 7;
    		cout << "\nCalculating y-coordinate when x = " << x << endl;
    		y = slopeAndIntercept1.findY(x);
    		cout << "When x = " << x << " , y = " << y << endl;
    
    		cout << "\n----------<Case3>---------\n" << endl;
    
    		Line slopeAndIntercept2(0, 0),coordinates2(0, 0, 1, 1); 
    		coordinates2.output2();		
    
    		x = 2;
    		cout << "\nCalculating y-coordinate when x = " << x << endl;
    		y = coordinates2.findY(x);
    		cout << "When x = " << x << " , y = " << y << endl;
    
    	return 0;
    
    }
    thanks, axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Do you ever actually initialize your slope and intercept in your "third case" ? Perhaps you want a function to calculate the slop and intercept based on the two points, initialize them within that function, and then call findY...or you could initialize them to zero by default (in your second constructor) and then check in findY whether they are 0 or not and calculate them if they are...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thats the whole point...I want to get the slope and the intercept from the two sets of coordinates, without initilizing the first constructor to a value different than 0....

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>without initilizing the first constructor to a value different than 0....

    Eh? I'm not sure what you mean...it should be pretty simple if you know the math you need...or is that the problem?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    ...mmmm...that definately is not the problem! if you don't have much to say except comments like that you should refrain from saying anything. Look in the code and you'll see that the math is fine

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> slope = (y2 - y1)/(x2 - x1);

    If x2-x1 evaluates to zero, your program will crash (a divide by zero exception will occur).

    Second, the solution to your problem is to calculate slope & intercept *within* the 4 parameter constructor, thus simplifying further calculation:


    Code:
    Line::Line( double newx1, double newy1, double newx2, double newy2 )
    		: x1(newx1), y1(newy1), x2(newx2), y2(newy2)
    {
     // prevent divide by zero!
     double valid_denominator = x2 - x1;
     slope = valid_denominator ? (y2 - y1)/(valid_denominator) : 0;
     intercept = (y1 - slope*(x1));
    }

    Will allow you to do:

    Code:
    double Line::findY(double x)
    {
     return (slope*x + intercept);	
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thanks sebastiani...that worked great! I got rid of the extra constructors that initialized the variables to 0. I think I wanted it to be more complicated then it really is...Thanks !

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  2. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM