Week 4 Task 2 -- triangle

Source: Internet
Author: User

[Cpp]/* 02. * copyright and version Declaration of the program part 03. * Copyright (c) 2012, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: object. cpp 06. * Author: Dong Wanpeng 07. * Completion Date: April 8, March 22, 2013. * version: v1.0 09. * input Description: None. 10. * Problem description: design a class that calculates the circumference and area of a triangle. 11. * program output: the circumference and area of the triangle are 12. */# include <iostream> # include <Cmath> using namespace std; class Triangle {public: inline void setA (double x); inline void setB (double y ); inline void setC (double z); // set the value of the three sides. Be sure to convert it into a triangle inline double getA (); inline double getB (); inline double getC (); bool isTriangle (); double perimeter (void); // calculate the triangle perimeter double area (void); // calculate and return the Triangle area private: double a, B, c; // The three sides are private member data}; inline void Triangle: setA (double x) {a = x;} inline void Triangle: setB (double y) {B = y;} inline void Triangle: setC (double z) {c = z;} bool Triangle: isTriangle () {if (a + B)> c) & (a + c)> B) & (B + c)> a) & (a-B) <c) & (a-c) <B) & (B-c) <a) return true; else return false;} inline double Triangle: getA () {return a;} inline double Triangle: getB () {return B;} inline double Triangle: getC () {return c;} double Triangle: perimeter () {return (a + B + c);} double Triangle: area () {double area, p; p = (a + B + c)/2; area = sqrt (p * (p-a) * (p-B) * (p-c); return area;} int main () {Triangle tri1; // define an instance (object) of the triangle class double x, y, z; cout <"Enter the three sides of the triangle :"; cin> x> y> z; tri1.setA (x); tri1.setB (y); tri1.setC (z); // if (tri1.isTriangle ()) {cout <"three sides:" <tri1.getA () <',' <tri1.getB () <',' <tri1.getC () <endl; cout <"Triangle perimeter:" <tri1.perimeter () <'\ t' <"Area:" <tri1.area () <endl ;} else cout <"cannot form a triangle" <endl; system ("pause"); return 0 ;}

Related Article

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.