Document 9653864

Download Report

Transcript Document 9653864

Course Name: High Level Programming Language
Year
: 2010
Classes I
Lecture 6
Learning Outcomes
At the end of this lecture, students are
capable of:
• Concept Encapsulation
• Struct versus Class
• Class Access Control
• Constructor & Destructor
• Object Class
• Access with scope resolution operator
3
(::)
Concept Of Encapsulation
•
•
•
Encapsulation  grouping data and functions into one class.
Has access specifiers with a default type private (while, default access struct is public).
– reduces coupling, and almost paradoxically, increases the ability to create large programs.
– encourage coherence, which means that any given class does one thing. By increasing
coherence, a program becomes easier to understand, more simply organized, and this better
organization is reflected in a further reduction in coupling.
Information hiding
– Class objects communicate across well-defined interfaces
– Implementation details hidden within classes themselves
4
Struct VS Class
Default Access
member: private
Default Access
member: public
•
•
•
•
•
•
•
struct Product
{ int Prod_id;
char Prod_Name;
float Price;
int Stock_qty;
};
Product Brg1;
class Product
{ public: int Prod_id;
char Prod_Name;
float Price;
int Stock_qty;
};
Class Object
Product Brg1;
5
Struct Object
3 Access Level
• Public:
– Part of class that can be accessed by public whether it is accessed from inside or from outside
the class
• Protected:
– Part of class that only can be accessed by internal class environment and by its derivatives’
class (we will discuss about derivative class in inheritance)
• Private:
– Part of the class that can be accessed only by its internal environment objects within that class.
6
Constructor & Destructor
• class Time
• { private:
•
•
• public:
•
•
•
•
• };
int hour;
Constructor
int minute;
int second;
Time();
void setTime( int, int, int );
void printUniversal();
void printStandard();
~Time();
Destructor
7
Constructor & Destructor
• Constructor  the first member function that is executed once an
object is created
– Constructor’s name has to be the same with its class name
– Used to initialize the object
– Minimal, there is 1 constructor in a class (can be overloaded), but if it is not writen than the
compiler will generate a default constructor for that class
– Can be given arguments
– Does not have a return value (default: void)
8
Constructor & Destructor
• Destructor  member function that is called when an object is destroyed or
death
–
–
–
–
–
Destructor’s name is the same with the class name, except add tilde (~) in front of that name
Used to de-initialize or clean up or de-allocation of memory.
Only one 1 destructor (can not be overloaded)
Does not have argument
Does not have a return value
9
Object Class
• Deklarasi Object Class
• Time timeObject; //object
• Time timeArray[ 10 ]; // object array
• Time *timePtr;
//object pointer
• Time &timeRef = timeObject; // object reference
10
Access with scope resolution operator (::)
ClassName :: classMemberName
void Time :: setTime( int h, int m, int s )
{ hour = ( h >= 0 && h < 24 ) ? h : 0;
minute = ( m >= 0 && m < 60 ) ? m : 0;
second = ( s >= 0 && s < 60 ) ? s : 0;
}
void Time :: printUniversal()
{ cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":"
<< setw( 2 ) << second;
}
void Time :: printStandard()
{ cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour
% 12 )
<< ":" << setfill( '0' ) << setw( 2 ) << minute
<< ":" << setw( 2 ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
class Time
{ private:
int hour;
int minute;
int second;
public:
Time()
{ hour = minute = second = 0;
}
void setTime( int, int, int );
void printUniversal();
void printStandard();
~Time(){ }
};
11
Conclusions
• Encapsulation is just another word of “information hiding”
• Struct contains only data member, while class adds
member-function.
• Private, protected, public are member controls class.
• Constructor is an initializer, while destructor is a
destroyer.
• :: is a scope resolution operator.
12
Topic For Next Week
• A Closer Look at Classes
• Assignment:
– Read page 90-92 of “Cpp Essentials.pdf”.
– Define a class named Sequence, for storing sorted strings. Define a
constructor, a destructor, and the following member functions for Sequence:
• Insert, which inserts a new string into its sort position
• Delete, which deletes an existing string
• Find, which searches the sequence for the given string and returns true if
it finds it, and false, otherwise.
• Print, which prints the sequence strings.
13