Point class derived line class

Source: Internet
Author: User

[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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.