2.1 Classes and Objects
2.1 Classes and Objects
2.1 Classes and Objects
1.0 ●
Introduction To OOP
2.0 ●
Classes And Objects
4.0 ●
Exception handling
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
2
1 ●
Define Class
2 ●
Define Method
3 ●
Access Specifier
4 ●
Declare Objects
© 2009 | PN NORHASLIZA BT MUHAMAD NOR
4
CLASSES
* Semicolon (;) must be placed after closing braces each time you define a class.
class Rectangle
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}
};
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;
}
};
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;
}
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,
`
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 ( );
};
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
};
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( );
};
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
Cont…
Example