Project 1 in week 3-triangle type 1 and project triangle in week 3
# Include <iostream> # include <Cmath> using namespace std; class Triangle {public: void setABC (double x, double y, double z); // set the value of three sides, note that you must create a triangle void getABC (double * x, double * y, double * z); // obtain the value of double perimeter (void) on the three sides ); // 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 }; int main () {Triangle tri1; // defines an instance (object) of the Triangle class tri1.setABC (, 6); // values double x, y, and z for the three sides; tri1.getABC (& x, & y, & z); // assign values of the three sides x, y, and z to cout. <"the three sides are: "<x <'\ t' <y <' \ t' <z <endl; cout <" the triangle perimeter is: "<tri1.perimeter () <'\ t' <" Area: "<tri1.area () <endl; return 0 ;} // define the member functions in the Triangle class below
My answer:
/* Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Chen Danni * Completion Date: July 15, March 25, 2015 * version number: v1.0 * Problem description: design a triangle class below, please give the definition of each member function * input description: enter the number of three double types. * Output result: the length and area of the double type are output */# include <iostream> # include <cmath> using namespace std; class Triangle {public: void setABC (double x, double y, double z); void getABC (double * x, double * y, double * z); double perimeter (void); double area (void); private: double, b, c ;}; int main () {Triangle t; t. setABC (4,5, 6); double x, y, z; t. getABC (& x, & y, & z); cout <"three sides: "<x <'\ t' <y <' \ t' <z <endl; cout <" Triangle perimeter: "<t. perimeter () <'\ t' <"Area:" <t. area () <endl; return 0;} void Triangle: setABC (double x, double y, double z) {a = x; B = y; c = z ;} void Triangle: getABC (double * x, double * y, double * z) {* x = a; * y = B; * z = c;} double Triangle :: perimeter (void) {double sum; sum = a + B + c; return sum;} double Triangle: area (void) {double s, p; p = (a + B + c)/2; s = sqrt (p * (p-a) * (p-B) * (p-c )); return s ;}
Experience: I believe I will be better. Come on !!