You are on page 1of 25

COMPUTER PROGRAMMING

LECTURE NOTES

CLASS - OBJECTS - OOP

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
};

‫ ہ‬Variables are objects void DayOfYear::output()


{
cout << month << day << endl;
}

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;
};

‫‘ ہ‬.’ used with variables to void DayOfYear::output()

identify a member {
cout << month << day << endl;
DayOfYear birthday; }

birthday.output( );
4
POLYMORPHISM
ABSTRACTION

OBJECTS

CLASS
OOP

INHERITANCE

ENCAPSULATION

MESSAGE PASSING

5
ENCAPSULATION

Combining a number of items, such as


variables and functions, into a single
package such as an object of a class

6
public and private

• private members of a class can only be class DayOfYear


{
referenced within the definitions of public:
void output( );
member functions void input();
• the keyword public identifies the members private:
int month;
of a class that can be accessed from outside int day;
};
the class

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);
}

BankAccount::BankAccount(int dollars, int cents, double


percent){
balance = dollars + 0.01*cents;
rate = percent/100.0;
}
9
Overloading Constructors

BankAccount(int dollars, int cents, double rate);


BankAccount(double balance, double rate);
BankAccount(double balance);
BankAccount(double rate);
BankAccount();

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{}

Money total, cost, tax;

total = cost + tax;

// instead of total = add(cost, tax);

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 +

friend Money operator + (const Money& cash1 ....

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);
....}

Money operator + (const Money& cash1, const Money& cash2){


Money temp;
temp.cents = cash1.cents +cash2.cents;
return temp;
}

18
Overloading << and >>
Given the declaration:
Money cash(100);

cash.output(cout);
can become
cout << cash;

19
what does << return?

cout << "Nhom 1 2, " << "hoc phan" << ten_hoc_phan;

is grouped as:

( (cout << "Nhom 1 2, ") << "hoc phan") << ten_hoc_phan;

---> (cout << "Nhom 1 2, ") returns cout

20
overloaded << declaration
class Money{
......
friend ostream& operator << (ostream& outs, const Money& cash);
}

ostream& operator << (ostream& outs, const Money& cash){


outs << cash.cents;
return outs,
}
21
22
23
24
POLYMORPHISM
ABSTRACTION

OBJECTS

CLASS
OOP

INHERITANCE

ENCAPSULATION

MESSAGE PASSING

25

You might also like