Connecting Tech Pros Worldwide Forums | Help | Site Map

need help on this inheritance problems..

Newbie
 
Join Date: Jan 2007
Posts: 16
#1: Oct 19 '09
hello guys...i just done my assignment and here's the code


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class CelestialBody
  7. {
  8.  
  9. private:
  10. double size;
  11. public:
  12.     CelestialBody (double );
  13. ;
  14.  
  15. class Planet : public CelestialBody
  16. {
  17. private:
  18.  
  19. double orbit_time;
  20. public:
  21.   Planet(double , double);
  22.  
  23. };
  24.  
  25. Class Earth : public Planet
  26.  
  27. {
  28.  
  29. private:
  30. unsigned long int population ;
  31.  
  32. public:
  33.  
  34.    Earth();
  35. };
  36.  
  37. Celestialbody :: Celestialbody (double) :
  38. size(SIZE)
  39. {
  40. cout<<"CelestialBody is called"<<endl;
  41. }
  42.  
  43. Planet :: Planet (double) : size(SIZE) , orbit_time(ORBIT_TIME)
  44. {
  45. cout <<  "Planet constructor is called"<<endl;
  46.  
  47.  
  48.  
  49.  
  50.  
  51. Earth :: Earth ()
  52.  
  53. : Planet (1,40000), population (3000000000)
  54.  
  55. {
  56.  
  57. cout <<" Earth constructor is called."<<endl;
  58. cout <<"Population =" <<population <<endl;
  59. cout<<"orbit time = "<<orbit_time<< endl;
  60. cout<< "size =" <<size <<endl;
  61.  
  62.  
  63.  
  64. int main ()
  65.  
  66.  
  67. {
  68. Earth earth;
  69.  
  70. return 0;
  71.  
  72. }
  73.  
  74.  
the output should be like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. CelestialBody constructor is called
  3. Planet constructor is called
  4. Population  constructor is called
  5.  
  6. Population = 5000000000
  7.  
  8. orbit time = 1
  9.  
  10. size = 40000
  11.  
  12.  
but i am stuck in the earth class and constructor section.
I guess i was wrong in calling of the CelestialBody constructor to initialize the
data members of both the Planet and CelestialBody.
I didn't declared SIZE and ORBIT_TIME but it will not effect the end results will it?
any help plez..i did my homework..need for it to be compiled..

Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#2: Oct 19 '09

re: need help on this inheritance problems..


there are some compiling errors caused by -
line 13 - missed '}'
line 25 - class keyword in must belowercase
line 37 - class name must be with letter case exactly as declared
lines 46, 61 - missed '}' after function body

Planet constructor - must have two argument, as in declaration,
and init parent's member using parent's constructor:
Planet :: Planet (double size, double time)
: CelestialBody(size)
, orbit_time(time)
{ ... }

then SIZE and ORBIT_TIME are not needed

Same for CelestialBody constructor -
CelestialBody :: CelestialBody (double s)
: size(s)


Also there are errors with private members visibility in cout << statements in Earth constructor - you need to make some getters in Planet and Celestialbody to return their value. Also there is a problem with 3E9 value - it doesn't fit signed (long) int ( 32 bit), and 5E9 doesn't fit in 32 bit at all - use long long int.
Newbie
 
Join Date: Jan 2007
Posts: 16
#3: Oct 20 '09

re: need help on this inheritance problems..


so thanks for the info.
so is it this

[
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2.  
  3. using namespace std; 
  4.  
  5. class CelestialBody 
  6.  
  7. private: 
  8. double size; 
  9. public: 
  10.     CelestialBody (double ); 
  11.  
  12. class Planet : public CelestialBody 
  13. private: 
  14.  
  15. double orbit_time; 
  16. public: 
  17.   Planet(double , double); 
  18.  
  19. }; 
  20.  
  21. class Earth : public Planet 
  22.  
  23.  
  24. private: 
  25. long  long int population ; 
  26.  
  27. public: 
  28.  
  29.    Earth(); 
  30. }; 
  31.  
  32. Celestialbody :: Celestialbody (double s) : 
  33. size(s) 
  34. cout<<"CelestialBody is called"<<endl; 
  35.  
  36. Planet :: Planet (double size, double time) : Celestialbody(size) , orbit_time(time) 
  37. cout <<  "Planet constructor is called"<<endl; 
  38.  
  39.  
  40.  }
  41.  
  42.  
  43. Earth :: Earth () 
  44.  
  45. : Planet (1,40000), population (3000000000) 
  46.  
  47.  
  48. cout <<" Earth constructor is called."<<endl; 
  49. cout <<"Population =" <<population <<endl; 
  50. cout<<"orbit time = "<<orbit_time<< endl; 
  51. cout<< "size =" <<size <<endl; 
  52.  
  53.  
  54.  }
  55. int main () 
  56.  
  57.  
  58. Earth earth; 
  59.  
  60. return 0; 
  61.  
but i am unsure about return private members in earth constructor to return value.
do i need to make a member function also in class Celestialbody and Planet to return the values of it's data members?
how is this like?
Member
 
Join Date: Apr 2007
Posts: 35
#4: Oct 20 '09

re: need help on this inheritance problems..


Just change way of creating objects in 'Earth' class.
Expand|Select|Wrap|Line Numbers
  1. Earth :: Earth ()
  2. :population (3000000000),Planet (1,40000)
  3. {
  4.  
  5.         cout <<" Earth constructor is called."<<endl;
  6. }
  7.  
Newbie
 
Join Date: Jan 2007
Posts: 16
#5: Oct 22 '09

re: need help on this inheritance problems..


thanks..really appreciated the effort..it works like a charm
Reply