More Related Content
Similar to OOP - Introduction (20)
More from Mohammad Shaker (8)
OOP - Introduction
- 3. Topics you Should Understood WELL
• Pointers
• Functions
• Call by value
• Call by reference
• Character array
Copyright © 2012 IM
Group. All rights
reserved
- 4. Fast Training
• Make a program that takes a character array as
input
• Append this character array to itself
• Output it
• Example:
▫ Input: Mohammad
▫ Output: MohammadMohammad
• Note:
▫ You must save it in a character array NOT string
Copyright © 2012 IM
Group. All rights
reserved
- 5. What Is a Class
• A class is a data type whose variables are objects
• Some pre-defined classes you have used are
▫ int
▫ char
▫ string
• You can define your own classes as well
Copyright © 2012 IM
Group. All rights
reserved
- 6. Class Definitions
• A class definition includes
▫ Variables
▫ Functions
• We will start by defining structures as a first step
toward defining classes
Copyright © 2012 IM
Group. All rights
reserved
- 8. Structures
• A structure can be viewed as an object:
▫ Contains no functions
▫ Contains multiple values of possibly different
types
• Example: A Student has the following values:
▫ A Name
▫ A GPA
▫ An ID
Copyright © 2012 IM
Group. All rights
reserved
- 10. Using the Structure
• Structure definition is generally placed outside
any function definition:
▫ This makes the structure type available to all code
that follows the structure definition
• To declare two variables of type Student:
• Student Salah, Moaaz;
• Salah and Moaaz contain member variables
name, GPA, and ID
Copyright © 2012 IM
Group. All rights
reserved
- 11. Access the Variables of a Structure
• To access variables in a structure we should use
the dot Operator “.”
Copyright © 2012 IM
Group. All rights
reserved
- 12. Duplication
• You can duplicate the same name in different
structures.
Copyright © 2012 IM
Group. All rights
reserved
- 13. Structures as Return Types
• Structures can be the type of a value returned by
a function
• Example:
Copyright © 2012 IM
Group. All rights
reserved
- 14. Using Function initialize
• initialize builds a complete structure value in
temp, which is returned by the function
• We can use initialize to give a variable of type
Student a value in this way:
Copyright © 2012 IM
Group. All rights
reserved
- 15. Assignment and Structures
• The assignment operator can be used to assign
values to structure types
Copyright © 2012 IM
Group. All rights
reserved
- 17. Using Student
• A variable of type Student is declared by
▫ Student student1;
• To display the birth year of student1, first access
the birthday member of student1
▫ cout << student1.birthday…
• But we want the year, so we now specify the year
member of the birthday member
▫ cout << student1.birthday.year;
Copyright © 2012 IM
Group. All rights
reserved
- 21. Classes
• A class description is somewhat like a structure
definition plus the member variables
• To create a new type named DayOfYear as a
class definition
▫ This example’s values are dates such as July 4
using an integer for the number of the month
Member variable month is an int (Jan = 1, Feb = 2,
etc.)
Member variable day is an int
▫ Decide on the member functions needed
▫ We use just one member function named output
Copyright © 2012 IM
Group. All rights
reserved
- 24. Defining a Member Function
• There are 2 ways to declare a member function:
▫ Member functions are declared inside the class
declaration
Copyright © 2012 IM
Group. All rights
reserved
- 25. Defining a Member Function (cont.)
• Member functions are declared outside the class
declaration
• To declare a function outside the class
declaration you should know the scope operator
“::”
Copyright © 2012 IM
Group. All rights
reserved
- 26. The Scope Operator “::”
• Tells the class a member function is a member of
• void DayOfYear::output() indicates that function
output is a member of the DayOfYear class
Copyright © 2012 IM
Group. All rights
reserved
- 27. Training
• Define the function of class “Calculator” with the
scope operator
Copyright © 2012 IM
Group. All rights
reserved
- 28. Encapsulation
• Encapsulation is
▫ Combining a number of items, such as variables
and functions, into a single package such as an
object of a class
Copyright © 2012 IM
Group. All rights
reserved
- 29. Public Or Private
• C++ helps us restrict the program from directly
referencing member variables
• Private members of a class can only be
referenced within the definitions of member
functions
• If the program tries to access a private member,
the compiler gives an error message
• Private members can be variables or functions
Copyright © 2012 IM
Group. All rights
reserved
- 31. The Assignment Operator “=“
• Objects and structures can be assigned values
with the assignment operator (=)
• Example:
Copyright © 2012 IM
Group. All rights
reserved
- 32. Calling Public & Private Members
• Public:
▫ Fell free to call the public functions or variables in
any part of your code
▫ You must use an object to call it
• Private:
▫ Call it only in a public function
▫ Call it without objects
Copyright © 2012 IM
Group. All rights
reserved
- 33. Access private variables
• What you will do if you want to read or edit a
private variable ?!!
• Use Setter & Getter function
• Let’s see the new class
Copyright © 2012 IM
Group. All rights
reserved
- 36. Constructors
• A constructor can be used to initialize member
variables when an object is declared
• A constructor is a member function that is usually
public
• A constructor is automatically called when an object
of the class is declared
• A constructor’s name must be the name of the class
• A constructor cannot return a value
• No return type, not even void, is used in declaring or
defining a constructor
Copyright © 2012 IM
Group. All rights
reserved