In: Computer Science
Q2. Update the given code 2 to implement the following problem:
Code 2:
class triangle {
int base;
int height;
/* Write your code here
*/
};
int main(){
/*write your code here
}
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!