I don't give a damn...

9:12:00 PM

Osaka Power

It's been several days since I finished my first semester. And there was a subject named "Computer Programming" mainly focusing on C and C++. I will say I have more or less enjoyed it. :)
Because I'm not a student of Computer Science, I am not getting course on this subject in the future; kinda saddens me. :(

However, C/C++ is not free from typical mambo jumbo... Actually, C++ starts  with the benefits of OOP and describing it's terminology etc. One of them is Encapsulation. Encapsulation is some kind of mechanism that keeps a class safe from external exploitations, i.e. loosely saying, it provides a way that a variable will not change without a defined way. In C++ classes provides a way to encapsulation. And to ensure encapsulation value of a private variable of a class must not be changed outside the class without using it's public members (I'm really not sure if I've explained it well).
Well, I'm not going to stick to that principle. Let's change a private member without accessing it's public members. Just look at this short program:

#include "iostream"
using namespace std;

class A{
    int a;
public:
    A(int x) {a=x;}
    int *getaddress() {return &a;}
    int geta() {return a;}
};

void changeit(int *a)
{
    *a=88;
}

main()
{
    A ob(7);
    int *p;

    cout<< ob.geta() <<'\n';

    p=ob.getaddress();

    *p=9000;

    cout<< ob.geta()<<'\n';

    changeit(p);

    cout<< ob.geta()<<'\n';

    return 0;

}

What it does is that it gets the address of that variable that is in the class A. And if you can get address of a variable, you know that you can really change that thing to anything you desire no matter where are you in a program. The results can be achieved through reference parameter also.... :D

Well, in real life programs in a class there won't be any functions that will allow one to get the address of a private variable to preserve encapsulation. Well, there might be, but that's probably needed for that program...

Actually i wrote this blog because I am not writing for several months and it seemed that my blog is orphaned... :(
Well now you know it's not... :D


You Might Also Like

0 comments

Popular Posts

Like us on Facebook

Flickr Images