user11937123
user11937123

Reputation:

How to write a private attribute of a class?

I was working on a class which will contain an array of objects of another class. But I'm not able to do so. Here is a simplified version of the code I'm trying to use. Can anyone help me work with it?

Why s cannot access its own data members?

Can anyone show the whole concept of how to deal with these kind of scenarios?

P.S - ONLY USE of FRIEND (class or function) keyword. Inheritance isn't allowed.

class Student{
private:
    int roll;
    char name1[15];
};

class Subject  {
private:
    Student s[10];
};

int main()
{
    Subject s1;
    s1.s[1].roll = 5; // trying to set roll of 1st student
    return 0;
}

I was expecting that value will be assigned, but it has the following error. Why it is so?

error: 'Student Subject::s [10]' is private`
error: 'int Student::roll' is private
error: 'Student Subject::s [10]' is private
error: 'int Student::roll' is private

Upvotes: 0

Views: 1292

Answers (4)

R Sahu
R Sahu

Reputation: 206717

Can anyone help me how work with it. why s cannot access it's own data members.

It sure can but the error you are seeing is the result of trying to access private members of Subject and Student in main.

main cannot access private members of Subject or Student. In order to allow main to access the member variables directly, you'll have to make the member variables public. The other option is to add public accessor and modifier functions corresponding to the private member variables.

Upvotes: 3

stylo
stylo

Reputation: 506

The student array is declared as a private member of the Subject class.

private members cannot be accessed outside of the class scope, which means you cannot access it from your main (or any other) function.

the first option for you is to declare this array as public member. the other one is to use getter/setter for that member which will get/modify this member

class Student
{
private:
    int roll;
    char name1[15];

public:
    char* getName(void)
    {
        return strdup(this->name1); 
    }

    void setRoll(int r)
    {
        this->roll = r;
    }
};

class Subject
{
private:
    Student s[10];

public:

    void setStudentRoll(int roll, char *s_name)
    {
        for (int i = 0; i < 10; i++)
        {
            if (strcmp(this->s[i].getName(), s_name) == 0)
            {
                this->s[i].setRoll(roll);
                break;
            }
        }
    }
};

keep in mind that in order to set the student's roll member (which is also declared as a private member), you will have the same problem.

Upvotes: 2

Yunnosch
Yunnosch

Reputation: 26763

If you want to make an attribute private but still intend to allow it to be set, then the commonly used idiom is to create a "setter".

In this case, you need to do it twice, for each of your classes with private attributes that you nevertheless want to set.

class Student{
    public:
        void setroll(int newroll)
        {
            roll = newroll;
        }
    private:
        int roll;
        char name1[15];
};

class Subject  {
    public:
        void setstudentsroll(int newroll, int index)
        {
            /* a good setter would at this point check the index,
            to make sure it does not access beyond the array */
            s[index].setroll(newroll);
        }
    private:
        Student s[10];
};

int main()
{
    Subject s1;
    s1.setstudentsroll(5,1); // trying to set roll of 1st student
    return 0;
}

Using the friend keyword for this is possible for the inner case, where the accessing code is in a class that can be named. But, and I assume that this is the actual problem you are having, it does not help for allowing main() to do the setting.

Upvotes: 2

AravindK
AravindK

Reputation: 151

You are declared the variable and student Object has an Private access specifier. you cann't initialize the variable values out side that class. using public you can initialize the variable anywhere on the program... Solution:

class Student{
 public:
 int roll;
 char name1[15];
 };
 class Subject  {
 public:
 Student s[10];
 };
 int main()
 {
  Subject s1;
  s1.s[1].roll = 5; // trying to set roll of 1st student 
 return 0;
 }

Upvotes: 0

Related Questions