In C++, I was just wondering, if I have a base class and a derived class, and I use the set method of the derived class for a value, and then use the set method of the base class on the get method of derived, like so..
student is the base, domestic and international are derived. I have a node and sorted list class which are friends of student. The list and node class works fine, my issue is surrounding object slicing i would assume.
class student
{
template<class T> // these classes not shown because they work
friend class SortedList;
template<class T>
friend class Node;
public:
student();
student(int new_Id,string new_FirstName,string new_LastName,float new_Cgpa,int new_ResearchScore);
string getFirstName() const;
void setFirstName(string new_FirstName);
string getLastName() const;
void setLastName(string new_LastName);
float getcgpa() const;
void setCgpa(string new_Cgpa);
int getresearchscore() const;
void setresearchscore(int new_ResearchScore);
string getT() const; //could be virtual
void setT(string t); // attempt to set derived data member province from domestic student
//object
private:
string FirstName;
string LastName;
float cgpa;
int researchscore;
string T;
}
class domesticstudent : public student
{
public:
domesticstudent(); // constructor
domesticstudent(int the_ID, string The_FN, string The_LN,float The_cgpa, int the_RS,string newProvince);
string getT() const;
void setT(string newProvince);
private:
string province;
};
class internationalStudent : public student
{
internationalStudent();
internationalStudent(int the_ID, string The_FN, string The_LN, float The_cgpa, int the_RS,string newCountry, int new_ToeflScore);
string setT(new_Country);
string getT() const;
private:
string country;
};
// sorted list class member function
// which has a node class to initialize
//all of this works fine
sortedlist::listset(const T& obj)
{ obj.getfirstName();
obj.getLastName();
obj.getT(); // same function for three class methods for reusability
obj.getCgpa();
obj.getresearchscore();
sortedlist(obj); // sortedlist member function that initializes sorted
// single linked list;
}
int main()
{
student base_obj;
domesticstudent derived_obj1;
internationalStudent derived_obj2;
Node<student> node_obj; // tempalte node
SortedList<student> List_obj; // template singly link list
// this is read in through a while loop and .txt file
string firstname, lastname , province; //province is derived variable
int researchscore;
float cgpa; // all other variables belong to base
base_obj.setFirstName(firstName);
base_obj.setLastName(lastName);
derived_obj.setT(province);
base_obj.setT(derived_obj.getT());
base_obj.setcgpa(cgpa);
derived_obj.setresearchscore(researchScore);
list_obj.listset(base_obj);
Node<student> node_obj1; // template node
SortedList<student> List_obj1; //template singly linked list
// read in through another while loop and txt file
base_obj.setFirstName(firstName);
base_obj.setLastName(lastName);
derived_obj2.setT(province);
base_obj.setT(derived_obj2.getT());
base_obj.setcgpa(cgpa);
derived_obj.setresearchscore(researchScore);
list_obj1.listset(base_obj);
}
Ive created two template single linked lists.. i have to merge them into one. this seems to work the way i have that but ive been told thats not OOP. Is there any recommendations on how i could accomplish this, i know they have to be upcasted or initialized as base class so they can be merged later.