Professional Documents
Culture Documents
LECTURE NOTES
1
CONTENTS
1. CLASSES - OBJECTS
2. ENCAPSULATION
3. POLYMORPHISM
2
CLASSES - OBJECTS
ہDefinition of a class:
ہMember variables class DayOfYear
{
ہMember functions public:
void output( );
int month;
int day;
ہDATA TYPE
};
3
:: and . Operator
ہvoid DayOfYear::output( )
indicates that function output class DayOfYear
{
is a member of the public:
void output( );
DayOfYear class int month;
int day;
};
identify a member {
cout << month << day << endl;
DayOfYear birthday; }
birthday.output( );
4
POLYMORPHISM
ABSTRACTION
OBJECTS
CLASS
OOP
INHERITANCE
ENCAPSULATION
MESSAGE PASSING
5
ENCAPSULATION
6
public and private
7
Calling private member
• When a member function calls a private
member function, an object name is not
used.
class BankAccount{
....
private:
double balance;
double rate;
double fraction(double percent);
}
void BankAccount::update(){
balance = balance + fraction(rate)*balance;
}
8
Constructors
ہInitialize member variables when declared
class BankAccount{
public:
BankAccount(int dollars, int cents, double rate);
...
BankAccount account_number_one(10, 50, 20);
}
default constructor
--> calling default constructor:
BankAccount account_number_one;
10
Friends functions
11
Example: equal function
ہTwo DayOfYear objects : date1, date2
bool equal(DayOfYear date1, DayOfYear date2){
return ( date1.get_month( ) == date2.get_month( ) && date1.get_day( ) == date2.get_day( ) );
}
---------------------
Function equal could be made more efficient: equal uses member function calls to obtain the
private data values. Direct access of the member variables would be more efficient (faster)
---------------------
...
public:
friend bool equal(DayOfYear date1, DayOfYear date2);
bool equal(DayOfYear date1, DayOfYear date2){
return ( date1.month == date2.month && date1.day == date2.day ); 12
Friends
• Friend functions can be written as non-friend
functions using the normal accessor and mutator
functions that should be part of the class
• The code of a friend function is simpler and it is
more efficient
• In general, use a member function if the task
performed by the function involves only one object
• Use a nonmember function if the task
performed by the function involves more than
one object
13
POLYMORPHISM
ABSTRACTION
OBJECTS
CLASS
OOP
INHERITANCE
ENCAPSULATION
MESSAGE PASSING
14
Overloading Operators
class Money{}
15
Overloading Operators
To overload the + operator for the Money class:
- Use the name + in place of the name add
- Use keyword operator in front of the +
16
Overloading Operators RULES
• At least one argument of an overloaded operator
must be of a class type
• An overloaded operator can be a friend of a class
• New operators cannot be created
• The number of arguments for an operator cannot
be changed
• The precedence of an operator cannot be changed
• ., ::, *, and ? cannot be overloaded
17
Examples:
class Money{ ....
friend Money operator + (const Money& cash1, const Money& cash2);
....}
18
Overloading << and >>
Given the declaration:
Money cash(100);
cash.output(cout);
can become
cout << cash;
19
what does << return?
is grouped as:
20
overloaded << declaration
class Money{
......
friend ostream& operator << (ostream& outs, const Money& cash);
}
OBJECTS
CLASS
OOP
INHERITANCE
ENCAPSULATION
MESSAGE PASSING
25