inheritance
Jun 20, 2013 at 1:58pm UTC
i need help how can i show the information tha entered and how can i access the classes i have created in the main function. . .
here is my code
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
#include <iostream>
using namespace std;
const in LEN = 80;
////////////////////////////////////////////////////
class student
{
private :
long int idno;
char surname[LEN];
string name[LEN];
public :
void getdata(){
cout << "Enter IDno. \n " ; cin >> idno;
cout << "Enter Surname \n" ; cin >> surname;
cout <<"Enter Name \n " ; cin >> name;
}
void dispaly(){
cout << "\n IDno. : " << idno;
cout << "\n Surname : " << surname;
cout << "\n Name : " << name ;
}
};
/////////////////////////////////////////////////////
class lecture: public student //lecture class
{
private :
char title;
char surname[LEN];
public :
void getdata(){
student::getdata()
cout << "Enter title: " ; cin >> title;
cout << "Enter surname: " ; cin >> surname;
}
void display(){
student::dispaly()
cout << "\n Title: " << title << ". " << surname;
}
}
//////////////////////////////////////////////////////////
int main()
{
}
Last edited on Jun 21, 2013 at 10:12am UTC
Jun 20, 2013 at 2:36pm UTC
Please use code tags so that your code appears properly formatted, so that the people who might be able to help you can read it clearly.
Why is lecture inheriting from student ? That doesn't seem like an "is-a" relationship to me?
Jun 20, 2013 at 7:54pm UTC
If Lecture pertains to a lecture class then a Lecture should contain a Student or more specifically, a container of Students.
No inheritance is needed.
Last edited on Jun 20, 2013 at 7:56pm UTC
Jun 20, 2013 at 10:24pm UTC
So if the teacher gets sick, the lecture is cancelled and all the students die
Just saying that the lecture may no have ownership of the students.
> how can i access the classes i have created in the main function
You operate with objects, no with classes
1 2 3 4 5 6 7
int main(){
student freeman;
lecture programming;
freeman.getdata();
freeman.enroll( programming );
}
Jun 21, 2013 at 11:44am UTC
i have already changed it cause this how i want it to be:
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
using namespace std;
const int LEN = 80;
////////////////////////////////////////////////////
class school
{
private :
char courselevel[LEN];
char course[LEN];
public :
void getdata(){
cout << "Enter course level " ; cin >> courselevel;
cout << "Enter course name " ;
cin >> course;
}
void display(){
cout << "\n Course level : " << courselevel;
cout << "\n Name of course : " << course;
}
};
/////////////////////////////////////////////////////
class lecturer: public school //lecture class
{
private :
char title; //"Mr." etc
char surname[LEN];
public :
void getdata(){
school::getdata();
cout << " Enter title: " ; cin >> title;
cout << " Enter surname: " ; cin >> surname;
}
void display(){
school::display();
cout << "\n Title: " << title << ". " << surname;
}
};
//////////////////////////////////////////////////////////
class student : public school
{
private :
int studentnum;
char name;
char lastname[LEN];
public :
void getdata(){
school::getdata();
cout << " Enter student number: " ; cin >> studentnum;
cout << " Enter name: " ; cin >> name;
cout << " Enter last name: " ; cin >> lastname;
}
void display(){
school::display();
cout << "\n Student number: " << studentnum;
cout << "\n Name: " << name;
cout << "\n Last name: " << lastname;
}
};
//////////////////////////////////////////////////////////////////////////
int main()
{
lecturer l1;
student s1;
// gets data for several employees and students
cout << "\n Enter data for lecturer: " ;
l1.getdata();
cout << "\n Enter data for student: " ;
s1.getdata();
// display for several employees and students
cout << "\n Data for lecturer " ;
l1.display();
cout << "\n data for student" ;
s1.display();
system("pause" );
return 0;
}
and this is the out put:
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
Enter data for lecturer:
Enter course level bsc
Enter course name it
Enter title: mr
Enter surname:
Enter data for student:
Enter course level bsc
Enter course name it
Enter student number: 50178
Enter name: freeman
Enter last name:
Data for lecturer
Course level : bsc
Name of course : it
Title: m. r
data for student
Course level : bsc
Name of course : it
Student number: 50178
Name: f
Last name: reeman
Press any key to continue . . .
how can make it show all the info that i enter cause it doesn't allow me to enter lastname and surname it moves on to the last part . . .
Jun 21, 2013 at 1:35pm UTC
Your inheritance still makes no sense. Is a lecturer a type of school? Is a student a type of school?
I think you need to go back to your textbook and read up on what inheritance is actually for .
Jun 21, 2013 at 2:31pm UTC
@freemanl
There are 3 types of relationships:
1. "IS A" implies inheritance.
2. "HAS A" means a member variable has the type of another class.
3. "USES A" means a variable of another type of class is a parameter / argument to a member function.
Note that one doesn't have to fit everything into an inheritance tree.
As MikeyBoy says, read all about it.
HTH
Jun 25, 2013 at 9:32am UTC
thanks a lot didn't know . .. ..
Topic archived. No new replies allowed.