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) :
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 :
#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