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();
} |
|
|