Question

In: Computer Science

Q2. Update the given code 2 to implement the following problem: Create a class triangle Use...

Q2. Update the given code 2 to implement the following problem:

  • Create a class triangle
  • Use dynamic memory allocation to create 10 objects of the class triangle
  • Set default height and base of all created objects (triangle) to 5 and 10 respectively using a constructor
  • Ask user to change only the height of the 5th triangle
  • Print height and base of all (10) triangles to demonstrate that you did the task correctly
  • Deallocate the 5th object
  • Print height and base of all (10) objects of the class triangle to demonstrate that you deallocated the 5th object correctly

Code 2:

class triangle {

int base;

int height;

/* Write your code here

*/

};

int main(){

/*write your code here

}

Solutions

Expert Solution

Explanation:

10 objects are created dynamically and then traversed using for loop. Height is asked from the user and is stored into the 5th object height variable.

Code:


#include <iostream>

using namespace std;

class triangle {

public:
int base;

int height;


triangle()
{
base = 5;
height = 10;
}


};

int main(){

triangle** arr = new triangle*[10];
  
for(int i=0; i<10; i++)
{
arr[i] = new triangle();
}
  
  
cout<<"Enter the height of 5th object: ";
cin>>arr[4]->height;
  
for(int i=0; i<10; i++)
{
cout<<arr[i]->base<<" "<<arr[i]->height<<endl;
}
  
delete arr[4];
cout<<endl;
  
for(int i=0; i<10; i++)
{
cout<<arr[i]->base<<" "<<arr[i]->height<<endl;
}
  
  
  


}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!


Related Solutions

In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.
In java Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.+------------------------------+| Circle |+------------------------------+| - radius : double = 1.0 |+------------------------------+| + Circle(radius : double) || + getRadius() : double || + setRadius(radius : double) || + area() : double || + perimeter() : double |+------------------------------+The following information might also be useful:Formula for area of a circle with radius r:       A = πr^2Formula for...
Check errors and revise/update this java code below and update with OOp class; at least make...
Check errors and revise/update this java code below and update with OOp class; at least make two classes, use java library, validate user input, format and create UML/Pseudocode and flowchart for the code. import java.util.Scanner; public class TestScore {    public static void main(String[] args) {        String firstName;        String lastName;        int numTest;        int i;        int score;        double totalScore;        double avgScore;        String grade;        Scanner input = new Scanner(System.in);        System.out.println("Enter First Name");        firstName = input.nextLine();        System.out.println("Enter Last Name");        lastName = input.nextLine();        System.out.println("How many...
Use a style sheet to define the following rules and implement the given HTML code. Please...
Use a style sheet to define the following rules and implement the given HTML code. Please put your style information within the same file as the HTML code. Rules • Hyperlinks using the nodec class should display no decoration. • Hyperlinks should display text in white with a green background color when the mouse pointer is held over the link. (use the hover pseudo-class) • Unordered lists not nested within any other lists should be displayed in blue text and...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT