Upload
mohd-firhan-jasni
View
228
Download
0
Embed Size (px)
Citation preview
8/9/2019 2.1 Classes and Objects
1/13
Introduction To OOP1.0
Classes And Objects
2.0
Inheritance andPolymorphism3.0
Exception handling4.0
1
2009 | PN NORHASLIZA BT MUHAMAD NOR
8/9/2019 2.1 Classes and Objects
2/13
2
8/9/2019 2.1 Classes and Objects
3/13
Define Class1
Define Method2
Access Specifier3
Declare jects
3
2009 | PN NORHASLIZA BT MUHAMAD NOR
8/9/2019 2.1 Classes and Objects
4/13
General format for defining a class :
2009 | PN NORHASLIZA BT MUHAMAD NOR
4
class ClassName
{
// Data definition
// Method definition
};
*Keywordclass is a must in defining a class.
* Semicolon (;) must be placed after closing braces each time you define a class.
8/9/2019 2.1 Classes and Objects
5/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
5
class Rectangle
{ int length, width;
public :
void set_data (int x,int y)
{ length=x;
width=y;
}
double calculate_area()
{ return length*width}
};
Definasi metod /
Method definition
NamaKelas/
ClassName
Definasi ahli data /
Data member definition
Katakunci untuk mengisytihar kelas /
Keyword for defining a class
Kawalancapaian /
Access control
Contoh definisi kelas / Example of class definition
8/9/2019 2.1 Classes and Objects
6/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
6
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;
}
};
8/9/2019 2.1 Classes and Objects
7/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
7
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);
};
void Student::set_data(int a,int b,char * c,double d)
{
ID=a;age=b;
strcpy(name,c);
mark=d;
}
declare
define
8/9/2019 2.1 Classes and Objects
8/13
Three type of access specifier(kawalan capaian) for data members and methods:
1. Public
2. Private
3. Protected
`
2009 | PN NORHASLIZA BT MUHAMAD NOR
8
Class members are private members by default but the situations can be
changed by using another keyword. For example,
8/9/2019 2.1 Classes and Objects
9/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
9
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
8/9/2019 2.1 Classes and Objects
10/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
10
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;
int value2;
int func2 (char*);
void func1 (long);};
Contoh kawalan capaian public /Example of public access control
Private Data
Private Method
8/9/2019 2.1 Classes and Objects
11/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
11
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
{
public:
void FuncB( );};
class C : public B
{
public:
void FuncC( );
};
Example of protected access control
8/9/2019 2.1 Classes and Objects
12/13
Declaring an objects :
`
Example:
Assuming that you have created an object from the class Student.Example ofdeclaration 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
1
className objectName;
8/9/2019 2.1 Classes and Objects
13/13
2009 | PN NORHASLIZA BT MUHAMAD NOR
13
Example