Sunday 19 February 2012

Encapsulated SIS ?? PART II

Have you read the

Encapsulated SIS ??  PART I

if  not read it first ...

you might wonder why I put "Encapsulated SIS ??" as the title of this post. You know I created this project to show Object Oriented Concepts. I implemented inheritance, polymorphism, Overloading, Overriding and re usability . But I screwed up it. I misused the main concept "Encapsulation". that's why I made that as the topic.

Problem was I had classes for student ,staff and modules. I used separate classes to store details about each entity and separate class to manipulate or manage the entity. (student.cpp and studentmng.cpp). Encapsulation is a language mechanism for restricting access to some of the object's components and a language construct that facilitates the bundling of data with the methods (or other functions) operating on that data. Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. So I should define the student name,id, phone and such details as 'private' . But in my case I wanted to method that it can access in studentmng.cpp. so I made those attributes as public, otherwise I cant access them.

public:
     int stdno;
     char Gender;
     char dob[15];
     char Batch[10];
     char joined[15];
     char nationality[30];
     Student(int pstdno,char pname[],char pnic[],char pGender,char pdob[],char pBatch[],char pjoined[],char pnationality[]);
     Student(void);
     ~Student(void);
     Student *nextStudent;

So I violated the encapsulation. ;) I figured out after I submitted to my friend. Any way I learnt a big lesson from it. Never define a class like this. 

private:
     int stdno;
     char Gender;
     char dob[15];
     char Batch[10];
     char joined[15];
     char nationality[30];
     Student(int pstdno,char pname[],char pnic[],char pGender,char pdob[],char pBatch[],char pjoined[],char pnationality[]);
     Student(void);
     ~Student(void);
     Student *nextStudent;
     int getStdno();

     char* getBatch();

     char* getNationality();

     int setStdno();

     char* setBatch();
     char* setNationality();

This is the correct way and I defined some getters and setters also. You might confused that where is the name of the student. I created a class call 'Person' and put those common details inside that and inherits that by student and staff.

class Staff: public Person {..}
 

I had another problem ..

When I testing a input value to the name field in student it always gives skip the next step. At last I found that when I input the name with a space in between, second name goes to the next cin.  I correct it like this. There so many methods inside cin class..

             cout<<"Enter student's name (eg: Saman Kumara) : ";
             cin.ignore();

             cin.get(aname,50);

This is an alternative for the default cin>> command.

istream&  ignore ( streamsize n = 1, int delim = EOF );

Extracts characters from the input sequence and discards them.

The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.

http://www.cplusplus.com/reference/iostream/istream/ignore/

istream& get ( char* s, streamsize n );
istream& get (char* s, streamsize n );
Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline if you want this character to be extracted (and discarded).
The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.
http://www.cplusplus.com/reference/iostream/istream/get/



Do you know how to override funtions in C++? Its little bit different and confusing..

see what I used.. it called a virtual function.

class Personmng
{
public:
     Personmng(void);
     ~Personmng(void);
     virtual int maxno()=0;
};

See some more overriding methods in C++

  • Explicit overriding

  • Renamed overriding

  • Multiple overriding

http://www.codeproject.com/Articles/7498/Function-overriding-in-C-CLI

http://www.cplusplus.com/forum/general/53977/

http://www.cplusplus.com/forum/general/61557/

http://www.cplusplus.com/forum/general/10932/

I used a nice concept.. I did this using Linked Lists. So I needed a way to map the relations between classes.. So I first used student number and staff number as a attribute in module. But at last I thought that its better if can I use the whole objects as attributes. Then I can easily get connected to all the attributes in student and Staff as well.

So my code was...


class module
{
private:
     Student* st;
     Subject* sj;
     int semester;
     int ass1mrks;
     int ass2mrks;
     int finalmrks;
     module* nextmodule;
     module(Student* pst,Subject* psj,int psemester,int pass1mrks,int pass2mrks,int pfinalmrks);
     ~module(void);
};

The nice thing is it is an existing concept called ORM (Object Relational Mapping).. I never knew that there was something like this when I'm using. I heard and leant it first time at my training program at hSenid Software International Pvt Ltd.

See the real concept here (ORM - wikipedia)

These are some screen shots from my project...












 

 

Thank you for reading about my projects.. This is some kind of experience that I gained through out my projects.. I want to share them with you all.. .Hope you got something from it.. And there can be definitely many ways to a same task .. So I expect  you will share them with me as well.. Comment your valuable feedback.. valuable ideas.. Even an Objection.. All are welcome....

I have many more to show you... I will upload about my other projects as well... and some new technologies... I have a big java project to share my experience with you. ......


 

Thursday 16 February 2012

Encapsulated SIS ?? PART I

Recently I did a project for a good friend of mine who is studying at another University. He got the problem to design a Student Information System using Object Oriented Concepts. But the Language was C++. Worst this was it was command line project means not to deal with any GUI. It was little bit hard, since I haven't done such kind of big project using command prompt, even in Java. I'm more familiar with Java.

