Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:12:25+00:00 2026-06-08T17:12:25+00:00

I want to calculate area and perimeter of rects using the following code: rect

  • 0

I want to calculate area and perimeter of rects using the following code:

    rect a;
    a = ( -----
          !   !
          -----a );
std::cout << a.area() << std::endl;
std::cout << a.perimeter() << std::endl;

For this purpose I crafted the following class:

class rect
{
public:
    rect():w(0), h(2) {}
    rect& operator - () { w += 0.5f; return *this; }
    rect& operator - (rect&) { w += 0.5f; return *this; }
    rect& operator -- (int a) { w += a; return *this; }
    rect& operator -- () { w += 1; return *this; }
    rect& operator ! () { h += 0.5f; return *this; }
    void clear() { w = 0; h = 2; }
    int area() { return w * h; }
    int perimeter() { return 2 * w + 2 * h; }
    int width() { return w; }
    int height() { return h; }
private:
    float w;
    float h;
};

Here are some usage examples:

#include <iostream>

int main()
{
    rect a;

    a = ( -----
          !   !
          -----a );

    std::cout << a.area() << std::endl;
    std::cout << a.perimeter() << std::endl;
    std::cout << a.width()  << std::endl;
    std::cout << a.height() << std::endl;

    std::cout << std::endl;

    a.clear();

    a = ( ----------
          !        !
          !        !
          !        !
          !        !
          ---------a );

    std::cout << a.area() << std::endl;
    std::cout << a.perimeter() << std::endl;
    std::cout << a.width()  << std::endl;
    std::cout << a.height() << std::endl;

    return 0;
}

Here are my questions:

  1. Can it be done without involving any floating point arithmetic?
    (indeed, it is an integer grid)
  2. Can it be generalized on a 3D case? I.e:

    cuboid b;
    b = (  ---------
          /        /!
         ! -------! !
         !        ! !
         !        ! !
         !        !/
         ---------b );
    
    std::cout << b.volume() << std::endl;
    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:12:27+00:00Added an answer on June 8, 2026 at 5:12 pm

    I had to change the ‘/’ operator to be ‘+’, Since there is no prefix ‘/’ operator. + works great though. Oh and it uses ints. I only tested it once with the case you provided but as far as I could tell it should work.

    class cuboid
    {
            int w,h,l;
    public:
            cuboid () : w(2), h(3), l(6) {}
            cuboid& operator - () { w += 1; return *this; }
            cuboid& operator - (cuboid&) { w += 1; return *this; }
            cuboid& operator -- (int) { w += 2; return *this; }
            cuboid& operator -- () { w += 2; return *this; }
            cuboid& operator ! () { h += 1; return *this; }
            cuboid& operator + () { l += 1; return *this; }
            cuboid& operator + (cuboid&) { l += 1; return *this; }
            cuboid& operator ++ () { l += 2; return *this; }
            cuboid& operator ++ (int) { l += 2; return *this; }
    
            void clear () { w = 2; h = 3; l = 6; }
            int width () const { return w / 3; }
            int height () const { return h / 3; }
            int length () const { return l / 3; }
            int volume () const { return width() * height () * length (); }
            int surface_area() const { return width() * height () * 2 +
                                              width() * length () * 2 +
                                              length() * height () * 2; }
    };
    

    See it in action.
    http://ideone.com/vDqEm

    Edit: You don’t need the ++ operators since there shouldn’t be two +’s next to each other. Woops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an area map using rect coordinates to highlight tables of a floor
I know that I can calculate the display area for a given font using
So my question is on this: This program is supposed to calculate the area
I want to calculate the area in square kilometers of a polygon in Google
I want to calculate distance in my custom repository class. The problem is that
I want to calculate the smallest integer with exactly k bits set, that is
We want to calculate the value of an integral in linear plot. For a
I want to calculate a new date by adding a number of months to
I want to calculate total memory used by a process in .net. Total memory
I want to calculate the sum of 1/1 + 1/2 + 1/3 + ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.