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

Edited 7 Years Ago by Carrots: n/a

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

#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;
}
Comments
Thanks ever so much buddy! :)

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?

Edited 7 Years Ago by Carrots: n/a

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.

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

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 :

#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!

If thats your case then you should declare a pure virtual function like so :

virtual string getName() = 0;

That means that this class is a ABC class and all subclass should
redefine the method getName().

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

:) :)

Edited 7 Years Ago by Carrots: n/a

This question has already been answered. Start a new discussion instead.