123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
Accessing Private Data Members without the friend keyword

--------------------------------------------------------------------------------

Description : This code shows how easy it is to access and modify private data members of a class from outside it's class without using the friend
keyword.


/*
ACCESSING PRIVATE DATA MEMBERS IN C++
-SANCHIT KARVE
born2c0de@hotmail.com


*/

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


class bestcoder
{
private:

char name[40];
char grade;
int age;

public:

bestcoder(char* nam="Some Dude",char gr='A',int saal=25)
{
strcpy(name,nam);
grade=gr;
age=saal;
}

friend ostream& operator <<(ostream& s,bestcoder b);
};

ostream& operator <<(ostream& s,bestcoder b)
{
s<<"Best Coder :"<<b.name<<endl
<<"His Rating :"<<b.grade<<endl
<<"Current Age :"<<b.age<<"

<BR>;
return s;
}

struct hackit
{
char name[40];
char grade;
char age;
};


void main()
{
bestcoder bc;
cout<<bc;
void* ptr=&bc;
struct hackit* bettercoder=(hackit*)ptr;
bettercoder->grade='F';
bettercoder->age=56;
cout<<bc;
strcpy(bettercoder->name,"Sanchit Karve");
bettercoder->age=14;
bettercoder->grade='A';
cout<<bc;
getch();
}

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG