inheritance

plz help me in understanding how m i suppose to maintain the list and wat will be the structure of following code???

Write a C++ program that represents an HOD-Faculty relationship in an academic institution using public inheritance. C++ program should provide following operations, only through member functions, no constructor/destructor should be used.

a. Add a faculty or HOD.
b. Maintain a common list (use static allocation) for both HODs and Faculties.
c. Display all the Faculties (including HOD who is also a Faculty) with respect to their names.

Assume faculty attributes as Faculty ID and Faculty Name, while attributes of the HOD as Department Name.
what code have you written uptill now ?
what I see from this "homework" assignment, I might start with something like:

1
2
3
4
5
6
7
8
9
10
11
12
class facultyMember
{
protected:
       string name;
       string classTaught;
       // bla bla bla 
};

class HeadOfDepartment : public facutlyMember
{
      // head of department specific stuff...
};


The main reason every person is a faculty member, but only a few are Head of Departments.

When I use the list it would be listed as all faculty, and I would cast to put HOD's on it.

I hope that puts you in a direction.
Last edited on
Its actually after making class i got stuck what i am suppose to do...if i made objects of both the class faculty member and head of department and take inpput from user depending on choice whether to enter faculty or HOD then to maintain a common list should i used pointer to base class and use virtual display function and a friend function arrange in base class .... is this right?????????
to maintain a common list you can use a vector of base class pointer type, in your case faculty.
even if i use vector of base class as u said.... then i hv to use virtual display function and a friend function arrange in base class ....
is it ????
yes you have to use virtual display function. but why you want friend function?

your faculty class may be like this

1
2
3
4
5
6
7
8
9
10
11
class faculty
{
public:
virtual void add();
virtual void maintain_list(faculty fac1);
virtual void display();

string name;
int id;
vector<faculty *> fac;
};
Last edited on
k... thanks i was not thinking of making maintain_list function as faculty member function... but one thing y are u making this member function as virtual??
this function need to be reimplemented in hod class which will be derived from faculty class.

there is typo in function signature. it should take pointer as argument.
virtual void maintain_list(faculty *fac1);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <vector>
using namespace std ;

class Cfaculty  : 
{
public:
				virtual void add() = 0 ;
				virtual void maintain_list(faculty fac1) = 0;
				virtual void display() = 0;
public :
string name;
int id;

};

class HOD : public Cfaculty
{
	public:
			void add();
			void maintain_listC(faculty fac1);
			void display();
			vector<HOD >  fac;
};
void HOD:add()
{
	cout<<"\n Enter the name of the faculty ";
	cin>name; 
	cout<<"\n Enter the id of the faculty ";
	cin>id; 
	
}
void HOD::maintain_list(faculty fac1) 
{
	fac1.push_back(fac1);
}
void HOD::display()
{
	HOD hod ; 
	
	 vector<HOD>::iterator it;
	 cout << "myvector contains:";
  for ( it=myvector.begin() ; it < myvector.end(); it++ )
	{
	  hod = *it ; 
    cout << " \n" << hod.name<<hod.id;
	}
}


please correct me if i am wrong .
Last edited on
vector<HOD > fac; should be protected member of Cfaculty, as it should be accessed by Cfaculty
as well as HOD class. an object of Cfaculty class should also update vector<HOD > fac.


1
2
 vector<HOD > fac; as it should be accessed by Cfaculty
as well as HOD class. an object of Cfaculty class should also update vector<HOD > fac.


please let me know how this could be done ?
Last edited on
the question says that though hod inherits from faculty ... there can be some objects of faculty only which are not HOD.... as per ur code there is only HOD objects....
i have made a program without using STL plz see to it
#include<cstring>
#include<iostream>
using namespace std;
class faculty
{
protected:
char name[50];
long salary;
char designation[15];

public:
void add()
{
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter salary: ";
cin>>salary;
cout<<"\n Enter Designation: ";
cin>>designation;
}
virtual void display()
{
cout<<"\n NAME: "<<name<<"\n SALARY: "<<salary<<"\n DESIGNATION: "<<designation;
cout<<"\n";
}
friend void arrange(int);
}*faculty[100],f[100],*t;
class HOD:public faculty
{
char department[15];
public:
void add()
{
faculty ::add();
cout<<"\n ENTER DEPARTMENT: ";
cin>>department;
}
void display()
{
cout<<"\nHOD: ";
faculty::display();
cout<<"\n DEPARTMENT: "<<department<<endl;
}


}h[100];
void arrange(int n)
{

for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(strcmp(faculty[j]->name,faculty[j+1]->name)>0)
{

t=faculty[j];
faculty[j]=faculty[j+1];
faculty[j+1]=t;
}
}
}
}
int main()
{
int i=0,ch,j=0,k=0;
do
{

cout<<"\n MAIN MENU \n1. ADD A HOD\n2. ADD A FACULTY\n3. DISPLAY LIST\n ENTER UR CHOICE: ";
cin>>ch;
switch(ch)
{

case 1:

h[j].add();
faculty[i]=&h[j++];i++;
break;

case 2:

f[k].add();
faculty[i]=&f[k++];i++;
break;
case 3:arrange(i);
for(int j=0;j<i;j++)
{
faculty[j]->display();
}

break;
default: cout<<"\n WRONG CHOICE";
}
cout<<"\n CONTINUE (0/1) ??";
cin>>ch;
if(ch==0) break;
} while(1);
}

correct me if i m wrong

arjita07@ program seems to be fine to me .. thanks ..
but i need to correct and understand the earlear version ( vector version ) . Any suggestion is appreciated .
Thanks again
Topic archived. No new replies allowed.