#include<iostream>
using namespace std;

class area
{
    double dim1, dim2;
public:
    void setarea(double d1, double d2)
    {
        dim1=  d1;
        dim2 = d2;
    }
    void getdim(double &d1, double &d2)
    {
        d1 = dim1;
        d2 = dim2;
    }
    virtual double getarea()
    {
        cout<<"You must override this functionn";
        return 0.0;
    }
};

class rectangle : public area
{
public:
    double getarea()
    {
        double d1, d2;
        getdim(d1, d2);

        return d1*d2;;
    }
};

class triangle : public area
{
public:
    double getarea()
    {
        double d1, d2;
        getdim(d1, d2);
        return 0.5*d1*d2;
    }
};

int main()
{
    area *p;
    rectangle r;
    triangle t;

    r.setarea(3.3, 4.5);
    t.setarea(4.0, 5.0);

    p=&r;
    cout<<"Rectangle has area: "<<p->getarea()<<'n';

    p=&t;
    cout<<"Triangle has area: "<<p->getarea()<<'n';
}

I only have problem with the getdim() function within "class area". I don't understand what is the necessity of this function.
Futhermore, in the derived class when the virtual function overridden, getdim function is used outside the "class area". But the getdim function is not identified in the derived class.
Is it always possible to use a function to another class where the function is not identified??

Thanks in advance

Recommended Answers

All 7 Replies

Honestly, this code looks like it may be an overly-explanatory example from a text. The code contains many things that you generally won't see in well-designed/written code. I suggest you read the chapter it's part of a little closer. Here is a highly generalized summary (I want to avoid doing your homework for you).

Re-posting code with [code] code tags [/code] for readability and discussion:

#include<iostream>
 using namespace std;

 class area
 {
 double dim1, dim2;
 public:
 void setarea(double d1, double d2)
 {
 dim1= d1;
 dim2 = d2;
 }
 void getdim(double &d1, double &d2)
 {
 d1 = dim1;
 d2 = dim2;
 }
 virtual double getarea()
 {
 cout<<"You must override this function\n";
 return 0.0;
 }
 };

 class rectangle : public area
 {
 public:
 double getarea()
 {
 double d1, d2;
 getdim(d1, d2);

 return d1*d2;;
 }
 };

 class triangle : public area
 {
 public:
 double getarea()
 {
 double d1, d2;
 getdim(d1, d2);
 return 0.5*d1*d2;
 }
 };

 int main()
 {
 area *p;
 rectangle r;
 triangle t;

 r.setarea(3.3, 4.5);
 t.setarea(4.0, 5.0);

 p=&r;
 cout<<"Rectangle has area: "<<p->getarea()<<'\n';

 p=&t;
 cout<<"Triangle has area: "<<p->getarea()<<'\n';
 }

This code includes examples of inheritance and polymorphism.

Observe Lines 25 & 37:

...
25. class rectangle : public area
...
37. class triangle : public area
...

These lines say that the rectangle and triangle classes inherit properties from the area class. Because they inherit from area, they have access to the members of the area class.

In actuality, because the functions/methods are declared as part of the "public" section of area, any piece of code can access them as long as there is an object of that type available to call them through.

getdim() is declared as public in the base class, and the inheritance is public, so the derived classes will have access to this method (as public).

This method retrieves the values of the dim1 and dim2 variables by reference. In triangle and rectangle , the values of d1 and d2 are passed into the method, and are assigned the values of dim1 and dim2 respectively.

getdim() is declared as public in the base class, and the inheritance is public, so the derived classes will have access to this method (as public).

This method retrieves the values of the dim1 and dim2 variables by reference. In triangle and rectangle , the values of d1 and d2 are passed into the method, and are assigned the values of dim1 and dim2 respectively.

Thanks a lot Jonsca.

But can't I output the same things without getdim() function?? This is not my homework but I just need the concept to understand some fundamental things of c++.

>>But can't I output the same things without getdim() function??

Unfortunately no. The variables area::dim1 and area::dim2 are private. This means that only members of the actual area class can access them directly. In order to allow rectangle and triangle to access them directly, they need to be re-classified as protected.

This is just one of the atypical things that I alluded to in my previous post.

Read this for more information.

>>But can't I output the same things without getdim() function??

Unfortunately no. The variables area::dim1 and area::dim2 are private. This means that only members of the actual area class can access them directly. In order to allow rectangle and triangle to access them directly, they need to be re-classified as protected.

This is just one of the atypical things that I alluded to in my previous post.

Read this for more information.

What would if I declare the dim1 and dim2 as public inside the class area?? Then can I delete the getdim() function??

Is it always necessary to declare the variable as private?? Everywhere I see that the variable always declare as private....what is the advantage of this and what is the disadvantage of declaring the variable inside the public??

Read Wikipedia's explanation of encapsulation to see some of the philosophy behind it.

You could certainly make it public, but what if you want people using your classes not to have to muck around with those things. As an example, what if you wanted to change the way you stored the member variables? If they are private, all you'd have to do is modify getdim() and you'd be good to go. Everyone could access the values like they always did. If the member variables were public, you'd need to modify each time they are used in the code.

Thanks a lot from my heart. All the things of the code now I can understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.