Title: Introduction to c programming
1Unit - V
Introduction to c programming - object
oriented programming concepts - Structured
Vs OOP. Classes and objects - class
definition - Objects - class scope and
accessing members - access functions and
utility functions.
2- History of C
- Extension of C
- Early 1980s Bjarne Stroustrup (Bell
Laboratories) - Originally named C with Classes.
- Provides capabilities for object-oriented
programming - Objects reusable software components
- Model items in real world
- Object-oriented programs
- Easy to understand, correct and modify
- Hybrid language
- C-like style
- Object-oriented style
- Both
3Object Oriented Programming
- OOP is a programming style that is focused on
objects - Important Features of OOP
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
4Abstraction
- Showing only the essential features and hiding
the unnecessary features - The access modifiers in C or any OOP language,
provides abstraction - Functions also provide abstraction
- If a variable is declared as private, then other
classes cannot access it - The function name that is used in function call
hides the implementation details from user. It
shows only the outline of functionality that the
function provides.
5Encapsulation
- The process of bringing together the data and
method of an object is called as encapsulation - The data and method are given in the class
definition - Classes provides us with this feature -
Encapsulation
6Inheritance
- Feature that enables the characteristics or
properties of a parent to reach its child - C supports inheritance
- A class can inherit one or more classes
- Inherited class is called as parent class or
super class or base class - Class that inherits a parent class is called as
child class or sub class or derived class
7Polymorphism
- Poly Many
- Morph Form
- Polymorphism is the characteristic that enables
an entity to co exist in more than one form - C supports function overloading and operator
overloading to implement polymorphism
8Structured Vs OOP
Structured OOP
Focuses on Process Focuses on Object
Follows Top Down Approach Follows Bottom Up Approach
9- Top Down approach
- A Single module will be split into several
smaller modules - General to Specific
- Bottom Up approach
- Lot of small modules will be grouped to form a
single large module - Specific to General
- If the requirements are clear at the first
instance we can go for Top down approach - In circumstances where the requirements may keep
on adding, we go for Bottom up approach
10Structured Programming
- Using function
- Function program is divided into modules
- Every module has its own data and function which
can be called by other modules.
11OBJECT ORIENTED PROGRAMMING
- Objects have both data and methods
- Objects of the same class have the same data
elements and methods - Objects send and receive messages to invoke
actions
12I/O in C
- Since C is a superset of C, all of the C I/O
- functions such as printf and scanf which are
found - in the stdio.h header file, are still valid in
C. - C provides an alternative with the new stream
- input/output features by including iostream.h.
- Several new I/O objects available when you
include the iostream header file. Two important
ones are - cin // Used for keyboard input
- cout // Used for screen output
- Both cin and cout can be combined with other
member functions for a wide variety of special
I/O capabilities in program applications.
13- Since cin and cout are C objects, they are
somewhat "intelligent" - They do not require the usual format strings and
conversion specifications. - They do automatically know what data types are
involved. - They do not need the address operator, .
- They do require the use of the stream extraction
( gtgt ) and insertion ( ltlt ) operators. - The next slide shows an example of the use of cin
and cout.
14Classes in C
- Class is defined as
- A collection of related variables and functions
into a - single structure
- A user-defined complex data type with its own
- operations
Object Instances of the class are called
objects. The variables and functions in the
definition of the class are called members.
Syntax to create a Class class class-name
private data and functions access-specifier
data and functions access-specifier
data and functions // ... access-specifier
data and functions object-list The
object-list is optional. If present, it declares
objects of the class.
12/16/2013
14
15- access-specifier is one of these three C
keywords - public
- private (default)
- Protected
- private data can be accessed only by other
members of the class. - - public data can be accessed by other parts of
the program - - protected needed only when inheritance is
involved. - - access-specifier can be changed in any order,
default is private.
Class Scope
- Class data members and member functions belong to
that class's scope. - Within a class's scope, class members are
references by name. - Outside a class's scope, class members are
referenced through one of the handles on an
object.
- Use dot (.) notation for object and references.
- Use arrow (-gt) for pointer to the object
- E.g.,
- c.x , cpt -gt x
12/16/2013
15
16Implementation of a Class
Class declaration contains ? Declarations of
data members ? Prototypes (declarations) of
function members Definitions of function members
are not usually placed in class
declarationDefinitions placed outside the class
declaration must tell compiler where the
corresponding declaration/prototype is Use the
scope operator which has the
form ClassNameItemName
171. Member functions "Inside" an object, so
don't pass object to them as a parameter. (They
receive the object to be operated on implicitly,
rather than explicitly via a parameter.)Non-membe
r functions "Outside" an object, so to operate
on an object, they must receive it via a
parameter. 2. Public items must be qualified when
referred to outside the class declaration Class
NameItemNamePublic constants are usually
declared static so they are global class
properties that can be accessed by all objects
of that class type rather than each object having
its own copy. 3. Simple member functions
Usually specified as inline functions. This
suggests to compiler to replace a function call
with actual code of the function with parameters
replaced by arguments saves overhead of
function call.
18Access Functions and Utility Functions
- Utility functions
- private functions that support the operation of
public functions - Not intended to be used directly by clients
- Access functions
- public functions that read/display data or check
conditions - For a data structure, it could call the isEmpty
function
19Assignment - 5
- Distinguish between a struct' and a class' in
C? - How does a class accomplish data hiding? Explain
with an example. - Explain the benefits of object oriented
programming over procedure oriented programming - What are the access privileges in C? What is
the default access level? - What do you mean by Encapsulation and explain in
detail. - What is the difference between C structure and
C structure. - Explain about the C classes in detail and
design a class for playing cards? - Discuss in detail about utility functions and
access functions.