Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The doubt is about whether to write member functions for area and perimeter and
my syntax is as follows check whether it is right

C#
#include <iostream.h>
#include<conio.h>
class Area
{
  public:
    float area(float l,float b)
    {
        return l*b;
    }
};

class Perimeter
{
  public:
    float peri(float l,float b)
    {
        return 2*(l+b);
    }
};

class Rectangle : private Area, private Perimeter
{
    private:
        float length, breadth;
    public:
       Rectangle() : length(0.0), breadth(0.0) { }
       void get_data( )
       {
           cout<<"Enter length: ";
           cin>>length;
           cout<<"Enter breadth: ";
           cin>>breadth;
       }

       
};

int main()
{
    Rectangle r;
    r.get_data();
    cout<<"Area = "<<r.area_calc();
    cout<<"\nPerimeter = "<<r.peri_calc();
    return 0;
}
Posted
Updated 3-Apr-14 8:54am
v2
Comments
Maciej Los 3-Apr-14 14:55pm    
"check whether it is right" - not a question at all!
[no name] 6-Apr-14 4:29am    
what do u mean to say!!!!!!!!!!!
Maciej Los 6-Apr-14 8:27am    
What's the question? What kind of issue do you have?
nv3 3-Apr-14 17:07pm    
This is a non-viable class design. Area and Perimeter are properties of a Rectangle and should not be base classes. Just throw area Area and Perimeter classes away and make are and perimeter member functions of Rectangle. Then things start to make some sense.
enhzflep 3-Apr-14 18:28pm    
As nv3 mentions, these (perimeter & area) are both attributes of the shape. All shapes have these attributes, though they are computed differently for each shape.
A better design would be to declare a base-class, shape - give it virtual member functions to compute area and perimeter. (Made virtual to prevent you creating an instance of the shape class, also to force you to implement these member functions in descendant classes) Then, inherit from this class when you'd like to create circle, rect, triangle etc shapes. You then implement these two methods differently, as required for each new class.

You will then have the basis for some classes that deal with shapes that have a consistent interface. That is, you can call .area() or .perimeter() on any shape - whether it's an instance of a circle, rect or triangle class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900