inheritance

i m studying the multiple level of inheritance.This is my code.but i have error in this line:
subject(string facultlyname,string studentsname,string code)
: facultly(facultlyname,studentsname),code(code){}

the compiler said that need declaration b4 {,can anyone tell me the error?thanks~
there is also an error saying that there is an ambiguity for the base facultly.Why?

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
#include <iostream>

using namespace std;

class facultly
{
    protected:
    string facultlyname;
    string studentsname;
    public:
    facultly (string facultlyname,string studentsname);
    string getfacultlyname() const;
    string getstudentsname()const;
};

facultly::facultly(string facultlyname, string studentsname)
{
    this ->facultlyname=facultlyname;
    this ->studentsname=studentsname;
}

string facultly::getfacultlyname()const
{
    return facultlyname;
}

string facultly::getstudentsname()const
{
    return studentsname;
}

class subject: public facultly
{
    protected:
    string code;
    public:
    subject(string facultlyname,string studentsname,string code)
        : facultly(facultlyname,studentsname),code(code){}
    string getcode()const;
};

/*subject::subject(string code,string studentsname,string facultlyname)
    :facultly(facultlyname,studentsname)
{
    this-> code = code;
}*/

string subject:: getcode()const
{
    return code;
}

class students: public facultly, public subject
{
    public:
    students(string studentsname, string code, string facultlyname)
        :facultly ( facultlyname,studentsname),subject(facultlyname, studentsname,code){}
};
Last edited on
It compiles without error here (though I do get an ambiguity warning). Can you post the actual error instead of just paraphrasing it? And can you tell us what line it's on? (EDIT: you did tell us what line -- I just wasn't reading. Sorry)


Your larger problem here is that of design. The way your classes are inherited don't make any sense.

Inheritance forms an "is a" relationship. For example, if "Poodle" is derived from "Dog", that implies that a Poodle "is a" dog.

Here, you're deriving subject from faculty, implying that a subject "is a" faculty, which makes no sense.

Furthermore, you're deriving students from faculty and subject -- when subject is already derived from faculty. This results in you have two separate faculties as parents. This is what is causing your ambiguity errors.

Rethink your design. None of these classes make sense.
Last edited on
warning: direct base 'facultly' inaccessible in 'students' due to ambiguity
error:expected declaration before '}' token

Disch: i get what u mean. if let said i got facultly, student, subject, then how should i link together?
i got facultly, student, subject, then how should i link together?


If you only have those 3 classes and no others, then inheritance doesn't apply. None of them are related.

The only way I can think to apply inheritance would be to make a class person and have faculty and student derive from it (since students and faculties are both people, that would make sense). However that doesn't necessarily mean it's practical to do that.

As for tying it with a subject... maybe make something like a classroom class?

1
2
3
4
5
6
7
8
9
10
11
class classroom
{
  //...

private:
  faculty teacher;
  subject classsubject;
  std::vector<student> students;

  //...
};

Last edited on
actually this is an assigment, we need to relate it by inheritance, any idea?
I would have to say that assignment is screwed up (unless I'm missing something). Because I see no relation between any of those classes...
Maybe you're supposed to change them to have an inheritance relationship...?
Problem Statement
You are required to develop a system that models a subject registration system in a university. A student may register for many subjects and a subject may have many students. The attributes of subject and student are listed below:
· Subject: Code, faculty (faculty offering the subject), students (students taking the subject)
· Student: Name, faculty, subjects (subjects taken by the student).

maybe this is clearer.
Nothing in the assignment description you posted suggests you need to use inheritance.

"Attributes" generally mean "member variables". So if code, faculty, and students are attributes of Subject, then Subject has 3 member vars with those names (similar to my "classroom" example above). That doesn't mean you inherit.
Last edited on
this is the criteria given to complete the assignment.
1.1. Style (indentation, self-documentation, identifier) & Modularity (small size functions/methods)
1.2. Composition
1.3. Inheritance
1.4. Dynamic Polymorphism

2.2. Correct program features and results (all attributes must be shown during listing)
2.2.1. Correctly list all students after creating student(s)
2.2.2. Correctly list all subjects after creating subject(s)
2.2.3. Correctly list students from a specified subject
2.2.4. Correctly list subjects of a specified student
2.2.5. Correctly list students of a subject after a student registers for the subject
2.2.6. Correctly list students of a subject after a student drops the subject
2.2.7. Correctly list subjects of a student after the student registers for a new subject
2.2.8. Correctly list subjects of a student after the student drops a subject
Topic archived. No new replies allowed.