Inheritance problem.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 26
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Inheritance problem.

 
0
  #1
Oct 24th, 2009
Hi,

I'm storing base class pointers in a vector, the pointers are pointing to objects from the derived class ( Leads ).

I'm unable to access the 'getter' functions of the derived class.

How can this be done?

I'm trying to save the derived objects in a text file (database.txt) when the users chooses to exit the program, but my IDE (Visual Studio) is reporting:

error C2039: 'getDate' : is not a member of 'Diary'


Here is my code :

Header file (diary.h) :
  1. using namespace std;
  2. #include <string>
  3.  
  4. class Diary
  5. {
  6. public:
  7.  
  8. Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
  9.  
  10. //Getters (Getters are used to access private or protected datamembers) :
  11. string getName() const { return m_Name; }
  12. string getAddress() const { return m_Address; }
  13. string getPostCode() const { return m_PostCode; }
  14. string getTelNo() const { return m_TelNo; }
  15. string getDetails() const { return m_Details; }
  16.  
  17. private:
  18.  
  19. protected:
  20.  
  21. string m_Name;
  22. string m_Address;
  23. string m_PostCode;
  24. string m_TelNo;
  25. string m_Details;
  26.  
  27. };
  28.  
  29. Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
  30. {
  31. m_Name = mainname;
  32. m_Address = mainaddress;
  33. m_PostCode = mainpostcode;
  34. m_TelNo = maintelno;
  35. m_Details = maindetails;
  36. }
  37.  
  38. //----------------------------------------------------------------
  39. class Leads : public Diary
  40. {
  41. public:
  42.  
  43. Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
  44.  
  45. //Getters:
  46. string getDate() const { return m_Date; }
  47. string getTime() const { return m_Time; }
  48.  
  49. private:
  50.  
  51. protected:
  52.  
  53. string m_Date;
  54. string m_Time;
  55.  
  56. };
  57. Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
  58. {
  59. m_Date = maindate;
  60. m_Time = maintime;
  61. }

main.cpp :
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include "diary.h"
  6.  
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. vector<Diary *>vectorname;
  12.  
  13. do{
  14. int choice = 0;
  15. cout << "Choose one of the options below:" << endl;
  16. cout << "1) Enter a lead" << endl;
  17. cout << "2) Exit the program" << endl;
  18. cin >> choice;
  19. cin.ignore();
  20.  
  21. switch(choice)
  22. {
  23. case 1:
  24. {
  25. cout << "You chose to enter a lead" << endl;
  26. //---------------------
  27. //Collect the leads details
  28. //---------------------
  29. string mainname;
  30. cout << "Name? " << endl;
  31. getline (cin, mainname);
  32.  
  33. string mainaddress;
  34. cout << "Address? " << endl;
  35. getline (cin, mainaddress);
  36.  
  37. string mainpostcode;
  38. cout << "Post Code? " << endl;
  39. getline (cin, mainpostcode);
  40.  
  41. string maintelno;
  42. cout << "Telephone Number? " << endl;
  43. getline (cin, maintelno);
  44.  
  45. string maindetails;
  46. cout << "Details? " << endl;
  47. getline (cin, maindetails);
  48.  
  49. string maindate;
  50. cout << "Date ? DDMMYY " << endl;
  51. getline (cin, maindate);
  52.  
  53. string maintime;
  54. cout << "Time? 24hr HHMM " << endl;
  55. getline (cin, maintime);
  56.  
  57. cout << "All questions asked" << endl;
  58.  
  59. try
  60. {
  61. Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
  62. vectorname.push_back( ptr );
  63. }
  64. catch (bad_alloc& ba)
  65. {
  66. cerr << "bad_alloc caught: " << ba.what() << endl;
  67. }
  68.  
  69. cout << "past push Lead to list and before deletion" << endl;
  70. break;
  71. }
  72.  
  73. case 2:
  74. {
  75. cout << "You chose to exit the program" << endl;
  76.  
  77. ofstream streamname("database.txt");
  78. if(!streamname)
  79. {
  80. cout << "Cannot open file" << endl;
  81. return 1;
  82. }
  83.  
  84. ////////////////////////////////////////////////////////////////////
  85. streamname << vectorname[0]->getDate();
  86. ///////////////////////////////////////////////////////////////////
  87. streamname.close();
  88.  
  89. cout << "IDE breakpoint used here";
  90. break;
  91. }
  92. }
  93. }
  94. while(true);
  95.  
  96. system ("PAUSE");
  97.  
  98. return 0;
  99. }


