0

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.

Colton F
  • 1
  • 1
  • 1
    It is hard to understand what you are asking for, but it looks like you are suffering from [object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing) in your linked list. – Remy Lebeau Aug 15 '21 at 19:51
  • In the top code box, is that valid way to set a derived object to a base? with the b_obj.setMethod(D_obj.getMethod()) ? – Colton F Aug 15 '21 at 20:08
  • you are not setting a derived object to a base object. You are setting a data member of a derived object to a data member of a base object, and that is perfectly fine (though your use of non-virtual methods with the same name and argument list is questionable) – Remy Lebeau Aug 15 '21 at 20:10
  • Virtual functions are *the single* defining feature of OOP in C++. If you are not using them, you are not breaking OOP principles, you are just not doing any OOP to begin with. – n. m. could be an AI Aug 15 '21 at 20:13
  • Could you recommend what you would if you were to accomplish this same task? i have 4 data members of a base class, 1 in a derived. Im putting two object lists into a template single linked lists. ill edit to post, i was just wondering about this particular example, i apoligize. – Colton F Aug 15 '21 at 20:18
  • 1
    @ColtonF it is not clear what task you are trying to solve in the first place – Remy Lebeau Aug 15 '21 at 20:20
  • Why does your base `getMethod` return an `int` when the `s` member is a `string`? – Adrian Mole Aug 15 '21 at 20:20
  • accident just updated. – Colton F Aug 15 '21 at 21:17
  • If your task is to submit homework and earn some credits, but you don't want to tell us what the assignment says, then we can only guess. Does it say "OOP" somewhere? If so. I would start with reading up on [polymorphism](https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c), while reading [this](https://stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references) and [this](https://stackoverflow.com/questions/274626/what-is-object-slicing) early on. – n. m. could be an AI Aug 16 '21 at 08:34

0 Answers0