08 - Classes and Objects - Part II

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 7

Object Oriented

Programming
Lec # 8
By
Engr. Sajid Saleem
Classes and Objects
Revisited
Introduction
Access and Utility Functions (9.5)
When Constructors and Destructors are called
(9.8)
Default Member wise assignment (9.10)
Using Initialization List (pg 350)
Friend functions and friend classes (10.4)
Using the this pointer(10.5)
DMA with operations new and delete(10.6)
Static class members(10.7)
Introduction
There are few principle concepts that form the foundation of object-
oriented programming:
Object: This is the basic unit of object oriented programming. That
is both data and function that operate on data are bundled as a
unit called as object.
Class: When you define a class, you define a blueprint for an object.
This doesn't actually define any data, but it does define what the
class name means, that is, what an object of the class will consist
of and what operations can be performed on such an object.
Abstraction: Data abstraction refers to, providing only essential
information to the outside word and hiding their background details
ie. to represent the needed information in program without
presenting the details.
Encapsulation: Encapsulation is placing the data and the functions
that work on that data in the same place. While working with
procedural languages, it is not always clear which functions work
on which variables but object-oriented programming provides you
framework to place the data and the relevant functions together in
the same object.
Introduction
Inheritance: One of the most useful aspects of object-
oriented programming is code reusability. As the name
suggests Inheritance is the process of forming a new class
from an existing class that is from the existing class called
as base class, new class is formed called as derived class.
This is a very important concept of object oriented
programming since this feature helps to reduce the code
size.
Polymorphism: The ability to use an operator or function in
different ways in other words giving different meaning or
functions to the operators or functions is called
polymorphism. Poly refers many. That is a single function or
an operator functioning in many ways different upon the
usage is called polymorphism.
Overloading: The concept of overloading is also a branch of
polymorphism. When the exiting operator or function is
made to operate on new data type it is said to be
overloaded.
Using Initialization List
Previously, you initialized the members of an object in the
class constructor using explicit assignment. You could also
have used a different technique, using what is called an
initialization list.
// Constructor definition
CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0)
{
cout << endl << Constructor called.;
m_Length = lv; // Set values of
m_Width = bv; // data members
m_Height = hv;
}
// Constructor definition using an initialization list
CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0):
m_Length(lv), m_Width(bv), m_Height(hv)
{
cout << endl << Constructor called.;
}
Using Initialization List
The way this constructor definition is written assumes that
it appears within the body of the class definition. Now the
values of the data members are not set in assignment
statements in the body of the constructor.
As in a declaration, they are specified as initializing values
using functional notation and appear in the initializing list
as part of the function header. The member m_Length is
initialized by the value of lv, for example. This can be more
efficient than using assignments as you did in the previous
version. If you substitute this version of the constructor in
the previous example, you will see that it works just as well.
Note that the initializing list for the constructor is separated
from the parameter list by a colon and each of the
initializers is separated by a comma. This technique for
initializing parameters in a constructor is important,
because, as you will see later, its the only way of setting
values for certain types of data members of an object.
Topics Covered
C++ How to Program, Fifth Edition By
H.M.Deitel (Ebook references)
9.5,9.8,9.10
10.4,10.5,10.6,10.7

Beginning Visual C++ 2005 By Ivor


Horton (Ebook references)
Pg 193-198
Pg 350, 368-372, 495-496

You might also like