In: Computer Science
hello i have question in c++ language
Q1: create a class called RightTriangleShape, and it has data member called height which initialized to 3 by the constructor of the class. It has also the following function members:
The functions from 3 to 6 have to print right-triangle shape of entered height.
#include <iostream>
using namespace std;
class RightTriangleShape
{
// Access specifier
public:
// Data Members
int height;
//Constructor and assigning height value to 3
RightTriangleShape()
{
height = 3;
}
// Setter
//Inputing value from user
void setHeight() {
cout << "Enter height value: ";
cin >> height;
if (height <= 0)
height = 3;
}
// Getter
//Printing the user entered value.
void getHeight() {
cout << "The height is " << height << "\n";
}
//Printing left bottom right triangle
void leftBottom_RTraingle() {
cout << "leftBottom_RTraingle:\n";
//Outer loop for counting rows from 1 to 5
for (int i = 1;i <= height;i++) {
//Inner loop for printing asterik values
for (int j = 1;j <= i;j++) {
cout << "* ";
}
cout << "\n";
}
}
void leftTop_RTraingle() {
cout << "leftTop_RTraingle:\n";
//This loop for counting rows from 5 to 1
for (int i = height;i >= 1;i--) {
//Inner loop for printing asterik values for i value
for (int j = 1;j <= i;j++) {
cout << "* ";
}
cout << "\n";
}
}
void RightTop_RTraingle() {
cout << "RightTop_RTraingle:\n";
//This loop for counting from 5 to 1
for (int i = height;i >= 1;i--) {
//This loop for printing spaces
for (int k = 1;k <= height - i;k++) {
//This will print two spaces
cout << " ";
}
//This loop for printing asterik
for (int j = 1;j <= i;j++) {
cout << "* ";
}
cout << "\n";
}
}
void RightBottom_RTraingle() {
//This loop for counting from 1 to 5
cout << "RightBottom_RTraingle:\n";
for (int i = 1;i <= height;i++) {
//This will print two spaces
for (int k = 1;k <= height - i;k++) {
cout << " ";
}
//This loop for printing asterik
for (int j = 1;j <= i;j++) {
cout << "* ";
}
cout << "\n";
}
}
};
int main()
{
//Creating objects
RightTriangleShape myObj,myobj1;
//With user input value
//If value is less than or equal to zero then value is set to
3
myObj.setHeight();
myObj.getHeight();
myObj.leftBottom_RTraingle();
myObj.leftTop_RTraingle();
myObj.RightTop_RTraingle();
myObj.RightBottom_RTraingle();
//Without user input value
myobj1.leftBottom_RTraingle();
myobj1.leftTop_RTraingle();
myobj1.RightTop_RTraingle();
myobj1.RightBottom_RTraingle();
return 0;
}
Sample Input and Output:
Enter height value: 7
The height is 7
leftBottom_RTraingle:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
leftTop_RTraingle:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
RightTop_RTraingle:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
RightBottom_RTraingle:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Sample Input and Output:
leftBottom_RTraingle:
*
* *
* * *
leftTop_RTraingle:
* * *
* *
*
RightTop_RTraingle:
* * *
* *
*
RightBottom_RTraingle:
*
* *
* * *
Sample Input and Output:
//If the value is less than zero or equal to zero then height
will set to 3.
Enter height value: -1
The height is 3
leftBottom_RTraingle:
*
* *
* * *
leftTop_RTraingle:
* * *
* *
*
RightTop_RTraingle:
* * *
* *
*
RightBottom_RTraingle:
*
* *
* * *
COMMENT DOWN BELOW FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ASNWER HELPS YOU.