3 Replies - 261 Views - Last Post: 08 February 2012 - 11:27 PM Rate Topic: -----

Topic Sponsor:

#1 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

C++:Objects and Classes

Posted 08 February 2012 - 03:42 AM

I am beginner to the OOP.
Did my answer fullfill what the question want?
Question:
Design a class named Triangle to represent a triangle.The class contain:
  • Three sets of points X and Y represented by two double values each,containing the three points that define a triangle.
  • A no-arg constructor that creates a default triangle with the points {1.0,1.0},{1.0,1.0},{1.0,1.0}
  • A constructor that creates a triangle from the specified three points
  • The accessor and mutator functions for all the data fields
  • A function named getArea() that returns the area of the triangle
  • A function named getPerimeter() that returns the length of the perimeter

Draw the UML diagram for the class.Implement the class.Write the test program that creates two Triangle objects.
Assign points{1,1},{5,2.5},{1,5} to the first object.
Assign points{-4.0,-3.0},{-1.5,-1.0},{1.2,-3.0} to the second object.
Display the properties of both objects.In addition,find and display their area and perimeter.


UML diagram:
_______________
Triangle
_______________
-x1:double
-y1:double
-x2:double
-y2:double
-x3:double
-y3:double
_______________
+Triangle()
+Triangle(newX1:double,newY1:double,newX2:double,newY2:double,newX3:double,newY3:double)
+getX1():double
+getY1():double
+getX2():double
+getY2():double
+getX3():double
+getY3():double
+getPerimeter():double
+getArea():double

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
using namespace std;

class Triangle
{
      public:
             Triangle()
             {  x1=1.0;
                y1=1.0;
                x2=1.0;
                y2=1.0;
                x3=1.0;
                y3=1.0;  }
              
             Triangle(double newX1,double newY1,double newX2,double newY2,double newX3,double newY3)
             {  x1=newX1;
                y1=newY1;
                x2=newX2;
                y2=newY2;
                x3=newX3;
                y3=newY3;   }
                
             double getX1()
             {   return x1;   }
             
             double getY1()
             {   return y1;   }
             
             double getX2()
             {   return x2;   }
             
             double getY2()
             {   return y2;   }
             
             double getX3()
             {   return x3;   }
             
             double getY3()
             {   return y3;   }
             
             double getPerimeter()
             {  
                double distance1=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
                double distance2=sqrt(pow(x3-x2,2)+pow(y3-y2,2));
                double distance3=sqrt(pow(x3-x1,2)+pow(y3-y1,2));
                return distance1+distance2+distance3;
             }
             
             double getArea()
             {
       
                   return abs((x1*(y2-y3)+x2*(y1-y3)+x3*(y1+y2))/2);
             }
      
      private:
              double x1,y1,x2,y2,x3,y3;
};

int main()
{
    Triangle triangle1(1,1,5,2.5,1,5);
    Triangle triangle2(-4.0,-3.0,-1.5,-1.0,1.2,-3.0);
    
    cout<<fixed<<setprecision(1); 
    cout<<"Triangle 1 has point("<<triangle1.getX1()<<","<<triangle1.getY1()<<") , ("<<triangle1.getX2()
      <<","<<triangle1.getY2()<<") and ("<<triangle1.getX3()<<","<<triangle1.getY3()<<")"<<endl;
    cout<<"The area is "<<triangle1.getArea()<<" and perimeter is "<<triangle1.getPerimeter()<<endl<<endl;
    
    cout<<"Triangle 2 has point("<<triangle2.getX1()<<","<<triangle2.getY1()<<") , ("<<triangle2.getX2()
      <<","<<triangle2.getY2()<<") and ("<<triangle2.getX3()<<","<<triangle2.getY3()<<")"<<endl;
    cout<<"The area is "<<triangle2.getArea()<<" and perimeter is "<<triangle2.getPerimeter()<<endl;
    
    system("PAUSE");
    return 0;
    
}
    


Is This A Good Question/Topic? 0
  • +

Replies To: C++:Objects and Classes

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 1893
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: C++:Objects and Classes

Posted 08 February 2012 - 06:18 AM

Do you have a question or problem with your code?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: C++:Objects and Classes

Posted 08 February 2012 - 06:25 AM

It's ok but I think they are looking more at something of a

class Triangle {
  private:
    double a[2];
    double b[2];
    double c[2];
};

or 

class Triangle {
  private:
    double points[2][3];
};



Or perhaps

struct Point {
    double x;
    double y;
};

class Triangle {
  private:
    Point a;
    Point b;
    Point c;
};

or

class Triangle {
  private:
    Point points[3];
};



Using C++11, you can even make the initialization look very much like the examples i.e.:

#include <iostream>

struct Point {
    double x;
    double y;
};

class Triangle {

  private:
    Point a;
    Point b;
    Point c;

  public:
    Triangle(Point new_a, Point new_b, Point new_c) : a(new_a), b(new_B), c(new_c) {}

};

int main() {
    Triangle triangle = {{1.0,1.0},{1.0,1.0},{1.0,1.0}};
}


Was This Post Helpful? 0
  • +
  • -

#4 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

Re: C++:Objects and Classes

Posted 08 February 2012 - 11:27 PM

Ok,Thank You!
I will improve it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1