"Item 3-Programs that organize multiple classes with multiple files"
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.
Main function:
#include <iostream> #include <cmath> #include "need.h" using namespace Std;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;}
Header file Need.h established
#ifndef need_h_included#define need_h_includedclass cpoint{private: double x; Horizontal double y; Ordinate public: CPoint (double xx=0,double yy=0); Double Distance1 (CPoint p) const; 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) { A=b.distance1 (c), B=c.distance1 (A), C=a.distance1 (b); The constructor for three points } 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, bool Isrighttriangle ();//Whether it is right triangle bool Isisoscelestriangle (); is isosceles triangle private: CPoint a,b,c;//three vertex double a,b,c;//triangle three side}; #endif//need_h_included
#include <iostream> #include <cmath> #include "need.h" using namespace std; Cpoint::cpoint (double xx,double yy) { x=xx; Y=yy;} Double CPoint::D Istance1 (CPoint p) The distance between the const//two points (point is the current point-think of this?, another point is P) { return sqrt ((p.x-x) * (p.x-x) + (p.y-y ) * (p.y-y));} void Cpoint::input () //Enter the coordinate point in X, y form { 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 specification, please re-enter \ n"; }} void Cpoint::output ()//output coordinate point { cout<< "(" <<x<< "," <<y<< ") in (x, y)" <<ENDL;}
#include <iostream> #include <cmath> #include "need.h" using namespace Std;void ctriangle::settriangle ( CPoint &x,cpoint &y,cpoint &z)//{ a=x; b=y; C=z; A=b.distance1 (C), B=c.distance1 (A), C=a.distance1 (B); Float Ctriangle::p erimeter (void)//Calculate the perimeter of the triangle { return (A + B + c);} float Ctriangle::area (void)//calculates and returns the area of the triangle { double q= (A + B + c)/2; return sqrt (q* (q-a) * (q-b) * (q-c)); BOOL Ctriangle::isrighttriangle ()//Is Right triangle { 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 { if (ABS (a) <1e-7) | | (ABS (B-C) <1e-7) | | (ABS (C-A) <1e-7)) Double type data cannot be directly compared to the size of return true; else return false;}
By learning about the past, this will be easy.
Third week Item 33 Corner multi-file processing