Question
number 4 in c++ please
Laboratory work 12 Investigation of using classes for describing class data-members Task 1. Study class members definition in

square method not mentioned

no sauare method was given. can thus be solved without a specific square mthod?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

4)

triangle.h

#include<math.h>

class Triangle{
private:
float x1,y1; //A
float x2,y2; //B
float x3,y3; //C

public:
//Default constructor
Triangle(){
}
//-----------------------
//Parameterized constructor
Triangle(float x1,float y1,float x2,float y2,float x3,float y3){
this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;
this->x3=x3;
this->y3=y3;
}
//-----------------------
//initialize the coordinates
void setValue(float x1,float y1,float x2,float y2,float x3,float y3){
this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;
this->x3=x3;
this->y3=y3;
}
//----------------------------
//destructor
~Triangle(){
delete(this); //delete the object
}
//-----------------------
//finds the perimeter of the triangle
float perimeter(){
float AB;
float AC;
float BC;
float P;
//length of each three sides of the triangle
AB=sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
AC=sqrt((x3-x1)*(x3-x1) + (y3-y1)*(y3-y1));
BC=sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));

P=AB+AC+BC;
return P;
}
//------------------------------------
//find the area of the circle
float area(){
float AB;
float AC;
float BC;
float S;
float A;
//length of each three sides of the triangle
AB=sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
AC=sqrt((x3-x1)*(x3-x1) + (y3-y1)*(y3-y1));
BC=sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));
//Heron's formula to find the area of the triangle
S=(AB+AC+BC)/2;
A=sqrt(S*(S-AB)*(S-AC)*(S-BC));
return A;

}

};
//---------------------------------------------

triangle.cpp

#include<iostream>
#include "triangle.h"

using namespace std;

int main(){
Triangle *T=new Triangle(3,5,12,30,50,67);
cout<<"\nArea of the triangle= "<<T->area()<<" unit square"<<endl;
cout<<"Perimeter of the triangle= "<<T->perimeter()<<" unit"<<endl<<endl;

return 0;
}
//-----------------------------

OUTOUT:

Area of the triangle- 308.5 unit square Perineter of the triangle- 157.409 unit

Add a comment
Know the answer?
Add Answer to:
number 4 in c++ please square method not mentioned no sauare method was given. can thus...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ please 27. How do you differentiate a destructor from the rest? 28. Can a static...

    C++ please 27. How do you differentiate a destructor from the rest? 28. Can a static function call an instance one? 29. Can an instance function call a static one? 30. How many objects are created? class Account { public: string firstName; string lastName; double balance; Account* p; 31. Discuss the differences among the following access modifiers, public, private, and protected. 32. When do you use the following notation? . (dot notation) :: (scope resolution operator) -> (arrow notation) 33....

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • This exercise guides you through the process of converting an Area and Perimeter application from a...

    This exercise guides you through the process of converting an Area and Perimeter application from a procedural application to an object-oriented application. Create and use an object 1. Open the project named ch04_ex2_Area and Perimeter that’s stored in the ex_starts folder. Then, review the code for the Main class. 2. Create a class named Rectangle and store it in the murach.rectangle package. 3. In the Rectangle class, add instance variables for length and width. The, code the get and set...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes...

    Computer Science 111 Introduction to Algorithms and Programming: Java Programming Net Beans Project #4 - Classes and Objects (15 Points) You will create 3 new classes for this project, two will be chosen (THE TWO CHOSEN ARE TENNIS SHOE AND TREE) from the list below and one will be an entirely new class you invent. Here is the list: Cellphone Clothes JuiceDrink Book MusicBand Bike GameConsole Tree Automobile Baseball MusicPlayer Laptop TennisShoe Cartoon EnergyDrink TabletComputer RealityShow HalloweenCostume Design First Create...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT