You are on page 1of 13

1

F3031 OBJECT ORIENTED PROGRAMMING

F3031 Object Oriented Programming

1.0 ●
Introduction To OOP

2.0 ●
Classes And Objects

3.0 Inheritance and Polymorphism


4.0 ●
Exception handling
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
2

2.0 CLASSES AND OBJECTS

2.1 Understand the concepts of


© 2009 | PN NORHASLIZA BT MUHAMAD NOR
Classes and Objects
3

F3031 OBJECT ORIENTED PROGRAMMING

OBJECTIVES TOPIC 2.1

1 ●
Define Class

2 ●
Define Method

3 ●
Access Specifier

4 ●
Declare Objects
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
4

F3031 OBJECT ORIENTED PROGRAMMING

CLASSES

General format for defining a class :


•  
class ClassName
{
// Data definition
// Method definition
};

* Keyword class is a must in defining a class.

* Semicolon (;) must be placed after closing braces each time you define a class.

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


5

Cont... F3031 OBJECT ORIENTED PROGRAMMING

Katakunci untuk mengisytihar kelas /


Keyword for defining a class
NamaKelas /
ClassName
Definasi ahli data /
Data member definition

class Rectangle

{ int length, width;

public :

Kawalan capaian /
void set_data (int x,int y)
Access control
{ length=x;

width=y;
Definasi metod /
}
Method definition

double calculate_area()

{ return length*width}

};

Contoh definisi kelas / Example of class definition

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


6

Cont... F3031 OBJECT ORIENTED PROGRAMMING

There are two ways of defining method :


1. Define it in the class

 
class Student
{
int ID,age;
char name[30];
double mark;
 
public:
void set_data (int a,int b, char * c, double d)
{
ID=a;
age=b;
strcpy(name,c);
mark=d;
}
};

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


7

Cont... F3031 OBJECT ORIENTED PROGRAMMING

There are two ways of defining method :


2. Define it outside the class by using scope resolution operator (::)

class Student
{
int ID,age;
char name[30];
double mark;
 
public:
void set_data (int a,int b,char * c,double d); declare
};
 
void Student::set_data(int a,int b,char * c,double d)
{
ID=a;
age=b; define
strcpy(name,c);
mark=d;
}

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


8

F3031 OBJECT ORIENTED PROGRAMMING

ACCESS SPECIFIER

Three type of access specifier(kawalan capaian) for data members and methods:

1. Public
2. Private
3. Protected

Class members are private members by default but the situations can be
changed by using another keyword. For example,
`

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


9

Cont... F3031 OBJECT ORIENTED PROGRAMMING

Public:
• Public members are accessible outside the class (for example in main() function).
• Members in the class must be declared in the public section to make it accessible
from outside the class.
• Definition of friend does not give any effect to this access control.

class AccessCtrl
{
int value1;
void func1 (long);
int value2;
int func2 (char*);

public :
char* value4;
long func4 ( );
};
 

Contoh kawalan capaian public / Example of public access control

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


10

Cont... F3031 OBJECT ORIENTED PROGRAMMING

Private:
• Private members can only be achieved by functional members (ahli-ahli fungsi)
and friends for a class, which has been declared. (Will be explained further later)
• Example of the use of this keyword in the real world:
The ATM pin number, password to open an e-mail. This example is more appropriate
if it is declared privately so that it is not accessed by any other unauthorized person.

class AccessCtrl
{
int value1; Private Data
int value2;
int func2 (char*);
void func1 (long); Private Method
};
 

Contoh kawalan capaian public / Example of public access control

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


11

Cont... F3031 OBJECT ORIENTED PROGRAMMING

Protected:
• Protected members are accessible in the class where it is declared and also any
other classes, which are inherited from that class.
• Definition of friend does not give any effect to this access control.
• Example of the use of this keyword in the real world: total savings for a student. Only
certain people knows the actual total savings by a student such as his/her family
members.
class A
{ protected:
int valueA;
};
class B : public A
{ Example of protected access control
public:
void FuncB( );
};
class C : public B
{
public:
void FuncC( );
};
 

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


12

F3031 OBJECT ORIENTED PROGRAMMING

OBJECTS
Declaring an objects :

className
className objectName;
objectName;

`
Example:
• Assuming that you have created an object from the class Student. Example of
declaration is shown as below:
Student p;
• When an object has been declared, it can be manipulated by any function, which is
declared inside the object class.
• Syntax is used to call a data or method:
ObjectName . MemberName

dot operator

© 2009 | PN NORHASLIZA BT MUHAMAD NOR


13

F3031 OBJECT ORIENTED PROGRAMMING

Cont…
Example

© 2009 | PN NORHASLIZA BT MUHAMAD NOR

You might also like