OOPs with C++

advertisement
Course Description:
 Title
:
Object Oriented Systems
 Paper Code
:
CA - 205
 Course
:
MCA
 Faculty
:
Ms. Charu Sharma
Vikrant Gupta
Course Objectives & Prerequisites:
The course aims is to introduce the students to Object Oriented
Programming Concepts with special emphasis on Object Oriented
Programming in C++.
The course begins with a gentle introduction to OOAD and OOP
concepts. Students will then be presented with detailed discussion
of Object Oriented Programming features in C++. At the end the
use of few applications of OOAD and OOP concepts in areas like
Software Engineering and Operating Systems.
In order to take the course the student should be familiar with
Digital Computer System and must have completed a basic course
in Computer Programming, preferably in C.
Object Oriented Programming
Programming evolution
 Machine code
● programs in binary code, executed directly by the processor
Assembly languages
● still low level programming, replaced machine code functions with mnemonics and memory
addresses with symbolic labels
Procedural programming
● decompose programs into procedures/functions
Modular Programming
● decompose programs into modules
Object Oriented Programming
● decompose program into a set of objects
● objects interact with each other to form a system
What is OOP?
 OOP is a design philosophy. It stands for Object Oriented
Programming. Object-Oriented Programming (OOP) uses a
different set of programming languages than old procedural
programming languages (C, Pascal, etc.). Everything in OOP is
grouped as self sustainable "objects“.
What is OOP?(Cont’d)
 In order to clearly understand the object orientation, let’s take
your “hand” as an example. The “hand” is a class.Your body has two
objects of type hand, named left hand and right hand. Their main
functions are controlled/ managed by a set of electrical signals
sent through your shoulders (through an interface). So the
shoulder is an interface which your body uses to interact with your
hands. The hand is a well architected class. The hand is being reused to create the left hand and the right hand by slightly changing
the properties of it.
Problem Description
 “ customers are allowed to have different types of bank
accounts, deposit money, withdraw money and transfer
money between accounts”
Procedural Approach
bool MakeDeposit(int accountNum,float amount);
float Withdraw(int accountNum,float amount);
struct Account {
char *name;
int accountNum;
float balance;
char accountType;
};
Procedural Approach cont’d
 Focus is on procedures
 All data is shared: no protection
 More difficult to modify
 Hard to manage complexity
Procedural vs. Object-Oriented
 Procedural
 Object Oriented
Withdraw, deposit, transfer
Customer, money, account
Mapping the world to software
 Objects in the problem domain are mapped to objects in software
Object Oriented
 Data and operations are grouped together
Account
Deposit
Withdrawl
Transfer
Interface:
Set of available operations
Object Oriented Paradigm
Provide flexible and powerful abstraction
Allow programmers to think in terms of the structure of the
rather than in terms of the structure of the computer.
● Decompose the problem into a set of objects
● Objects interact with each other to solve the problem
● create new type of objects to model elements from the problem space
An object is an entity that:
● has a local state
● able perform some operation (behavior)
problem
Object Oriented Paradigm(Cont’d)
 It may be viewed as a combination of:
 ● data (attributes)
 ● procedural elements (methods)
 Object Oriented Programming is a method of implementation
where:
objects are fundamental building blocks
 ● each object is an instance of some type (class)
 ● classes are related to each others by inheritance
Fundamental concepts and properties
 Concepts:
● object
● class
● method (message)
 Properties:
● encapsulation
● inheritance
● polymorphism
Objects and Classes
 Classes reflect concepts, objects reflect instances that embody
those concepts.
object
Jodie
class
Daria
Jane
girl
Brittany
Objects and Classes (cont’d)
 A class captures the common properties of the objects
instantiated from it
 A class characterizes the common behavior of all the
objects that are its instances
Objects and Classes (cont’d)
Class BankAccount
Operations
Balance
InterestYTD
Owner
Account_number
MakeDesposit
Balance 500
InterestYTD
Owner
Account_number
Transfer
WithDraw
GetBalance
Balance 10,000
InterestYTD
Owner
Account_number
Objects as instances of Classes
 The world conceptually consists of objects
 Many objects can be said to be of the same type or class
 My bank account, your bank account, Bill Gates’ bank
account …
 We call the object type a class
Instantiation
 An Object is instantiated from a Class
BankAccount myAccount;
myAccount = new BankAccount;
Objects and Classes
 Class
 Object
 Visible in source code
 Own copy of data
 The code is not duplicated
 Active in running program
 Occupies memory
 Has the set of operations
given in the class
Classification
Animal
Mammal
Rodent
Mouse
Reptile
Primate
Squirel
Cats
Rabbit
Classification
Account
Checking Account
Value First
Select Access
Savings Account
First Interest
Encapsulation
Abstraction in OOP is closely related to a concept called
encapsulation.
Data and the ways to get at that data are wrapped in a
single package, a class. The only way to access such data is
through that package. This idea translates to information
hiding.
Encapsulation (Cont’d)
Encapsulation is the method of combining the data and
functions inside a class. This hides the data from being accessed
from outside a class directly, only through the functions inside the
class is able to access the information.
This is also known as "Data Abstraction", as it gives a clear
separation between properties of data type and the associated
implementation details. There are two types, they are "function
abstraction" and "data abstraction". Functions that can be used
without knowing how its implemented is function abstraction.
Data abstraction is using data without knowing how the data is
stored.
Data Encapsulation
class Account {
public:
float withdraw();
void deposit(float amount);
private:
float balance;
);
Advantages of Encapsulation

Protection
 Consistency
 Allows change
Inheritance
 A class which is a subtype of a more general class is said to be
