Problem
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.
Code
#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 coordinate point in (x, y)};class ctriangle{public:ctriangle (CPoint &x,cpoint &y,cpoint &z): A (x), B (y), C (Z) {}// Gives the three-point constructor void Settriangle (CPoint &x,cpoint &y,cpoint &z);//double perimeter (void);//computes the perimeter of the Triangle double area ( void);//calculate and return the area of the triangle bool Isrighttriangle (); Whether it is right triangle bool Isisoscelestriangle (); Whether it is isosceles triangle Private:cpoint a,b,c; }; Cpoint::cpoint (double xx,double yy) {x=xx; Y=yy;} void Cpoint::input () {char ch; cout<< "Please enter the coordinate point (format x, y):"; while (1) {cin>>x>>ch>>y; if (ch== ', ') break; cout<< "Input data format does not conform to specifications, please re-enter \ n"; }}void Cpoint::output () {cout<< "(" <<x<< "," <<y<< ")" <<ENDL;} Double CPoint::d Istance1 (CPoint p) const{double D; D=sqrt ((p.x-x) * (p.x-x) + (p.y-y) * (p.y-y)); return D;} void Ctriangle::settriangle (CPoint &x,cpoint &y,cpoint &z) {a=x; B=y; C=z;} Double ctriangle::p erimeter (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); return (A + B + c);} Double Ctriangle::area (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double s = (A + B + c)/2; return sqrt (S * (s-a) * (s-b) * (S-C));} BOOL Ctriangle::isrighttriangle () {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) && (a*a==b*b+c*c)) | | ((max==b) && (b*b==a*a+c*c)) | | ((max==c) && (c*c==b*b+a*a))) return true; else return false;} BOOL Ctriangle::isisoscelestriangle () {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 () {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:
Summary of Knowledge points:
Classes in class
Learning experience:
Good study Day Day up
Week five item two-object as data member