| | |
Inheritance problem.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 26
Reputation:
Solved Threads: 0
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:
Here is my code :
Header file (diary.h) :
main.cpp :
Could anyone point out how this should be done?
Really do appreciate any help given with this.
Many thanks.
Carrots
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) :
c++ Syntax (Toggle Plain Text)
using namespace std; #include <string> class Diary { public: Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails); //Getters (Getters are used to access private or protected datamembers) : string getName() const { return m_Name; } string getAddress() const { return m_Address; } string getPostCode() const { return m_PostCode; } string getTelNo() const { return m_TelNo; } string getDetails() const { return m_Details; } private: protected: string m_Name; string m_Address; string m_PostCode; string m_TelNo; string m_Details; }; Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails) { m_Name = mainname; m_Address = mainaddress; m_PostCode = mainpostcode; m_TelNo = maintelno; m_Details = maindetails; } //---------------------------------------------------------------- class Leads : public Diary { public: Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time); //Getters: string getDate() const { return m_Date; } string getTime() const { return m_Time; } private: protected: string m_Date; string m_Time; }; Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails) { m_Date = maindate; m_Time = maintime; }
main.cpp :
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> #include <fstream> #include "diary.h" using namespace std; int main () { vector<Diary *>vectorname; do{ int choice = 0; cout << "Choose one of the options below:" << endl; cout << "1) Enter a lead" << endl; cout << "2) Exit the program" << endl; cin >> choice; cin.ignore(); switch(choice) { case 1: { cout << "You chose to enter a lead" << endl; //--------------------- //Collect the leads details //--------------------- string mainname; cout << "Name? " << endl; getline (cin, mainname); string mainaddress; cout << "Address? " << endl; getline (cin, mainaddress); string mainpostcode; cout << "Post Code? " << endl; getline (cin, mainpostcode); string maintelno; cout << "Telephone Number? " << endl; getline (cin, maintelno); string maindetails; cout << "Details? " << endl; getline (cin, maindetails); string maindate; cout << "Date ? DDMMYY " << endl; getline (cin, maindate); string maintime; cout << "Time? 24hr HHMM " << endl; getline (cin, maintime); cout << "All questions asked" << endl; try { Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime); vectorname.push_back( ptr ); } catch (bad_alloc& ba) { cerr << "bad_alloc caught: " << ba.what() << endl; } cout << "past push Lead to list and before deletion" << endl; break; } case 2: { cout << "You chose to exit the program" << endl; ofstream streamname("database.txt"); if(!streamname) { cout << "Cannot open file" << endl; return 1; } //////////////////////////////////////////////////////////////////// streamname << vectorname[0]->getDate(); /////////////////////////////////////////////////////////////////// streamname.close(); cout << "IDE breakpoint used here"; break; } } } while(true); system ("PAUSE"); return 0; }
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.
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 :
>>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 :
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include<string> using namespace std; class Base{ private : string name; public: Base() : name("") { }; Base(const string str) : name(str) { }; virtual const string getName() { return name; } }; class Derived : public Base{ string myName; public: Derived() : Base(), myName("") { } Derived(const string derStr) : myName(derStr) { } Derived(const string baseStr, const string derStr) : Base(baseStr) , myName(derStr) { } const string getName() { return myName; } }; int main() { vector<Base*> baseVec; Base b1("base 1"); Derived d1("derived 1"); Derived d2("derived 2"); baseVec.push_back( &b1); baseVec.push_back( &d1); baseVec.push_back( &d2); for(unsigned int i = 0; i < baseVec.size(); i++){ cout << baseVec[i]->getName() << endl; } return 0; }
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
•
•
Join Date: Mar 2009
Posts: 26
Reputation:
Solved Threads: 0
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?
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.
0
#4 Oct 24th, 2009
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
•
•
Join Date: Mar 2009
Posts: 26
Reputation:
Solved Threads: 0
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 :
Header file (diary.h) :
main.cpp :
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!

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
c++ Syntax (Toggle Plain Text)
using namespace std; #include <string> class Diary { public: Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails); //Getters (Getters are used to access private or protected datamembers) : string getName() const { return m_Name; } string getAddress() const { return m_Address; } string getPostCode() const { return m_PostCode; } string getTelNo() const { return m_TelNo; } string getDetails() const { return m_Details; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX virtual string getDate() { return "aString"; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX private: protected: string m_Name; string m_Address; string m_PostCode; string m_TelNo; string m_Details; }; Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails) { m_Name = mainname; m_Address = mainaddress; m_PostCode = mainpostcode; m_TelNo = maintelno; m_Details = maindetails; } //---------------------------------------------------------------- class Leads : public Diary { public: Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time); //Getters: string getDate() const { return m_Date; } string getTime() const { return m_Time; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX string getDate() { return m_Date; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX private: protected: string m_Date; string m_Time; }; Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails) { m_Date = maindate; m_Time = maintime; }
main.cpp :
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> #include <fstream> #include "diary.h" using namespace std; int main () { vector<Diary *>vectorname; do{ int choice = 0; cout << "Choose one of the options below:" << endl; cout << "1) Enter a lead" << endl; cout << "2) Exit the program" << endl; cin >> choice; cin.ignore(); switch(choice) { case 1: { cout << "You chose to enter a lead" << endl; //--------------------- //Collect the leads details //--------------------- string mainname; cout << "Name? " << endl; getline (cin, mainname); string mainaddress; cout << "Address? " << endl; getline (cin, mainaddress); string mainpostcode; cout << "Post Code? " << endl; getline (cin, mainpostcode); string maintelno; cout << "Telephone Number? " << endl; getline (cin, maintelno); string maindetails; cout << "Details? " << endl; getline (cin, maindetails); string maindate; cout << "Date ? DDMMYY " << endl; getline (cin, maindate); string maintime; cout << "Time? 24hr HHMM " << endl; getline (cin, maintime); cout << "All questions asked" << endl; try { Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime); vectorname.push_back( ptr ); } catch (bad_alloc& ba) { cerr << "bad_alloc caught: " << ba.what() << endl; } cout << "past push Lead to list and before deletion" << endl; break; } case 2: { cout << "You chose to exit the program" << endl; ofstream streamname("database.txt"); if(!streamname) { cout << "Cannot open file" << endl; return 1; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX streamname << vectorname[0]->getDate(); //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX streamname.close(); cout << "IDE breakpoint used here"; break; } } } while(true); system ("PAUSE"); return 0; }
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!
0
#6 Oct 24th, 2009
If thats your case then you should declare a pure virtual function like so :
That means that this class is a ABC class and all subclass should
redefine the method getName().
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2009
Posts: 26
Reputation:
Solved Threads: 0
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
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.
![]() |
Similar Threads
- C++ Inheritance problem with some syntax errors (C++)
- has-a inheritance (C++)
- Inheritance and Data structure (C++)
- inheritance problem (C++)
- Class/Inheritance problem (C++)
- hybrid inheritance prob (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Games
- Next Thread: sort files based on filename
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






