Sunteți pe pagina 1din 26

FACULTY OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY

UNIVERSITY TUN HUSSEIN ONN MALAYSIA

SEM II 2010/2011

(BIT 2063/BIT 20603)

OBJECT-ORIENTED PROGRAMMING

LAB 4

NAME MATRIC NO.


NURUL HAMTA ATIKAH BINTI ABDUL HALIM CI090094

INSTRUCTOR NAME: PN. SHAZANA BT MD.ZIN

Lab 4/Tutorial 2
Bit 2063- object-oriented programming

BIT 2063

Object-Oriented Programming

(Pengaturcaraan Berorentasikan Objek)

1. Explain the concepts of encapsulation.


Encapsulation means both data and method for an object is contained inside the
object. When an object’s data is inside the object, the object can protect that data against
use by other objects.

2. What is the difference between information hiding and encapsulation?


Information hiding encapsulation
Only operations that preserve Both data and methods for an object
information correctness are are contained inside the object.
available to the client
Considered the implementation of Implementation of object can
certain operation change without having to change the
way the object is used by the rest of
the system

3. What is the difference between container and iterator?


Container iterator
Object that encapsulate and track Object encapsulates the internal
zero structure of how the iteration occurs
Bit 2063 Page 2
Bit 2063- object-oriented programming

Tracked in a first-in and first-out Design pattern are used to access


list the elements of an aggregate object
sequentially without exposing its
underlying representation.

4. Modify Code 5.2.1 so that the object is added at the end of the list.
/Filename: Hamtastd.cpp
//This program illustrate attributes of class Student
//using simple data structure

#include <iostream.h>

class Student
{
private:
struct Data
{
char Name[25];
char Course[30];
int Result;
}stdata;
public:
void SetData();
void GetData();
}; //class Student

void Student::SetData()
{
cout <<"Enter student name: ";
cin >>stdata.Name;
cout <<"Enter student course: ";
cin >>stdata.Course;
cout <<"Enter student Result:";
cin >>stdata.Result;

Bit 2063 Page 3


Bit 2063- object-oriented programming

}; //method SetData

void Student::GetData()
{
cout <<"\nStudent name: "<<stdata.Name;
cout <<"\n";
cout <<"\nStudent course: "<<stdata.Course;
cout <<"\n";
cout <<"\nStudent result: "<<stdata.Result;
cout <<"\n";
}; //method GetData

//Filename:atikahlist.cpp

#include <iostream.h>

#include "hamtastd.cpp"

class StudentList

Bit 2063 Page 4


Bit 2063- object-oriented programming

private:

struct ListNode

Student astudent;

ListNode *next;

};

ListNode *head;

public:

StudentList();

~StudentList();

int IsEmpty();

void Add(Student newstudent);

void Remove();

void DisplayList();

};

StudentList::StudentList()

head=NULL;

};

StudentList::~StudentList()

cout <<"\nDestructing the objects..\n";

Bit 2063 Page 5


Bit 2063- object-oriented programming

while(IsEmpty()!=0)

Remove();

if(IsEmpty()==0)

cout <<"All students have been deleted from a list\n";

};

int StudentList::IsEmpty()

if(head==NULL)

return 0;

else

return 1;

};

void StudentList::Add(Student newstudent)

ListNode *newPtr=new ListNode;

if(newPtr==NULL)

cout <<"Cannot allocate memory";

else

newPtr->astudent=newstudent;

newPtr->next=head;

head=newPtr;

Bit 2063 Page 6


Bit 2063- object-oriented programming

};

void StudentList::Remove()

if(IsEmpty()==0)

cout <<"List empty on remove";

else

ListNode *temp=head;

head=head->next;

temp->next=NULL;

delete temp;

};

void StudentList::DisplayList()

ListNode *cur=head;

if(IsEmpty()==0)

cout <<"List empty\n";

else

cout <<"Current List: \n";

while(cur!=NULL)

Bit 2063 Page 7


Bit 2063- object-oriented programming

cur->astudent.GetData();

cur=cur->next;

cout <<"\n";

} //end if

};// method display list

//Filename:studentlistdr.cpp

#include <iostream.h>

#include "atikahlist.cpp"

int main()

const int size=5;

int i;

Student newstudent;

StudentList alist;

cout <<"Inserting"<<size<<"objects\n";

for(i=0;i<size;i++)

newstudent.SetData();

alist.Add(newstudent);

alist.DisplayList();

Bit 2063 Page 8


Bit 2063- object-oriented programming

cout <<"deleting\n";

alist.Remove();

cout <<"after removing 1 object\n";

alist.DisplayList();

return 0;

};

Output:

Bit 2063 Page 9


Bit 2063- object-oriented programming

Bit 2063 Page 10


Bit 2063- object-oriented programming

5. Test your program with the same data so your output is displayed as in Diagram 1.
Diagram 1

Bit 2063 Page 11


Bit 2063- object-oriented programming

Input:

//Filename:student.cpp

#include<iostream.h>

class Student{

private:

struct Data{

char Name[25];

char Course[30];

int Result;

} stdata;

public:

void SetData();

void GetData();

};//class student

void Student::SetData(){

cout<<"Enter student name: ";

cin>>stdata.Name;

cout<<"Enter student course: ";

cin>>stdata.Course;

cout<<"Enter student result: ";

cin>>stdata.Result;

}; //method SetData

void Student::GetData(){

cout<<"Student name :"<<stdata.Name;

cout<<"\n";

cout<<"student course :"<<stdata.Course;

Bit 2063 Page 12


Bit 2063- object-oriented programming

cout<<"\n";

cout<<"student result :"<<stdata.Result;

cout<<"\n";

}; //method GetData

//Filename:stdListDr.cpp

#include<iostream.h>

#include"Student.cpp"

class StudentList{

private:

struct ListNode{

// the data of the list is an instance of class student

Student astudent;

ListNode *next;

};

ListNode *head;

public:

StudentList();

~StudentList();

// studentlist operations

int IsEmpty();

void Add(Student newstudent);

void Remove();

void DisplayList();

}; //class StudentList

//constructor and destructor implementation

StudentList::StudentList(){

head = NULL;

Bit 2063 Page 13


Bit 2063- object-oriented programming

};

StudentList::~StudentList(){

cout<<"\n Destructing the object.. \n";

//remove until list empty

while(IsEmpty()!=0)

Remove();

if(IsEmpty()==0)

cout<<"All student have been delate from a list\n";

};

int StudentList::IsEmpty(){

if(head==NULL)

return 0;

else

return 1;

};

