17 Feb 2009 : Overall comments for OOPS submitted by 15 Feb 2009

a) Fields within the class Employee cannot be accessed in the main program.
   Normally this fields are declared as private or protected.

For example, class Employee has been defined as
class Employee { 
private: string id,name,tel,type
};

void main(){
      cout << name;  // the 'name' here is not the Employee name. Just meaningless.
}
What object does "name" belongs to?
If you "cout << name" in a member function e.g. show() 
void Employee::show(){
  cout << name << endl; // the name is an attribute of Employee
}

void main(){
  Employee x,y.z; // x,y and z are 3 employees of the company
  x.show();  // show the name of x (not of y or not of z)
  y.show();  // show the name of y
}



For Employee we can defined many employees objects.
Employee x,y,z;  // defines 3 employees x,y and z 
                 // and each object has a name.

//How is the name of x,y,z accessed?
//How is the name of x,y,z updated?


b) You must have constructor in each class definition.
  The constructor is used to give an initial value to the class object.
  This is called "initialisaton".
  e.g.
  void main(){
    int x,y,z;
    cout << x << endl;  // what is the value of x? Not known
    z=3; // now z has a value
    cout << z << endl;
  }
  In the above example x,y,z may have an initial value of 0, but NOT all compilers
  do that.
For OOPs
class Student {
  private:   
     string name;
  public:
     void show() { cout << name << endl;} // what name will be displayed;
};
void main(){
  Student p1,p2;
  p1.show();  // name shown is not known as value is not initialised
}
// To initialised use the construtor
Employee::Student() { Name = "";}


c) Writa a program either as an OOPs program or without using OOPs.
   Don't mixed up the two
   Don't define a class Employee and next 
   enter and save the record in the main program WITHOUT reference to OOP.
   or use  x.show();  where the data for x has NOT BEEN ENTERED at all

class Student {
 string name;
 public:
      void show() { cout << name << endl; }
};
void main(){
  Student x;
  string name;
  cout << "Name ";  getline( cin,name); // the "name" here is not the "name" defined in Student
  x.show(); // it will not show "name" entered in the previous line
}


d) Saving records in a file.
The format of file is given and save according to fixed length fields.
e.g. Name 20(A) and sex=1 char and classNo = 2 digit.
Save name in columns   1-20
Save sex  in column    21
save classNo in column 22-23
DON'T PUT A SPACE BETWEEN the fields.
 ----------------------------------------
 outfile << left << setw(20) << name << sex << setw(2) << classNO<< endl;// correct

Should not be
 outfile << left << setw(20) << name << " " << sex << " " << setw(2) << classNO<< endl;//wrong

For REPORTS you may leave space in betwen fields for readability 
but NOT for DATA file where the lengths of the fields have been fixed.


e) Don't create EXTRA fields in the  definition of your class.
e.g
class Employee {
public:
  string id, name, tel;
  char employee_type;
  int i,no_rec;         // should not be included
  ofstream outfile;     // should not be included
}
void main(){
   Employee x,yz; // The 3 empolyees x,y and z each  have 
                  // the field name,id,type and tel  
                  // but also include i,no_rec,outfile
                  // which SHOULD NOT BE part of Employee

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

FEB19 2009 Comments on submitted project work

f) String type and cstring type
-------------------------------
This are two different types of 'string'.

cstring type use 'char'

For cstring operation  use of  = or +  is an ERROR
e.g  
char  name1[31], name2[31];  // for name1 and name2
name1 = "Ah Kow";  // this is wrong
strcpy( name, "Ah Kow");  // this is correct
strcpy(name2,name1);   // name2 = name1

name2="TAN " + name1;  // this is wrong
strcpy(name2,"TAN ");  // name2 = "TAN"
strcat(name2,name1);  // name2 = name2 + name1

// to enter a name into name1 use
cout << "enter name ";
cin.get(name1, 31, '\n'); cin.get();

For string type, you can use + and = 
string name1,name2;
name1 = "Ah Kow";
name2 = "Tan ";

To enter name
cout << "Enter name ";
getline( cin,name1);
name2 = name2 + name1;

You should use string type for fields like
EG: name,employee_id,address.
    cout << "name "; getline(cin,name);

For single char, use char type 
EG:  char  type_employee; // 'H' or 'S'
     cin.get(type_employee); cin.get();

for telephone, you can use long
long telephone;   //  long integer
     cin >> telephone;  cin.get();

PREFERABLY  KEEP TO STRING TYPE IN YOUR FIELD DEFINITION

g) The CREATEEMPLOYEE should NOT be a member function 
of Employee.

The algorithm for CREATEEMPLOYEE could be
void CREATEEMPLOYEE {
   open output file("employee.dat") 
   Check for file error if any
   Enter the deptName and no of records.
   Store as deptName and No of records as header in output file
   for 1 to No of records {
      enter a record   //  x.enter()  where Employee x;
      save a record    //  x.save()   'x' is an employee
   }
   close file
}

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

Other Comments

h) Demo program to read in data from file  class.dat. Not using OOPs
-------------------------------
4
013Ab beng             AGPMACS
022Ah Seng             SGPMACS
054Ah Mong             CGPECCS
126Ah Seng             SGPHAMA
-------------------------------
* Reading of file
There is a 'file pointer' that is placed at the begining of file when it is opened.
As the record is read, the pointer moves to the next position.
For e.g. in the above file:
reading in one line (which is the first line): dept and no_rec
The pointer goes to the second line.
reading in another line.
The second line, the second line "013Ab beng             AGPMACS" is read
and pointer goes to the 3rd line.
Continue reading will move the pointer down until it reaches
the  "end of file"

// DEMO PROGRAM TO READ IN THE ABOVE FILE
ifstream infile;            // ifstream is for input file
infile.open("class.dat");  // open the file class.dat as an input file
if (!infile) {
  cout << "File not found " << endl;
  exit(1);        // abort processing
}


// get the number of records '4' from the input file (the first line)
int i,no_rec;
  infile >> no_rec; infile.get();    // reads in no_rec =4


// loop to read in rest of file   
string one_rec, name, subj1,subj2,subj3 ;
long idno;
char stream;
  for (i=0;i

    Source: geocities.com/tpjc_sg