Could anyone point out how this should be done?

Really do appreciate any help given with this.

Many thanks.

Carrots
Last edited by Carrots; Oct 24th, 2009 at 7:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
1
  #2
Oct 24th, 2009
>>I'm storing base class pointers in a vector, the pointers are pointing to objects from the derived class ( Leads ).
>>I'm unable to access the 'getter' functions of the derived class. How can this be done?

It seems like you need the use of virtual functions. The error message says what it means, you have not defined a getDate function in you
base class.

Here is an example of something that might help you :
  1. #include <iostream>
  2. #include <vector>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Base{
  9. private :
  10. string name;
  11. public:
  12. Base() : name("") { };
  13. Base(const string str) : name(str) { };
  14.  
  15. virtual const string getName() {
  16. return name;
  17. }
  18. };
  19.  
  20. class Derived : public Base{
  21. string myName;
  22. public:
  23. Derived() : Base(), myName("") { }
  24. Derived(const string derStr) : myName(derStr) { }
  25. Derived(const string baseStr, const string derStr) : Base(baseStr) , myName(derStr) { }
  26. const string getName() {
  27. return myName;
  28. }
  29. };
  30. int main()
  31. {
  32. vector<Base*> baseVec;
  33. Base b1("base 1");
  34. Derived d1("derived 1");
  35. Derived d2("derived 2");
  36.  
  37. baseVec.push_back( &b1);
  38. baseVec.push_back( &d1);
  39. baseVec.push_back( &d2);
  40.  
  41. for(unsigned int i = 0; i < baseVec.size(); i++){
  42. cout << baseVec[i]->getName() << endl;
  43. }
  44. return 0;
  45. }
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 26
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster
 
0
  #3
Oct 24th, 2009
Thanks for helping me out firstPerson !

Can I ask though, I see the base class in your example has a datamember called 'name'.

My base class however is an abstract base class.

Should I add a datamember to my base class even though it won't ever be used?
Last edited by Carrots; Oct 24th, 2009 at 8:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #4
Oct 24th, 2009
Originally Posted by Carrots View Post
Thanks for helping me out firstPerson !

Can I ask though, I see the base class in your example has a datamember called 'name'.

My base class however is an abstract base class.

Should I add a datamember to my base class even though it won't ever be used?
You should not add data variables to Abstract Base class(ABC) if it wont be used in its subclass.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 26
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster
 
0
  #5
Oct 24th, 2009
Thanks again firstPerson

I have the program working now, but would welcome any comments on my implementation in case it's wrong or proper messed up.

I've surrounded the relevant lines with :

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Header file (diary.h) :

  1. using namespace std;
  2. #include <string>
  3.  
  4. class Diary
  5. {
  6. public:
  7.  
  8. Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
  9.  
  10. //Getters (Getters are used to access private or protected datamembers) :
  11. string getName() const { return m_Name; }
  12. string getAddress() const { return m_Address; }
  13. string getPostCode() const { return m_PostCode; }
  14. string getTelNo() const { return m_TelNo; }
  15. string getDetails() const { return m_Details; }
  16.  
  17. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  18. virtual string getDate() { return "aString"; }
  19. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  20. private:
  21.  
  22. protected:
  23.  
  24. string m_Name;
  25. string m_Address;
  26. string m_PostCode;
  27. string m_TelNo;
  28. string m_Details;
  29. };
  30.  
  31. Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
  32. {
  33. m_Name = mainname;
  34. m_Address = mainaddress;
  35. m_PostCode = mainpostcode;
  36. m_TelNo = maintelno;
  37. m_Details = maindetails;
  38. }
  39.  
  40. //----------------------------------------------------------------
  41.  
  42. class Leads : public Diary
  43. {
  44. public:
  45. Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
  46.  
  47. //Getters:
  48. string getDate() const { return m_Date; }
  49. string getTime() const { return m_Time; }
  50.  
  51. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  52. string getDate() { return m_Date; }
  53. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  54. private:
  55.  
  56. protected:
  57.  
  58. string m_Date;
  59. string m_Time;
  60. };
  61.  
  62. Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
  63. {
  64. m_Date = maindate;
  65. m_Time = maintime;
  66. }

main.cpp :

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include "diary.h"
  6.  
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. vector<Diary *>vectorname;
  12.  
  13. do{
  14. int choice = 0;
  15. cout << "Choose one of the options below:" << endl;
  16. cout << "1) Enter a lead" << endl;
  17. cout << "2) Exit the program" << endl;
  18. cin >> choice;
  19. cin.ignore();
  20.  
  21. switch(choice)
  22. {
  23. case 1:
  24. {
  25. cout << "You chose to enter a lead" << endl;
  26. //---------------------
  27. //Collect the leads details
  28. //---------------------
  29.  
  30. string mainname;
  31. cout << "Name? " << endl;
  32. getline (cin, mainname);
  33.  
  34. string mainaddress;
  35. cout << "Address? " << endl;
  36. getline (cin, mainaddress);
  37.  
  38. string mainpostcode;
  39. cout << "Post Code? " << endl;
  40. getline (cin, mainpostcode);
  41.  
  42. string maintelno;
  43. cout << "Telephone Number? " << endl;
  44. getline (cin, maintelno);
  45.  
  46. string maindetails;
  47. cout << "Details? " << endl;
  48. getline (cin, maindetails);
  49.  
  50. string maindate;
  51. cout << "Date ? DDMMYY " << endl;
  52. getline (cin, maindate);
  53.  
  54. string maintime;
  55. cout << "Time? 24hr HHMM " << endl;
  56. getline (cin, maintime);
  57.  
  58. cout << "All questions asked" << endl;
  59.  
  60. try
  61. {
  62. Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
  63. vectorname.push_back( ptr );
  64. }
  65. catch (bad_alloc& ba)
  66. {
  67. cerr << "bad_alloc caught: " << ba.what() << endl;
  68. }
  69.  
  70. cout << "past push Lead to list and before deletion" << endl;
  71. break;
  72. }
  73.  
  74. case 2:
  75.  
  76. {
  77. cout << "You chose to exit the program" << endl;
  78.  
  79. ofstream streamname("database.txt");
  80. if(!streamname)
  81. {
  82. cout << "Cannot open file" << endl;
  83. return 1;
  84. }
  85.  
  86. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  87. streamname << vectorname[0]->getDate();
  88. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  89.  
  90. streamname.close();
  91. cout << "IDE breakpoint used here";
  92.  
  93. break;
  94. }
  95. }
  96. }
  97. while(true);
  98.  
  99. system ("PAUSE");
  100.  
  101. return 0;
  102. }

I used the "aString" text in the abstract classes definition of the function because it was complaining about the function having the wrong type when I used "0".

Thanks for any advice on this!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #6
Oct 24th, 2009
If thats your case then you should declare a pure virtual function like so :
  1. virtual string getName() = 0;

That means that this class is a ABC class and all subclass should
redefine the method getName().
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 26
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster
 
0
  #7
Oct 24th, 2009
Perfect!!

Thank you sooo much for your help firstPerson!!

If your considering buying a lottery ticket this week, I wish you much luck and I hope it comes in for you!

hugethumbsup.jpg

Last edited by Carrots; Oct 24th, 2009 at 10:45 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC