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

  • SEARCH
  • Home
  • 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 7562525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:25:43+00:00 2026-05-30T13:25:43+00:00

The following compiled code is showing the resulting area of zero. Some how the

  • 0

The following compiled code is showing the resulting area of zero. Some how the width and height variables are still remaining as zero even though we set it using the base constructor.

#include <iostream> 
using namespace std;

class Shape {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      virtual int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0)
      {
          Shape(a, b); 
      }
      int area ()
      { 
          cout << "Rectangle class area :" <<endl;
          return (width * height); 
      }
};
class Triangle: public Shape{
    public:
        Triangle( int a=0, int b=0)
        {
            Shape(a, b); 
        }
        int area ()
        { 
            cout << "Rectangle class area :" <<endl;
            return (width * height / 2); 
        }
};
// Main function for the program
int main( )
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle  tri(10,5);

    // store the address of Rectangle
    shape = &rec;
    // call rectangle area.
    cout << shape->area() << endl;

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   cout << shape->area() << endl;

   return 0;
}

Output:
Rectangle class area :
0
Rectangle class area :
0

Trying to find out why the area is zero and how to make the pgm to print the actual area ?

  • 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-05-30T13:25:44+00:00Added an answer on May 30, 2026 at 1:25 pm

    The correct syntax is:

    Rectangle( int a=0, int b=0) : Shape(a, b)
    {                       //     ^^^^^^^^^^^^
    }
    

    You need to call Shape constructor as part of initializer list.
    As in your example, if you write it as a statement,

    {
      Shape(a,b);  // <--- no effect on Base part
    }
    

    then, a temporary Shape object is created and destroyed, so it has no effect.

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

Sidebar

Related Questions

I have the following code compiled by gcc: #include <iostream> using namespace std; class
I compiled the following code as a shared library using g++ -shared ... :
the following piece of C++ code compiled two years ago in a suse 10.1
I come into a strange problem in pthread programming I've compiled the following code
The following code is generating warning C6284 when compiled with /analyze on MSVC 2008
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code gives a different number of destructors when compiled on GCC and
I'm looking for a VM with the following features: Small compiled code footprint (
The following code compiled with MSVC9.0 runs and outputs Destructor four times, which is
i have the following code compiled with GCC 4.2 / XCode. template <typename T>

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.