By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

please i need your help in this program

P: 11
Hi every one :)

would any one help me in this program:
I have a base class called "Person" and two derived classes "Student" and "teacher"
i have a function in class Student called getStudents takes one parameter which is a name and returns an array of objects
that their names similer to the argument.the same function in the class Teachers called getTeachers.
Also, there is a function in another class "School" called getBoth , which takes one parameter " the name" and returns an array of abjects of type Person contains both types and prints their names depending on their type.
i know how i am going to include the classes in the Shool class,all what i want it is the implement of the functions.I tryed to implement it .. but errors occuring spcially in the function getBoth..

please.. i need your help..

class Person{
string name;
public:
string getName(){return name;}
};
class Student : public Person{
Student* getStudents(string name);
}
class Teacher : public Person{
Teacher *getTeachers(string name);
}
class School{
Person* getBoth(string name);
}
Dec 31 '06 #1
Share this Question
Share on Google+
3 Replies


100+
P: 1,646
Hi every one :)

would any one help me in this program:
I have a base class called "Person" and two derived classes "Student" and "teacher"
i have a function in class Student called getStudents takes one parameter which is a name and returns an array of objects
that their names similer to the argument.the same function in the class Teachers called getTeachers.
Also, there is a function in another class "School" called getBoth , which takes one parameter " the name" and returns an array of abjects of type Person contains both types and prints their names depending on their type.
i know how i am going to include the classes in the Shool class,all what i want it is the implement of the functions.I tryed to implement it .. but errors occuring spcially in the function getBoth..

please.. i need your help..

class Person{
string name;
public:
string getName(){return name;}
};
class Student : public Person{
Student* getStudents(string name);
}
class Teacher : public Person{
Teacher *getTeachers(string name);
}
class School{
Person* getBoth(string name);
}
Hi. Could you please post the code with the errors?
Dec 31 '06 #2

P: 11
this is the code

Student* School::getStudents(string name)
{
n=0; // global variable used in main to print the array
Student *student;
student = new Student[index1]; //index1 is the number of students
for (int i = 0 ; i<index1 ; i++)
if (students[i]->getName()==name)//students is an array
//contains all students
{
student[n]=*students[i];
n++;
}
return student;
}

Teacher* School::getTeachers(string name)
{
n=0;
Teacher *teacher;
teacher = new Teacher[index2];
for (int i = 0 ; i<index2 ; i++)
if (teacher[i]->geName()==name)
{
teacher[n]=*teachers[i];
n++;
}
return teacher;
}

Person* School::getBoth(string name)
{
Person *person;
index=index1+index2;
person=new Person [index];
for( int i=0; i<index1;i++)
{
if (students[i]->getName()==name)
{

person[index]=*students[i];
index++;
}
for ( int j=0; j<index2; j++)
if (teachers[j]->getName()==name)
{
person[index]=*teachers[j];
index++;
}

}// end for


return person;
}


and the error occur during the run time when i print the array returned from getBoth function in main
i know i cann't store in base class array objects of derived class type and there is away which is use apointer as Person **person but i don't know how to deal with it,, other thing i tryed to use the function getStudents() and getTeachers in the last fuction as an easier way.. but the same error occur.....
thank you :)
Dec 31 '06 #3

P: 8
Could you please paste the class declarations (including member variable and member functions).

I guess there is a problem in the statment
person[index]=*teachers[j]; /// what do u want to say

If u want u can store derived class object in the base class pointer.

So, it might be a better way to store derived objects in base class array of pointers.

Hope this would help.

Cheers!
Jan 2 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.