introduction to c++ programming - object oriented programming concepts - structured vs oop. classes...
TRANSCRIPT
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.
Unit - V
• 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
Object Oriented Programming
• OOP is a programming style that is focused on
objects
• Important Features of OOP
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
Abstraction
• 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.
Encapsulation
• 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
Inheritance
• 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
Polymorphism
• 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
Structured Vs OOP
Structured OOP
Focuses on Process Focuses on Object
Follows Top Down Approach
Follows Bottom Up Approach
• 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
MAIN PROGRAM
FUNCTION 3FUNCTION 2
GLOBAL DATA
FUNCTION 5FUNCTION 4
FUNCTION 1
• Using function• Function & program is divided into modules• Every module has its own data and function
which can be called by other modules.
Structured Programming
OBJECT ORIENTED PROGRAMMING
Object 1 Object 2
Data
Function
Data
Function
Object 3
Data
Function
•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
I/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.
• 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 ( >> ) and insertion ( << ) operators.
• The next slide shows an example of the use of cin and cout.
04/18/23 14
Classes 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 operationsObject:
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 functionsaccess-specifier: data and functionsaccess-specifier: data and functions// ...access-specifier: data and functions} object-list;
The object-list is optional. If present, it declares objects of the class.
04/18/23 15
access-specifier is one of these three C++ keywords:
publicprivate (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 (->) for pointer to the object• E.g.,
c.x , cpt -> x
Implementation 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 declaration
Definitions placed outside the class declaration must
tell compiler where the corresponding
declaration/prototype is :
Use the scope operator :: which has the form
ClassName::ItemName
1. 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-member 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:
ClassName::ItemNamePublic 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.
Access 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
Assignment - 5
1. Distinguish between a `struct' and a `class' in C++?
2. How does a class accomplish data hiding? Explain with an example.
3. Explain the benefits of object oriented programming over procedure oriented programming
4. What are the access privileges in C++? What is the default access level?
5. What do you mean by Encapsulation and explain in detail.
6. What is the difference between “C structure” and “C++ structure”.
7. Explain about the C++ classes in detail and design a class for playing cards?
8. Discuss in detail about utility functions and access functions.