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
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).
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.
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.