0% found this document useful (0 votes)
35 views21 pages

OOP-02-Introduction To Object Oriented Programming-English

Uploaded by

Thắm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views21 pages

OOP-02-Introduction To Object Oriented Programming-English

Uploaded by

Thắm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to

Object Oriented
Programming
Nguyễn Minh Huy

Object Oriented Programming - Nguyễn Minh Huy 1


Contents
 Object and class.
 Object usage.
 Scope.

Object Oriented Programming - Nguyễn Minh Huy 2


Contents
 Object and class.
 Object usage.
 Scope.

Object Oriented Programming - Nguyễn Minh Huy 3


Object and class
 Procedural vs. Object Oriented:
 Problem: nấu món thịt kho trứng + rau muống xào
xào..
Action Procedural Object Oriented
Lặt Ướp ( Thịt ) Trứng. Luộc( )
Luộc Luộc ( Trứng ) Trứng. Bóc vỏ( )
Ướp Lặt ( Rau ) Rau. Lặt( )
Kho Bóc vỏ ( Trứng ) Rau. Xào( )
Xào Kho ( Thịt, Trứng ) Thịt. Ướp( )
Bóc vỏ Xào ( Rau ) Thịt. Kho( Trứng )

Materials Procedural: Object Oriented:


Thịt - Action first. - Data first.
Trứng - Function + Data. - Data triggers function.
(Verb) + (Object) (Object) does (Verb)
Rau
 Change your thinking!!

Object Oriented Programming - Nguyễn Minh Huy 4


Object and class
 What is object?
 Program is a complex machine.
 Compose of units:
 Basic units: variables, functions.
 Procedural: function + variables.
 Not easy to create abstract program!
 Object Oriented: variable triggers functions.
 Need a new kind of unit.

Special unit: Object!!

Object Oriented Programming - Nguyễn Minh Huy 5


Object and class
 Object characteristics:
 A special variable.
 Contain data and trigger functions:
 Attribute: data of object.
 Method: functions of object.

Function Struct Object

Object Oriented Programming - Nguyễn Minh Huy 6


Object and class
 Class concept:
 Variable ~ Type. Person:
 Struct variable ~ Struct type.  Name.
 Age.
 Object ~ Class:  Hair Color.
 Class is object type.  Eat( ).
 A description of:  Work( ).
 Attributes.
 Methods.

Person1: Person2:
 Name: Peter.  Name: Thomas.

 Age: 25.  Age: 50.

 Hair Color: Brown.  Hair Color: White.

 Eat().  Eat( ).

 Work().  Work( ).

Object Oriented Programming - Nguyễn Minh Huy 7


Contents
 Object and class.
 Object usage.
 Scope.

Object Oriented Programming - Nguyễn Minh Huy 8


Object usage
 How to use object in C++:
 Same as struct
struct..
 Usage:
 Declare class (file .h):
class <Class Name>
{
<Attribtes>;
Attribtes>;
<Methods>;
};
 Implement class (file .cpp
.cpp):
):
 Implement methods same as functions.
 Create object from class (main( ) function):
 Declare variables from class.

Object Oriented Programming - Nguyễn Minh Huy 9


Object usage
 Example: object oriented vs. procedural.
// Declare class, file Fraction.h // Declare struct,
struct, file Fraction.h
class Fraction struct Fraction
{ {
private: int m_num
m_num;;
int m_num
m_num;; int m_denom
m_denom;;
int m_denom
m_denom;; };
public:
Fraction add(Fraction p); Fraction add(Fraction p1, Fraction p2);
};

// Implement class, file Fraction.cpp // Implement add, file Fraction.cpp


