How to Access Private Data Members in C++ Without Using Friend Function

You can refer the below YouTube Video in which I have explained the complete program. Example code is given below. 

#include <stdio.h>

#include <stdlib.h>
#include <conio.h>
#include <string.h>

class Test

int privateData;

Test(int data1)
privateData = data1;
void display();

void Test::display()

printf("Value of private data = %d\n", privateData);

int main()

Test obj1(97);
printf("Before Change:\n");

int *p = (int *) &obj1;
*p = 43;
printf("Violate the Object Oriented Programming Concept\n");
printf("After Change:\n");

return 1;

Accessing the private data using this type of algorithm is against Object Oriented Concept. 

Related Post:

Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault ?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in linux with c example code

Share on Google Plus

About Kapil Thakar

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment


Post a Comment

Related Posts Plugin for WordPress, Blogger...