Re-implement Project 2 with multi-file organization. Among them, the entire project consists of 3 files:
Main file: Main.cpp, which defines the main () function
Header files: triangle.h, header files, declaring classes, defining built-in member functions
Class definition file: Triangle.cpp, which defines other member functions in class triangle
Note that the 3 set functions and 3 get functions are designed as built-in member functions, and other functions are not used as built-in functions.
Main.cpp
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: March 21, 2015 * version number: v1.0*/#include <iostream > #include <cmath> #include <c:\users\mayuko2012\desktop\demo\triangle.h>using namespace Std;int main ( { Triangle tri1;//defines an instance (object) of a triangle class double x, y, Z; cout<< "Please enter three sides of the triangle:"; cin>>x>>y>>z; Tri1.seta (x); Tri1.setb (y); Tri1.setc (z);//For the three-side initial value if (Tri1.istriangle ()) { cout<< "three edges are:" <<tri1.geta () << ', ' <<TRI1.GETB () << ', ' <<tri1.getc () <<endl; cout<< "The perimeter of the triangle is:" << tri1.perimeter () << ' \ t ' << "area:" << tri1.area () <<endl; } else cout<< "cannot make a triangle" <<endl; return 0;}
Triangle.cpp
#include <c:\users\mayuko2012\desktop\demo\triangle.h>\\ A custom header file requires an absolute path #include<cmath>bool triangle:: Istriangle () { if (a+b>c && a+c>b && b+c>a) return true; else return false;} Double Triangle::p erimeter () { return (a+b+c);} Double Triangle::area () { double p,s; p= (A+B+C)/2; S=sqrt (p* (p-a) * (p-b) * (p-c)); return s;}
Triangle.h
#ifndef triangle_h_included#define triangle_h_includedclass triangle{public: void SetA (double x) { a=x; } ; void Setb (double y) { b=y; }; void SetC (double z) { c=z; }; Double Geta () { return A; }; Double Getb () { return b; }; Double GetC () { return C; }; Double perimeter (void);//Calculates the perimeter of a triangle with double area (void),//calculates and returns the square of the triangle, bool Istriangle ();//Determines whether the triangle is composed of private: double a,b,c;//three sides for private member data}; #endif//triangle_h_included
Run Pic:
@ Mayuko
Item 3-Multi-file organization of the program for the third week