inherited from it.
 The sub-class inherits the base class’ data members and member
functions
Inheritance (Cont’d)
 A sub-class has all data members of its base-class plus its own
 A sub-class has all member functions of its base class (with
changes) plus its own
 Inheritance is meant to implement sub-typing (don’t abuse it)
Inheritance Examples
Base Class
Student
Shape
Loan
Derived Classes
CommuterStudent
ResidentStudent
Circle
Triangle
Rectangle
CarLoan
HomeImprovementLoan
MortgageLoan
University community members
CommunityMember
Employee
Faculty
Administrator
31
Teacher
Staff
Student
Abstraction


Management of complexity
Hierarchical classification:
is-a relationship: inheritance
has-a relationship: containment
Abstraction Cont’d
 One of the chief advantages of object-oriented programming is the
idea that programmers can essentially focus on the “big picture”
and ignore specific details regarding the inner-workings of an
object. This concept is called abstraction.
Polymorphism
 One interface
 Multiple implementations
 Inheritance
 Method overloading
Polymorphism (Cont’d)
 Polymorphism describes how programmers write
methods to do some general purpose function.
 Different objects might perform polymorphic methods
differently.
What is a good class ?
 A class abstracts objects
 A class should be non-trivial in the context of the program (has
data structures and operations different from other classes)
Motivation
Variables  objects
Types
 classes
Procedural programming:
 Low-level, closer to hardware
 More intuitive, less abstract
 More ‘action’ oriented
 Focus on ‘action’, ‘procedure’, ‘method’
 Procedure-oriented
Object-oriented programming:
 High-level
 More abstract
 Focus on ‘what to do’ not on ‘how to do’
In the implementation of OOP,
we still need sound ‘procedure programming’ skills!
Procedural programming:
A sequence of ‘procedures’
int main()
{
int x,y,z;
int a,b,c;
a=f1(x); b=f2(y); c=f3(z);
…
}
int f1()
{
}
int f2()
{
}
int f3()
{
}
Object oriented programming:
A sequence of ‘objects’!
int main()
{
A a; B b; C c;
}
a.f1(); b.f2(); c.f3();
…
Class A
{
Int x;
Int f1();
}
Class B
{
Int y;
Int f2()
}
Class C
{
Int z;
Int f3();
}
Object-oriented modeling and design
Introduction
 It is a new way of thinking about problems using models based on
real world concepts.
 The basic construct is object which combines both data structure
and behavior in a single entity.
 Rambaugh presents an object oriented software development
methodology, the Object Modeling Technique (OMT) which
extends from analysis through design to implementation.
What is object-oriented?
 Software is organized as a collection of discrete objects that
incorporate both data structure and behavior.
 In general it includes- identity, classification, polymorphism and
inheritance.
 Object Oriented Programming Language
= Object Based Programming Language(e.g. '83 Ada a Modula2,C++and Java,etc.)
+ Inheritance + Dynamic Binding
41
Identity
 Identity means that data is organized into discrete, distinguishable
entities called objects.
 Objects can be concrete or conceptual.
 In real world an object simply exist but within a programming
language each object has a unique handle by which it can be
uniquely referenced.
 The handle can be implemented by address, array index or unique
value of an attribute.
42
Classification
 It means that objects with same data structure (attribute) and behavior
(operations) are grouped into a class.
 A class is an abstraction that describes important properties and
ignores the rest.
43
Polymorphism
 It means that the same operation (i.e. action or
transformation that the object performs) may behave
differently on different classes.
 Specific implementation of an operation by a certain
class is called a method.
44
Inheritance
 It is the sharing of attributes and operations among
classes based on a hierarchical relationship.
 Subclasses can be formed from broadly defined class.
 Each subclass incorporates or inherits all the properties
of its super class and adds its own unique properties.
45
Object-Oriented Development?
 The theme is the identification and organization of application
concepts rather than final representation in a prog. Language.
 OOD approach encourages software developers to work and think
in terms of the application domain through most of the software
engineering life cycle.
 It is a conceptual process independent of a programming language
until the final stage.
46
Object-Oriented Methodology
 Stages.
 Analysis
 System design
 Object design
 Implementation
47
3 Models
 Object Model
 Dynamic model
 Functional model
48
Object Model
 Describes basic structure of objects and their
relationship.
 Contains object diagram.
 Object diagram is a graph whose nodes are object classes
(Classes) and whose arcs are relationships among classes.
49
Dynamic model
 Describes the aspects of a system that change over time.
 It specifies and implement control aspects of a system.
 Contains state diagram.
 State diagram is a graph whose nodes are states and
whose arcs are data-flows.
50
Functional Model
 Describes data value transformation within a system.
 Contains data flow diagram.
 Data Flow Diagram is a graph whose nodes are processes
and whose arcs are data flows.
51
Object-Oriented Concepts
 Abstraction
 Encapsulation
 Combining data and behavior
 Sharing
 Object structure, not Procedure Structure
 Synergy
52
On completion of the class, a student
should be able:
•To prepare object-oriented design for small/medium scale problems
• To demonstrate the differences between traditional imperative design
and object-oriented design
• To explain class structures as fundamental, modular building blocks
• To understand the role of inheritance, polymorphism, dynamic binding
and generic structures in building reusable code
• To write small/medium scale c++ programs with simple graphical user
interface
• To use classes written by other programmers when constructing their
systems
Summary
What is Object Oriented Programming?
Object-oriented programming is a method of
implementation in which programs are organized as
cooperative collections of objects, each of which
represents an instance of some class, and whose classes
are all members of one or more hierarchy of classes
united via inheritance relationships
Expected Result
100%
Download