0

hello i am trying to create a class that uses inheritance from class Student to class Graduate Student but program says it is inaccessible.


    class Student
{
    Student(char* n, int id)
    {
        name = new char[strlen(n) + 1];
        strcpy_s(name,strlen(n)+1, n);
        studentId = id;
    }
    ~Student()
    {
        if (name != NULL)
            delete[]name;
    }
    void printStudent()
    {
        cout << "Student ID: " << studentId << "\nStudent Name: " << name << endl;
    }
protected:
    int studentId;
    char* name;
};
class GraduateStudent :public Student
{
public:
    GraduateStudent(char* n, char* a, int id) : Student(n, id)
    {
        area = new char[strlen(a) + 1];
        strcpy_s(area, strlen(a) + 1, a);
    }
    ~GraduateStudent()
    {
        if (area != NULL)
            delete[]area;
    }
    void printGrad()
    {
        printStudent();
        cout << "Research area: " << area << endl;
    }
private:
    char* area;
};

the program is supposed to print out an initiallised GraduateStudent class variable be calling printStudent();

  • 3
    you forgot to add `public:` at the top of `Student` class, by default access level for class is private (for struct its public so you could alternatively make `Student` a struct) – Borgleader Apr 02 '21 at 12:53
  • 1
    offtopic: have mercy: `name = new char[strlen(n) + 1]` for anyone (yourself) who will read this code. Just use `std::string`. – Marek R Apr 02 '21 at 12:56
  • This doesn't address the question, but you don't have to test for a null pointer before deleting it. `delete[]area;` will work just fine, even if `area` is null. Further, given the code above, `area` won't ever be null, so the test is doubly pointless. – Pete Becker Apr 02 '21 at 13:37

3 Answers3

0

Default access for a class is private, which makes OP's functions private. So, your constructor is private. You should read this. Also, does this answer your question?

thx for edits @sweenish

Tuna
  • 113
  • 1
  • 11
0

You declared int studentId; and char* name; as protected, so in GraduateStudent you can access them, so you can easily do:

void printGrad() {
  cout << "Student ID: " << studentId << "\nStudent Name: " << name << endl;
  cout << "Research area: " << area << endl;
}

or, alternatively, declare printStudent(); in the public part of Student. In fact, if you declare something without any kind of specification, in a class, it is private by default, while in a struct, it is public.

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
0xNIC
  • 135
  • 2
  • 10
0

When you are creating your classes, you may define access modifiers or access specifiers.(https://en.cppreference.com/w/cpp/language/access) (These are those words such as public, private or protected in your class). You may find [doc here]

class myClass{ 
   public:
      // public members go here
   protected:
      // protected members go here
   private:
      // private members go here
};

To make it simple, they allow you to tell whether a member should be accessible from everywhere, from derived classes only or not at all from outside the class where it is defined. When you do not put anything, in a C++ class, it means that it is private by default.

Thus your printStudent() method is private and cannot be accessed from your GraduateStudent class.

A good starting point for more details and more understanding of the notion is to read some tutorials

To solve your compilation problem, you should make your Student constructor, destructor and printStudent as public to allow the derived class to access them and compile.

class Student
{
 public:
    Student(char* n, int id)
    {
      ...
    }
    ~Student()
    {
      ...
    }

    void printStudent()
    {
      ...
    }
....
};
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27