void StudentList::Add(Student newstudent){

//create a new node

ListNode *newPtr = new ListNode;

if(newPtr==NULL)

cout<<"Cannot assign memory";

else

{ //assign successful

newPtr->astudent=newstudent;

//add a new node

newPtr->next=head;

head=newPtr;

};

Bit 2063 Page 14


Bit 2063- object-oriented programming

void StudentList::Remove(){

if(IsEmpty()==0)

cout<<"List empty on remove";

else

ListNode *temp=head;

head=head->next;

temp->next=NULL;

delete temp;

};

void StudentList::DisplayList(){

ListNode *cur=head;

if(IsEmpty()==0)

cout<<"List empty \n";

else

cout<<"Current List : \n";

while(cur!=NULL){

cur->astudent.GetData();

cur=cur->next;

cout<<"\n";

};

//Filename:StListDr.cpp

#include<iostream.h>

#include"StdListDr.cpp"

int main(){

Bit 2063 Page 15


Bit 2063- object-oriented programming

const int size=5;

int i;

Student newstudent;

StudentList alist;

cout<<"Inserting"<<size<<"objects\n";

for(i=0;i<size;i++){

newstudent.SetData(); //create an instance of class Student

alist.Add(newstudent); //add an instance of class Student to the list

alist.DisplayList();

cout<<"deleting\n";

alist.Remove();

cout<<"after removing 1 object\n";

alist.DisplayList();

return 0;

};

Output:

Bit 2063 Page 16


Bit 2063- object-oriented programming

Bit 2063 Page 17


Bit 2063- object-oriented programming

6. Try to delete data Cristina from your linked list. Then display the linked list after deletion
of Cristina data. (Hint: You need to modify your Remove function so it is able to find the
correct data and delete the data, and link the pointers back. Careful programming is
needed to avoid dangling pointers.

input:

#include <iostream.h>

class Student{

public:

struct Data {

char Name[20];

char Course [30];

int Result;

} stdata;

public:

void SetData();

void GetData();

};

Bit 2063 Page 18


Bit 2063- object-oriented programming

void Student::SetData() {

cout<<"\n\nEnter student name: ";

cin>>stdata.Name

cout<<"\nEnter student course: ";

cin>>stdata.Course;

cout<<"\nEnter student result: ";

cin>>stdata.Result;

cout<<"\n";

};

void Student::GetData() {

cout<<"\n\nName:"<<stdata.Name;

cout<<"\nCourse:"<<stdata.Course;

cout<<"\nResult:"<<stdata.Result;

cout<<"\n";

};

#include <iostream.h>

#include <string.h>

#include "ci090094student.cpp"

class StudentList {

public:

struct ListNode{

Student astudent;

ListNode *next;

};

ListNode *head,*tail;

public:

StudentList();

Bit 2063 Page 19


Bit 2063- object-oriented programming

~StudentList();

int IsEmpty();

void Add(Student newstudent);

void Remove();

void Remove(char Name[20]);

void DisplayList();

};

StudentList::StudentList() {

head=tail=NULL; };

StudentList::~StudentList(){

cout<<"\n Destructing the objects..\n";

while(IsEmpty()!=0)

Remove();

if(IsEmpty()==0)

cout<<"All students have been deleted from a list\n";

};

int StudentList::IsEmpty() {

if(head == NULL)

return 0;

else

return 1;

};

void StudentList::Add(Student newstudent){

ListNode *newPtr = new ListNode;

if(newPtr==NULL)

cout<<"cannot allocate memory";

else

Bit 2063 Page 20


Bit 2063- object-oriented programming

{ newPtr->astudent = newstudent;

newPtr->next=NULL;

if (head == NULL)

{ head=newPtr;

tail=newPtr;

else

{tail->next = newPtr;

tail = newPtr;

};

void StudentList::Remove() {

if (IsEmpty()==0)

cout<< "List empty or remove";

else

{ ListNode *temp=head;

head=head->next;

temp->next=NULL;

delete temp;

};

Bit 2063 Page 21


Bit 2063- object-oriented programming

void StudentList::Remove(char Name [20])

{ ListNode *cur,*prev,*temp;

cur=prev=head;

if(strcmp(Name,cur->astudent.stdata.Name)==0)

{ temp=head;

head=head->next;

temp->next=NULL;

delete temp;

else

while (cur!=NULL)

if(strcmp(Name,cur->astudent.stdata.Name)==0)

{ prev->next=cur->next;

temp = cur;

temp->next=NULL;

delete temp;

cur = NULL;

else

Bit 2063 Page 22


Bit 2063- object-oriented programming

{ prev=cur;

cur=cur->next;

};

void StudentList::DisplayList() {

ListNode *cur = head;

if (IsEmpty()==0)

cout<<"Current List : \n";

else

cout<<"Current List: \n";

while( cur!=NULL) {

cur->astudent.GetData();

cur = cur-> next;

cout<<"\n";

};

Bit 2063 Page 23


Bit 2063- object-oriented programming

#include <iostream.h>

#include "liststdci090094.cpp"

int main(){

const int size=5;

int i;

char name[20];

Student newstudent;

StudentList alist;

cout<<"\nInserting "<<size<<" objects:-\n";

for(i=0;i<size;i++){

newstudent.SetData();

alist.Add(newstudent);

alist.DisplayList();

cout<<"Please enter the student name you want to delete: ";

cin>>name;

alist.Remove(name);

cout<<"after removing "<<name<<"\n";

alist.DisplayList();

return 0;

};

Bit 2063 Page 24


Bit 2063- object-oriented programming

Bit 2063 Page 25


Bit 2063- object-oriented programming

Bit 2063 Page 26

S-ar putea să vă placă și