value


i have done that code ..problem is that when i have set value of dim1,dim2 through main function ..but it just taking the garbage value not the value i have given how to solve it?

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
  assignment 1

#include<iostream>
using namespace std;
class shape
{
   public:
       int dim1;
       int dim2;
       void setDimensions(int d,int d2)
       {
           dim1=d;
           dim2=d2;
           cout<<dim1<<endl<<dim2;
       }
};
class triangle:public shape
{
public:
   float getArea()
   {
       float area;
       area=0.5* (dim1* dim2);
       return area;
   }
   void showArea()
   {
       cout<<"Area of triangle is:"<<getArea()<<endl;
   }
};

int main()
{
    shape s;
    s.setDimensions(4,6);
    triangle t;
    t.showArea();
}
You have not set the dimensions of the triangle t.
thanx a lot :)

i thought if i set values in parent class then child class will also have that value of that variable..there was my mistake? right?

The object s and t are two completely different objects. The share no data.
Topic archived. No new replies allowed.