Class demonstration

This is a discussion on Class demonstration within the C++ Programming forums, part of the General Programming Boards category; hey guys, I have an assignment that has the following requirements: 1. Write a class Line which represents a line ...

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

    Class demonstration

    hey guys, I have an assignment that has the following requirements:
    1. Write a class Line which represents a line in 2D
    2. Two constructors: one with slope and intercept
    one with (x1, y1) (x2, y2)
    3. Member function findY(double) which for a given line
    takes an x-value and returns the y-value
    4. Write short main program which exercises the class and
    show output

    Below is what I did...I would appreciate any suggestions or comments about code, and if you think this is enough for the given requirements. Thanks,

    axon

    Code:
    /* Author: 
     * Class:  CS201
     * Date:   09.08.03
     * Assgn:  Homework 0
     * System: g++ and MSVC++
    */
    
    #include <iostream>
    using namespace std;
    
    class Line
    {
    public:
    	//constructors
    	Line( double newSlope, double newIntercept );
    	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;
    };
    
    //--------------------------------------
    
    int main()
    {
    	double x = 0; 
    	double y = 0;
    
    	Line slopeAndIntercept(1, 2), coordinates(2, 4, 3, -1); 
    	
    	slopeAndIntercept.output1();
    	coordinates.output2();
    
    	x = 12;
    	cout << "\nCalculating y-coordinate when x = 12: " << endl;
    	y = slopeAndIntercept.findY(x);
    	cout << "When x = 12, y = " << y << endl;
    
    	return 0;
    }
    
    //-----------------------------------
    //defining functions and constructors
    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)
    {
    	return (slope*x + intercept);
      
    }

    some entropy with that sink? entropysink.com

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

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I would consider not storing the x1, y1, x2, and y2 that you get from the second constructor. Instead, it would make more sense to me to use it to figure out the slope and y-intercept, then store those.

    With your current code, try calling coordinates.findY(x). It will give you a crazy number because you don't initialize slope or intercept in your second constructor. It is nice to see that you are using initialization lists, but you have to remember to initialize all member variables on construction, not just the ones passed in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21