But we did a C++ project at SLIIT as a group project. It was a system for a Phamacy. "Good Health Pharmacy". It was an quite interesting projects because it was GUI based project. I'm the one who designed all the interfaces in colorful manner.. I did less programming part there.Most of the core codes were done by my friend Tharindu Edirisinghe.


It was based on Linked lists, which was dynamic data.

So since i had this project I referred this software to do the SIS. There were no limitations and I had to consider about Object Oriented Concept and command prompt. I used linked lists as the data storing method. ..Actually it was a real challenge to me.

I used Visual studio 2008 as a IDE and use visual C++.

This is the main menu..

 I used switch caseto go through the menu items as usual.

PROBLEM?? "what if the user enters a letter rather than typing any number ? " Anyway it should be validated to manage all the inputs..Since  I have used  'int ' type variable to catch the users selection.

Yes we can filter these numbers using case statements.. BUT it gave me a exception to all invalid inputs like letters.

I googled my problem and this is what I found...

int a;
    cout<<"Your Selection : ";
    cin>>a;
    while (!cin.good())
    {
        system( "CLS" );
        cin.clear();
        cin.ignore(1024, '\n');
        cout << "Invalid Selection Type only digits..."<<endl;
        cout<<"Your Selection : ";
        cin>>a;
    }

here cin.good() true if the data type matching succeed. if not cin.clear(); will clear the last cin and cin.ignore(1024,'\n') will ignore the characters of 1024 buffer size up to a new line character. Then it re prompt to get the input.

For the most of the C++ problems I found the correct answers from
http://www.cplusplus.com/

Actually one of the good site to refer for C++ beginners.

I used a user log in part in this. so I needed a way to hide what I type in password field (in windows command prompt.)

we should include "#include <windows.h>"

    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);

    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

    cin>>pass;
    cout<<endl;
    SetConsoleMode(hStdin, mode & (ENABLE_ECHO_INPUT));

Even it was bit difficult for me. I understood the part ~ENABLE_ECHO_INPUT  only it something like disabled the echo mode.
 I got this from 

http://www.cplusplus.com/forum/beginner/61174/

refer http://msdn.microsoft.com/en-us/library/windows/desktop/ms683167%28v=vs.85%29.aspx to get an idea about GetConsoleMode function.

Hope you got something out of it.... :)

****To be continued. please see  'Encapsulated SIS ?? PART II

Sunday 12 February 2012

Who I am...

Hi,
Sasika Suchithra Kalupahana.. ( SS )  Simple man who likes programming and do innovative things in IT field.
Officially I'm an Undergraduate at Sri Lanka Institute of  Information Technology (SLIIT). Now in 3rd year and I'm following B.Sc. in IT Sp. Hons Degree program. Just started my career life in IT field at hSenid Software International Pvt Ltd. as a Trainee Associate Software Engineer.     

http://www.bleachernation.com/wp-content/uploads/2011/10/happy-kid-at-computer.jpg
Programming..  started from Pascal at School. Never knew what we are doing. Just typed what instructor gave us at class.. But knew programming is an interesting subject, because it is dealing with computer. I didn't have a computer at Home. Though I typed just few words and see nothing I loved that subject and never missed that class. I had a dream to be an IT specialist. When someone asked  "Son, whom do you like to be in future ?" I always answered "Software Engineer" even I don't know the meaning of it and while Most of the friends answered "Doctor" for the same question.
Finally I got a computer after I end up my school life. Wow it was amazing. I always wondered how this this is working, since I can do so many things with this unit. Rapidly I changed and I found my track. I learned lots of softwares in few months on my own. Specially Adobe tools, Photoshop Illustrator. not in master level but  managed to do most of the things.
http://bostinno.com/wp-content/uploads/2011/05/programming.jpg

Then I started learning on Programming. I did a practical course in Java. which was most popular language in my world. I felt more familiar  to it. But after I started my degree I learned C, C++, C# ASP .Net... since I was more familiar with Java I grabbed most of the things quickly. It was easy for me. Most of the things I learned in my own. Getting errors is and fixed them in my own (anyhow) was a challenge for me, rather than asking from anyone to fixed it.

Finally I 'm in 3rd year of my undergraduate life now. I have some experience in programming. I'm NOT AN EXPERT in Programming. I'm a fresher. Still I know only a drop from this Ocean. But I would like to share even my drop with you all. I wanted to give guide from sharing my experience with you. I would like if you can show me up if I did something wrong. This my main objective of this Blog.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvvj7NuKtiGN_O3mcD-vybhYkcKV0NGf5TE3KbQizqx54RNqjhubep-SUUReneOiiU3UF053ag1w-CX2SgFmYFfY7XTo-iy7zNz9MZ8fQfn3I4w4IjoKYkSG5vjuK1ZxAFxSgjODOXK0g/s1600/b89a1b3657c3b87f.jpg
Programming is not a hard thing. just way of applying logic according to a given syntax. Syntax of any language can be learned in any way. Since we are having documentation and plenty of sample cording in internet. But analytical skills, thinking and applying the best logic is the most important thing. If you succeed on that, programming is nothing. Programming languages are nothing. Try to get much as you can from my blog. I know even a single word might be help full to you.
Thank you..