Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is wrong with the following code? class classA { protected: void setX(int a

ID: 3905694 • Letter: W

Question

What is wrong with the following code?

            class classA

            {

            protected:

                        void setX(int a);                      // Line 1

                        //postcondition: x = a;             // Line 2

            private:                                                // Line 3

                        int x;                                        // Line 4

            };

            .

            .

            .

            int main()

            {

                        classA aObject;                       // Line 5

                       

                        aObject.setX(4);                      // Line 6

                        return 0;                                   // Line 7

            }

Explanation / Answer

error here is protected members or members of class can be accessed by members of same class and they cannot be accessed by object references. Error here is we are trying to access setX() of classA using object of that clss which was not possible. That was the error.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote