[Cpp]/*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Liu Qingyuan
* Completion date: January 1, May 21, 2013
* Version: v1.0
* Input Description: None
* Problem description: Point class derived line class
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
# Include <iostream>
# Include <Cmath>
Using namespace std;
Class Point // defines the coordinate Point class
{
Public:
Point (): x (0), y (0 ){};
Point (double x0, double y0): x (x0), y (y0 ){};
Double getX ()
{
Return x;
}
Double getY ()
{
Return y;
}
Void PrintPoint (); // output point information
Private:
Double x, y; // the abscissa and ordinate of a point
};
Void Point: PrintPoint ()
{
Cout <"Point :(" <x <"," <y <")"; // output Point
}
Class Line: public Point // uses the coordinate Point class to define a Line class. The data member of the base class represents the midpoint of the Line.
{
Public:
Line (Point pts, Point pte); // constructor, which uses the two endpoints of the initialization Line and the midpoint described by the base-class data members.
Double Length (); // calculates and returns the Length of a straight line.
Void PrintLine (); // two endpoints of the output line and the line length
Private:
Class Point pts, pte; // two endpoints of a straight line
};
// Constructor, which respectively uses the two endpoints of the initialization line and the midpoint described by the basic data member (attribute)
Line: Line (Point pt1, Point pt2): Point (pt1.getX () + pt2.getX ()/2, (pt1.getY () + pt2.getY ()/2)
{
Pts = pt1;
Pte = pt2;
}
Double Line: Length () // calculates and returns the Length of a straight Line.
{
Double d;
Double dx = pts. getX ()-pte. getX ();
Double dy = pts. getY ()-pte. getY ();
D = sqrt (dx * dx + dy * dy );
Return d;
}
Void Line: PrintLine ()
{
Cout <"1st ";
Pts. PrintPoint ();
Cout <"\ n 2nd ";
Pte. PrintPoint ();
Cout <"\ n The Length of Line:" <Length () <endl;
}
Int main ()
{
Point ps (-2, 5), pe (7, 9 );
Line l (ps, pe );
L. PrintLine (); // output line l Information
Cout <"\ n The middle point of Line :";
L. PrintPoint (); // outputs the point information of the line l.
Return 0;
}
/*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Liu Qingyuan
* Completion date: January 1, May 21, 2013
* Version: v1.0
* Input Description: None
* Problem description: Point class derived line class
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
# Include <iostream>
# Include <Cmath>
Using namespace std;
Class Point // defines the coordinate Point class
{
Public:
Point (): x (0), y (0 ){};
Point (double x0, double y0): x (x0), y (y0 ){};
Double getX ()
{
Return x;
}
Double getY ()
{
Return y;
}
Void PrintPoint (); // output point information
Private:
Double x, y; // the abscissa and ordinate of a point
};
Void Point: PrintPoint ()
{
Cout <"Point :(" <x <"," <y <")"; // output Point
}
Class Line: public Point // uses the coordinate Point class to define a Line class. The data member of the base class represents the midpoint of the Line.
{
Public:
Line (Point pts, Point pte); // constructor, which uses the two endpoints of the initialization Line and the midpoint described by the base-class data members.
Double Length (); // calculates and returns the Length of a straight line.
Void PrintLine (); // two endpoints of the output line and the line length
Private:
Class Point pts, pte; // two endpoints of a straight line
};
// Constructor, which respectively uses the two endpoints of the initialization line and the midpoint described by the basic data member (attribute)
Line: Line (Point pt1, Point pt2): Point (pt1.getX () + pt2.getX ()/2, (pt1.getY () + pt2.getY ()/2)
{
Pts = pt1;
Pte = pt2;
}
Double Line: Length () // calculates and returns the Length of a straight Line.
{
Double d;
Double dx = pts. getX ()-pte. getX ();
Double dy = pts. getY ()-pte. getY ();
D = sqrt (dx * dx + dy * dy );
Return d;
}
Void Line: PrintLine ()
{
Cout <"1st ";
Pts. PrintPoint ();
Cout <"\ n 2nd ";
Pte. PrintPoint ();
Cout <"\ n The Length of Line:" <Length () <endl;
}
Int main ()
{
Point ps (-2, 5), pe (7, 9 );
Line l (ps, pe );
L. PrintLine (); // output line l Information
Cout <"\ n The middle point of Line :";
L. PrintPoint (); // outputs the point information of the line l.
Return 0;
}