Skip to main content
Question Protected by gnat
formatting kaizen
Source Link
gnat
  • 20.5k
  • 29
  • 117
  • 309

Using pointer, I am able to modify the private value of the class in the code below.

Does it violate the C++ concept that private member can only be modified by member or friend functions?

#include<iostream>
using namespace std;

class demo
{
        private: int info;
        
        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }
};

int main()
{
    
     demo ob;
     int* ptr=(int*)&ob;
   
     *ptr=20;

     ob.print_info();

     return 0;
}

Using pointer, I am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend functions?

#include<iostream>
using namespace std;

class demo
{
        private: int info;
        
        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }
};

int main()
{
    
     demo ob;
     int* ptr=(int*)&ob;
   
     *ptr=20;

     ob.print_info();

     return 0;
}

Using pointer, I am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend functions?

Using pointer, I am able to modify the private value of the class in the code below.

Does it violate the C++ concept that private member can only be modified by member or friend functions?

#include<iostream>
using namespace std;

class demo
{
        private: int info;
        
        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }
};

int main()
{
    
     demo ob;
     int* ptr=(int*)&ob;
   
     *ptr=20;

     ob.print_info();

     return 0;
}
fix layout
Source Link
Kilian Foth
  • 111k
  • 45
  • 301
  • 323

#include using namespace std;

class demo { private: int info;

#include<iostream>
using namespace std;

class demo
{
        private: int info;
        
        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }

};

int main() {

};

int main()
{
    
     demo ob;
     int* ptr=(int*)&ob;
   
     *ptr=20;

     ob.print_info();

     return 0;
}

}

Using pointer i, I am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend functionfunctions?

#include using namespace std;

class demo { private: int info;

    public:
             demo()
             {
                     info=10;
             }

             void print_info()
             {
                   cout<<info;
             }

};

int main() {

 demo ob;
 int* ptr=(int*)&ob;

 *ptr=20;

 ob.print_info();

 return 0;

}

Using pointer i am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend function

#include<iostream>
using namespace std;

class demo
{
        private: int info;
        
        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }
};

int main()
{
    
     demo ob;
     int* ptr=(int*)&ob;
   
     *ptr=20;

     ob.print_info();

     return 0;
}

Using pointer, I am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend functions?

Source Link
curious
  • 119
  • 1
  • 1
  • 2

changing value of a private member of a class in c++ without using member or friend function

#include using namespace std;

class demo { private: int info;

    public:
             demo()
             {
                     info=10;
             }

             void print_info()
             {
                   cout<<info;
             }

};

int main() {

 demo ob;
 int* ptr=(int*)&ob;

 *ptr=20;

 ob.print_info();

 return 0;

}

Using pointer i am able to modify the private value of the class. Does it violate the C++ concept that private member can only be modified by member or friend function