virtual function not able to display proper result

i have done c++ program, where my base class ie Shape does the job of taking data , and the other two derived classes Triangle and Rectangle calculate the area . Problem is that am getting the area as some garbage value. i have done the code. please have a look at it and guide me. Thanks



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
     #include<iostream>
     using namespace std;

      class Shape{
      protected:  double b,h;
    
       public:void get_data()
          {
            cout<<"Enter the height\n";
            cin>>h;
             cout<<"Enter the breadth\n";
             cin>>b;
     
             }
       virtual void display_area(){}
    
      };



      class Rectangle:public Shape
      {
         public:void display_area(){
         cout<<"\n\nArea of Rectangle:" << b*h;
        }
       };

     class Triangle:public Shape
      {
    
        public:void display_area(){
         cout<<"\n\nArea of Triangle:"<<0.5*b*h; 
         }
     };

      int main()
      {
         Shape s;
         Triangle t;
         Rectangle r;
         Shape *ptr;
    
         ptr=&s;
         ptr->get_data();
    
         ptr=&t;
         ptr->display_area();
    
         ptr=&r;
         ptr->display_area();
         return 0;
     }
This
1
2
         ptr=&s;
         ptr->get_data();
is unnecessary: Simply write s.get_data();

You need to call get_data(); for each (t/r) object in ordert to get no garbage
a mistake here is that you are calling get_data for object s.But you need to call

1
2
t.get_data()
r.get_data

or through ptr.

By inheritence you can inherit its characterstics but not values set at runtime.This can be done by setting up custom function or changing current function. or using constructor with parameter(as class Shape).
i want to get data through s

and use the data to display the calculated area for triangle and rectangle. any way i can do this? please let me know
as i told you can use custom function or constructor with parameters in class triangle and rectangle to do so.No other direct way.

I don't know about static but it might be useful.Not sure.

Just pass s as argument in calling function.
e.g.
1
2
3
4
5
6
7
class Triangle:public Shape
      {
    
        public:void display_area(Shape Q){
         cout<<"\n\nArea of Triangle:"<<0.5*Q::b*Q::h; 
         }
     };


In main()
r.display_data(s);

and again you can use constructor too but then you have to define object after adding data to s.
Thanks Akshit it works perfect. But i have one doubt. h and b are protected. if i make them public then only they are accesible using Q.b othiwize not.
Last edited on
My mistake.I mixed a part of my project with yours.And you are correct about Q.b

Now the workaround I know will include two more functions.

And if you make h and b public then there will be no necessit for inheritence.

I suggest you to use your previous code with calling getdata from r and t.
But if you still want workaround then I will thought over it.
Last edited on
yea please . i dont want to call get_data from r and t
I answered that in starting if you haven't noticed.

BTW this is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include<iostream>
     using namespace std;

      class Shape{
      protected:  static double b,h;
    
       public:
		void get_data()
          {
            cout<<"Enter the height\n";
            cin>>h;
             cout<<"Enter the breadth\n";
             cin>>b;
     
             }
       virtual void display_area(){}
    
      };
	  double Shape::b=0;
	  double Shape::h=0;


      class Rectangle:public Shape
      {
         public:void display_area(){
         cout<<"\n\nArea of Rectangle:" << b*h;
        }
       };

     class Triangle:public Shape
      {
    
        public:void display_area(){
				   cout<<"\n\nArea of Triangle:"<<0.5*b*h; 
         }
     };

      int main()
      {
         Shape s;
         Triangle t;
         Rectangle r;
		 s.get_data();
		 t.display_area();
		 r.display_area();
		 cin.get();
		 cin.get();
         return 0;
     }
hey thanks a lot akshit. it works perfect now. btw can you please explain me how changing the variables to static made it work.
static means one time memory allocation of variables for any amount of objects.

That means h and b are fixed at a place now.only there values can be changed.And the change will carry over to each object and to every place they are used.

Static is mainly used to count object created,time program runned etc.

You can google static for more info if you do not get it.
thanks a lot man.. understood :)
:):)
Topic archived. No new replies allowed.