Question

(i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions...

(i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions that calculate the perimeter (4 * length) and the area (length*length) of the triangle. Also provide set and get functions for the length attribute. The set function should verify that length is between 0.0 to 100. Your class should also provide a display function that draws the aquare using * character. For example, if length = 5, the display function should draw

            *****

            *****

            *****

            *****

            *****

   example filled square

when creating the display function, keep in mind that the length can vary and isn’t necessarily equal to 5 or any other number. Class definition and implementations should be in a header file (.h file) and source file(.cpp).

(ii) Write a main program that tests your square class. Create at least two objects, demonstrate set , get and display functions properly work. Submit you code and a sample run of your code.

(iii) Extend the class with methods that allow only the perimeter to be drawn and add a bool parameter that can be set to say if the square is filled or not and add a constructor to build either a filled on not filled square.

            *****

            *      *

            *      *

            *      *

            *****

   example unfilled square

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This formula is for square not triangle .. There is mistake in our post.

Here is code:

#include <iostream>

using namespace std;

class Square

{

private:

int length = 5;

public:

Square(int l)

{

if (l > 0 && l < 100)

length = l;

else

cout << "Can't set the length" << endl;

}

int

getLength()

{

return length;

}

int

area()

{

return (getLength() * getLength());

}

int

perimeter()

{

return (4 * getLength());

}

void

draw()

{

for (int i = 0; i < getLength(); i++) {

for (int j = 0; j < getLength(); j++) {

cout << "*";

}

cout << endl;

}

}

void

drawPerimeter()

{

int l = getLength();

for (int i = 0; i < l; i++) {

for (int j = 0; j < l; j++) {

if (i == l - 1 || i == 0 || j == 0 || j == l - 1)

cout << "*";

else

cout << " ";

}

cout << endl;

}

}

};

int

main()

{

Square s1(6);

Square s2(12);

cout << "Length is " << s1.getLength() << ", Area is : " << s1.area() << ", Parameter is : " << s1.perimeter()

<< endl;

cout << "Length is " << s2.getLength() << ", Area is : " << s2.area() << ", Parameter is : " << s2.perimeter()

<< endl;


s1.draw();

cout << endl;

s1.drawPerimeter();

return 0;

}

Output:

Length is 6, Area is 36, Parameter is : 24 Length is 12, Area is 144, Parameter is :4 なななななな なななななな なななななな なななななな なななななな なななな

> Hello did you use constructors and destructors

esorprincess Thu, Feb 3, 2022 9:20 AM

> How can I do this for string class not square

esorprincess Thu, Feb 3, 2022 9:25 AM

Add a comment
Know the answer?
Add Answer to:
(i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions...
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
  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • Create a class called Skateboard. Skateboard has 3 private member variables and 3 public member functions.(C++,...

    Create a class called Skateboard. Skateboard has 3 private member variables and 3 public member functions.(C++, Visual Studios) Skateboard has the following 3 private member variables: 1) the brand of the skateboard (such as “Sector 9”)       2) the model of the skateboard (such as “Hot Steppa”) 3) the length of the skateboard in inches (such as “22”) Skateboard has the following 3 public member functions: a member function named print which does not have any return value or input...

  • Need solution in c++ that works on both Microsoft Visual 2017 and Xcode: Create a class...

    Need solution in c++ that works on both Microsoft Visual 2017 and Xcode: Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point number larger than 0.0 and less than 20.0. Write a program to test class Rectangle

  • Create a class named Game using the following UML class diagram. GAME -gameName : string +Game()...

    Create a class named Game using the following UML class diagram. GAME -gameName : string +Game() +setGameName(in name : string) : void +getGameName() : string +displayMessage() : void This class has 2 member variables namely playerName and playerScore. The class contain following member functions: Constructor, set function, get functions, display function. Code write in 3 files: Game.h header file, funtion.cpp file and main.cpp file. The output should be: Player John has scored 50 points.

  • using python 3: Program 14: Class with Private Attribute Collection of Objects: modRoom.py Step 1: Create...

    using python 3: Program 14: Class with Private Attribute Collection of Objects: modRoom.py Step 1: Create a class called Room as indicated by the UML shown below: Student -length: integer width: integer -height integer -roomNum: integer -roomType: String -room Capacity: integer <<constructor Room (tmpRoomNum:integer tmpRoomType: String, tmpCapacity:integer tmplength: integer, tmpWidth: integer, tmpHeight: integer) +get Length(): Integer +getWidth(): Integer +getHeight(): Integer +get RoomNum(): Integer +getRoom Type(): String +getRoomCapacity(): Integer tRoomType(): String +set +setRoomCapacity(): Integer +calcArea(): Integer +calcSurfaceArea(): nteger +str( String should...

  • Exercise 1: Create a class Resource. The class should have: a) Two private variables status and w...

    C++C++ Exercise 1: Create a class Resource. The class should have: a) Two private variables status and writeTo representing integer value either 0 or 1. b) One default constructor that initializes the status and writeTo to zero. c) One single parameterized constructor to initialize the writeTo variable. d) Two constant accessor functions per class that return the values of status e) Two mutator functions per class that set the values of status and writeTo One output (member) function that outputs...

  • Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...

    Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw, GetArea, GetDimensions, GetHeight, GetLength, GetSurfaceArea, GetVolume Members: height, length, width – all of type double Don’t actually write function implementations, just define the classes as you would in a header file. Use inheritance as appropriate. Use keywords such as “const” and “virtual” as appropriate. Make functions and members public, private, or protected as appropriate. Don’t forget constructors and destructors. Some classes may be abstract,...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

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