Fraction Fraction
Fraction::add(Fraction
::add(Fraction p) Fraction add(Fraction p1, Fraction p2)
{ {
// … // …
} }

Object Oriented Programming - Nguyễn Minh Huy 10


Object usage
 Example: object oriented vs. procedural.
// Use object, file main.cpp // Use struct,
struct, file main.cpp
int main() int main()
{ {
Fraction p1; Fraction p1;
Fraction p2; Fraction p2;

p1.add( p2 ); add( p1, p2 );


} }

Object Oriented Programming - Nguyễn Minh Huy 11


Contents
 Object and class.
 Object usage.
 Scope.

Object Oriented Programming - Nguyễn Minh Huy 12


Scope
 Scope concept:
 Working range:
 Variable. Declared block
 Function. Whole program
 members. Declared block of struct variable
Struct members.
 Object members. Can be control

 Scope control:
Keyword Scope
private Inside class only.
public Inside and outside class.
protected Inside class and children class.

Object Oriented Programming - Nguyễn Minh Huy 13


Scope
 Example: private vs. public.
class A void main
main()
()
{ {
private: A obj
obj;;
int x;
public: int x = obj.x
obj.x;; // Wrong
int y; obj.x = 1; // Wrong

public: int y = obj.y


obj.y;; // Right
int getX
getX(( ); obj.y = 2; // Right
private:
void calculate( ); int t = obj.getX
obj.getX(( ));; // Right
}; obj.calculate(( ));; // Wrong
obj.calculate
}

Object Oriented Programming - Nguyễn Minh Huy 14


Scope
 Dr. Guru advises:
 Rule of Black Box:
 Attributes: use private to hide inside.
 Methods: use public to provide functions.

class Fraction
{
private:
int m_num
m_num;;
Attributes
int m_denom
m_denom;;
public:
Fraction add( Fraction p ); Methods
Fraction reduce( );
};

Object Oriented Programming - Nguyễn Minh Huy 15


Scope
 Example: implement methods.
class Fraction Fraction Fraction
Fraction::add (Fraction p)
::add(Fraction
{ {
private: // Implement outside class.
int m_num
m_num;; }
int m_denom
m_denom;;
public:
Fraction add (Fraction p)
add(Fraction
{
// Implement inside class.
}
};

Object Oriented Programming - Nguyễn Minh Huy 16


Summary
 Object and class:
 Object is a special variable.
 Object contains both data and functions.

 Class is an object type.

 Object usage:
 Class declaration.
 Class implementation.

 Scope:
 Working range.
 Keywords: public, private, protected.

Object Oriented Programming - Nguyễn Minh Huy 17


Practice
 Practice 2.1:
Construct class Fraction with the following methods:
 input
input:: enter fraction from keyboard.
 output
output:: print fraction to screen.
 getNum
getNum//setNum:
setNum: get/update numerator of fraction.
 getDenom
getDenom//setDenom
setDenom:: get/update denominator of fraction.
 reduce
reduce:: return the reduction of fraction.
 inverse
inverse:: return the inversion of fraction.
 add
add:: return the sum of two fractions.
 compare
compare:: return the comparison result of two fractions.
(0: first = second, -1: first < second , +1: first > second)

Object Oriented Programming - Nguyễn Minh Huy 18


Practice
 Practice 2.2:
Construct class Monomial with the following methods:
 input
input:: enter monomial from keyboard.
 output
output:: print monomial to screen.
 getCoef
getCoef//setCoef:
setCoef: get/update coefficient of monomial.
 getExpo
getExpo//setExpo
setExpo:: get/update exponent of monomial.
 evaluate
evaluate:: return result of evaluating monomial with float number.
 derive
derive:: return the derivative of monomial.
 mul
mul:: return the product of multiplying two monomials.

Object Oriented Programming - Nguyễn Minh Huy 19


Practice
 Practice 2.3:
Student information: name, literature and math points.
Construct class Student with the following methods:
 input
input:: enter student information from keyboard.
 output
output:: print student information to screen.
 getName
getName//setName
setName:: get/update name of student.
 getLit
getLit//setLit:
setLit: get/update literature point of student.
 getMath
getMath//setMath:
setMath: get/update math point of student.
 calculateGPA
calculateGPA:: return GPA of student.
(GPA = (literature + math) / 2)
 grade
grade:: return student grade
 A (GPA >= 9.0), B (GPA >= 7.0).
 C (GPA >= 5.0), D (GPA < 5).

Object Oriented Programming - Nguyễn Minh Huy 20


Practice
 Practice 2.4:
Construct class Array of integers with the following methods:
 input
input:: enter array size and elements from keyboard.
 output
output:: print array elements to screen.
 getSize
getSize//setSize:
setSize: get/update size of array.
 getElement
getElement//setElement
setElement:: get/update element at specified index.
 find
find:: look for an element and return found index (-(-1 if not found).
 sort
sort:: sort array, the sort criteria can be customized.

Object Oriented Programming - Nguyễn Minh Huy 21

You might also like