Hi there,
I'm trying to store pointers in a Vector, but the inherited classes datamembers are being lost.
I believe this is due to Slicing, but I'm unable to see where I'm going wrong, so what hoping someone may be able to point out where my mistake is.
I've included my programs code. It does compile.
Header file (diary.h) :
Code:
using namespace std;
#include <string>
class Diary
{
public:
Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
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_MainDate, string m_MainTime);
private:
protected:
string m_MainDate;
string m_MainTime;
};
Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
{
m_MainDate = maindate;
m_MainTime = maintime;
}
main.cpp :
Code:
#include <iostream>
#include <string>
#include <vector>
#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;
//save all the objects to the text file
//itarate over the vector to delete all the pointers.
//exit the program
break;
}
}
}
while(true);
system ("PAUSE");
return 0;
}
As always, your help is really appreciated with my queries.
Thanks a lot.