Week five item two-object as data member

Source: Internet
Author: User

Problem

Recalling the data members of the Engineer class, there are glasses, backpacks and so on. A engineer glasses, backpack, is the object of glass, bag class. A data member in a class whose type can be a simple type or a class. In this way, some classes are combined into another class, which is used as one of the "parts."
This project designs a triangular class whose data members are no longer the edge length of the three-edged triangle, but the three vertices of the triangle. Using the Design triangle class, input the three vertices of the triangle, find out its area, perimeter, and determine whether it is right triangle and isosceles triangle.
Note: (1) This problem requires two classes, the vertex class refers to the CPoint class in Project 1, (2) The Triangle class refers to the following Ctriangle class declaration, (3) Make full use of the CPoint class in the existing code implementation, (4) about three side length of processing, you can add three private properties, The backup is obtained at the time of initialization. Of course, you can also calculate when you need to use it.

Code

#include <iostream> #include <cmath>using namespace Std;class cpoint{private:double x;  horizontal double y;  Ordinate Public:cpoint (double xx=0,double yy=0);   Double Distance1 (CPoint p) const;  Distance between two points void input (); Enter the coordinate point void output () in X, y form; Output coordinate point in (x, y)};class ctriangle{public:ctriangle (CPoint &x,cpoint &y,cpoint &z): A (x), B (y), C (Z) {}// Gives the three-point constructor void Settriangle (CPoint &x,cpoint &y,cpoint &z);//double perimeter (void);//computes the perimeter of the Triangle double area ( void);//calculate and return the area of the triangle bool Isrighttriangle (); Whether it is right triangle bool Isisoscelestriangle (); Whether it is isosceles triangle Private:cpoint a,b,c; };  Cpoint::cpoint (double xx,double yy) {x=xx; Y=yy;}  void Cpoint::input () {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 specifications, please re-enter \ n"; }}void Cpoint::output () {cout<< "(" <<x<< "," <<y<< ")" &LT;&LT;ENDL;}  Double CPoint::d Istance1 (CPoint p) const{double D; D=sqrt ((p.x-x) * (p.x-x) + (p.y-y) * (p.y-y)); return D;}  void Ctriangle::settriangle (CPoint &x,cpoint &y,cpoint &z) {a=x;  B=y; C=z;}  Double ctriangle::p erimeter (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); return (A + B + c);}  Double Ctriangle::area (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B);  Double s = (A + B + c)/2; return sqrt (S * (s-a) * (s-b) * (S-C));}  BOOL Ctriangle::isrighttriangle () {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B);  Double max=a;  if (B>max) max=b;  if (C>max) max=c; if (((max==a) && (a*a==b*b+c*c)) | | ((max==b) && (b*b==a*a+c*c)) | |    ((max==c) && (c*c==b*b+a*a)))  return true; else return false;}  BOOL Ctriangle::isisoscelestriangle () {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); if (ABS (a) <1e-7) | | (ABS (B-C) <1e-7) | |    (ABS (C-A) <1e-7))  return true; else return false;}    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;}


Operation Result:

Summary of Knowledge points:

Classes in class

Learning experience:

Good study Day Day up

Week five item two-object as data member

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.