Recalling the data members of the Engineer class, there are glasses, backpacks and so on. A engineer glasses, backpack, is the object of glass, bag class. A data member in a class whose type can be a simple type or a class. In this way, some classes are combined into another class, which is used as one of the "parts."
This project designs a triangular class whose data members are no longer the edge length of the three-edged triangle, but the three vertices of the triangle. Using the Design triangle class, input the three vertices of the triangle, find out its area, perimeter, and determine whether it is right triangle and isosceles triangle.
Note: (1) This problem requires two classes, the vertex class refers to the CPoint class in Project 1, (2) The Triangle class refers to the following Ctriangle class declaration, (3) Make full use of the CPoint class in the existing code implementation, (4) about three side length of processing, you can add three private properties, The backup is obtained at the time of initialization. Of course, you can also calculate when you need to use it.
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: April 08, 2015 * version number: v1.0*/#include <iostream > #include <cmath>using namespace std;class cpoint{private:double x; horizontal double y; Ordinate Public:cpoint (double xx=0,double yy=0); Double Distance1 (CPoint p) const; Distance between two points void input (); Enter the coordinate point void output () in X, y form; Output the coordinate point in (x, y)};class ctriangle{public:ctriangle (CPoint &x,cpoint &y,cpoint &z): A (x), B (y), C (Z) {};//given Three-point constructor void Settriangle (CPoint &x,cpoint &y,cpoint &z);//float perimeter (void);//Calculate the perimeter of the triangle float is A (void);//calculates and returns the area of the triangle bool Isrighttriangle (); Whether it is right triangle bool Isisoscelestriangle (); Whether it is isosceles triangle Private:cpoint a,b,c; Three vertices}; Cpoint::cpoint (Double xx,double yy): X (xx), Y (yy) {};d ouble CPoint::d istance1 (CPoint p) const{double s; S=sqrt ((p.x-x) * (p.x-x) + (p.y-y) * (p.y-y)); return s;} void Cpoint::input () {char ch; cout<< "Please input the point (x, y):"; WHile (1) {cin>>x>>ch>>y; if (ch== ', ') break; cout<< "wrong style,please input Agein." <<endl; }}void Cpoint::output () {cout<< "(" <<x<< "," <<y<< ")" <<ENDL;} void Ctriangle::settriangle (CPoint &x,cpoint &y,cpoint &z) {a=x; B=y; C=z;} Float Ctriangle::p erimeter (void)//Calculate the perimeter of the triangle {double s1,s2,s3; S1=a.distance1 (B); S2=b.distance1 (C); S3=c.distance1 (A); return (S1+S2+S3);} float Ctriangle::area (void)//calculates and returns the area of the triangle {double p,s; Double s1,s2,s3; S1=a.distance1 (B); S2=b.distance1 (C); S3=c.distance1 (A); p= (S1+S2+S3)/2; S=sqrt (p* (P-S1) * (P-S2) * (P-S3)); return s;} The following comes from old bool Ctriangle::isrighttriangle ()//Whether it is right triangle {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double max=a; if (B>max) max=b; if (C>max) max=c; if ((max==a) && (ABS (A*A-B*B-C*C) <1e-7) | | ((max==b) && (ABS (B*B-A*A-C*C) <1e-7)) | | (MAX==C) && (ABS (C*C-B*B-A*A) <1e-7)) return true; else return false;} BOOL Ctriangle::isisoscelestriangle ()//is isosceles triangle {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); if (ABS (a) <1e-7) | | (ABS (B-C) <1e-7) | | (ABS (C-A) <1e-7)) return true; else return false;} int main ()//test data, from old {CPoint X (2,5), Y (5,2), Z (7,8); Ctriangle Tri1 (x, y, z); Defines an instance (object) of a triangle class cout<< "the circumference of the triangle is:" <<tri1.perimeter () << ", Area:" <<tri1.area () <<endl <<endl; cout<< "The Triangle" << (Tri1.isrighttriangle ()? " Yes ":" not ") <<" right triangle "<<endl; cout<< "The Triangle" << (Tri1.isisoscelestriangle ()? " Yes ":" not ") <<" isosceles triangle "<<endl; return 0;}Operation Result:
! Judge whether it constitutes right triangle!
BOOL Ctriangle::isrighttriangle ()//Is Right triangle { double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double max=a; if (B>max) max=b; if (C>max) max=c; if ((max==a) && (ABS (A*A-B*B-C*C) <1e-7) | | ((max==b) && (ABS (B*B-A*A-C*C) <1e-7)) | | ((max==c) && (ABS (C*C-B*B-A*A) <1e-7))) return true; else return false;}
! Judge whether it constitutes isosceles triangle!
BOOL Ctriangle::isisoscelestriangle ()//is isosceles triangle { double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); if (ABS (a) <1e-7) | | (ABS (B-C) <1e-7) | | (ABS (C-A) <1e-7)) return true; else return false;}
@ Mayuko
Fifth week item 2-object as data member