The Project 2 is implemented in the form of "multiple files per project", where the declarations of two classes are placed in the same. h file, each of which has a file for the member function of each class, and a file for the main () function. Appreciate the advantages of this arrangement.
Class.h
#ifndef class_h_included#define class_h_includedclass 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 () as x, Y, and /or output the coordinate point in (x, y)};class ctriangle{public: ctriangle (CPoint &x,cpoint & Y,cpoint &z): A (X), B (Y), C (Z) {}; The constructor for three points is given void Settriangle (CPoint &x,cpoint &y,cpoint &z);// float perimeter (void);// Calculates the circumference of the Triangle float area (void),//calculates and returns the square of the triangle's size bool Isrighttriangle ();//Whether it is right triangle bool Isisoscelestriangle (); is isosceles triangle private: CPoint a,b,c;//Three vertices}; #endif//class_h_included
Class.cpp
#include <iostream> #include <cmath> #include "class.h" using namespace std; 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;}
Main.cpp
/** 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> #include "class.h" using namespace Std;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;}
@ Mayuko
Week five project 3-programs that organize multiple classes with multiple files