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. ......


 

2 comments: