Item 3-Multi-file organization of the program for the third week

Source: Internet
Author: User
Tags cmath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.