You are on page 1of 44

Object Oriented System using C

++
UNIT-1

BY: VIVEK DIMRI

OBJECT ORIENTED PARADIGM
Paradigm Common Features
Demonstrates feasibility of representation of problem solutions within computers.
Machine Instructions ( Op codes ) Data representations Input Output

Machine
Problems can be solved by running a program in a computer.

Paradigm Mnemonic (or Symbolic) ³Programs should be written in a manner that makes them easily understood by human beings.

Common Features
Improves readability of computer programs by using commands that looked words and numbers that looked like numbers.
Variable declarations Assignment statements Global scope of identifiers Absolute, conditional jumps # Statement labels # GOTO statements Comments

Paradigm

Common Features

Simplify understandability Modular of programs by partitioning of large problems into Large programs should smaller problems that are be composed of smaller more easily solved.

blocks of code.

Subroutines, functions, procedures # Return statement Formal/actual arguments Separation of code/data Local Scope of identifiers Function libraries

Sequence # Blocks # Block scope of identifiers Selection # If/Else statements # Case/Switch statements Iteration # Do/For/While/Until # Recursion Exception handling # Throw/Raise statements # Catch statements Controlled jumps # Break/Continue/Cycle . Common Features Improve readability of programs by limiting the number of arbitrary ways in which statements can be put together.Paradigm Structured Proper programs should be coded in a way that minimizes the number of detours from the main flow of the program.

Paradigm Object-oriented Programs should be written as a collection of otherwise independent objects that interact with one another. Common Features Simplifies programs by placing logically related parts close together and by having similar parts of the program behave in a similar manner. Encapsulation # Classes and objects # Fields/Attributes # Methods Inheritance # Constructors and Factories Polymorphism # Interfaces/abstractions/pure .

stimuli.Paradigm Common Features Makes programs more Event-driven understandable by implementing common Programs should be frameworks and written as responders conventions followed by to external and internal all programmers. Events Event generators Event handlers .

Common Features Facilitates communication between programmers and users by using standard and expected user-oriented components. Visual components/containers Properties Events .Paradigm Visual Programs should be written by focusing on the interfaces first and adding details later.

computer software was written in a Procedural style. and to a certain degree C++ and C# are as well .Structured versus Object oriented development Procedural Programming  Before Object Oriented Programming (OOP) was popular. C is largely procedural.  This meant that the application/program was divided into a large number of procedures.

 The two mathematicians demonstrated that any computer program can be written with just three structures:  Decisions  Sequences  loops  A popular design methodology was "Structured Programming" but that has been rendered obsolete by OOP .Structured Programming  Structured programming was first suggested by Corrado Bohm and Guiseppe Jacopini.

.  Certain languages such as Ada.Structured Programming  Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Pascal. and dBASE are designed with features that encourage or enforce a logical program structure.  Often planned using a Top-down Design or Hierarchy chart.

Procedure Program view Main Program Data Procedure1 Procedure2 Procedure3 .

Structured Programming Focus is on procedures All data is shared: no protection More difficult to modify Hard to manage complexity .

 Since the memory partitions are independent.Object-Oriented Programming  Object-oriented programming is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. the objects can be used in a variety of different programs without modifications Object = Data + Methods (Functions) .  This means that an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access the data.

Object1 Data1+Procedures1 Object2 Data2 + Procedures12 Data Data Object3 Data3 + Procedures3 Object4 Data4 + Procedures4 .

Concept /Elements of OOPs  Object  Class  Data abstraction and encapsulation  Inheritance  Polymorphism  Dynamic Binding  Message Passing .

shape.OOP Basic Concepts/ Elements Object:  An object is a concept. abstraction. For example.Two apples with the same color. and texture are still individual apples  An object contains both data and methods that manipulate that data  ³Object´ is an instance of a class. Each object has a class which defines its data and behavior  Objects are the basic run-time entities in an objectoriented system. or thing with identity that has meaning for an application  All objects have identity and are distinguishable. .

the objects interact by sending messages to one another.  When a program is executed.OOP Basic Concepts/ Elements  Objects take up space in memory and have an associated address like a record in Pascal. or a structure in C.  Object Own copy of data Active in running program Occupies memory Has the set of operations given in the class Person Age:34 .

OOP Basic Concepts/ Elements Class: 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 Classes reflect concepts. ³Class´ refers to a blueprint. objects reflect instances that embody those concepts. It defines the variables and methods the objects support .

or cookie cutter . or variables The class describes those fields An object may have methods The class describes those methods A class is like a template.OOP Basic Concepts/ Elements Class: Every object belongs to (is an instance of) a class An object may have fields.

int salary. char desg[20]. public: int calsalary() }. (syntax of class) .OOP Basic Concepts/ Elements Class Employee Name. (Implementation of class) class class_name { data types member functions }. designation salary Cal salary Class employee { char name[20].

(Protection)) Advantages of Encapsulation: Protection  Consistency  Allows change .OOP Basic Concepts/ Elements Encapsulation: Wrapping up data methods in to a single unit (class) is encapsulation. Data is accessible only through its methods.

and methods operate on these attribute) .OOP Basic Concepts Data Abstraction: Refers to the act of representing essential features without including the background details or explanations. weight and cost. (Classes use the concept of abstraction and are defined as a list of abstract attributes such as size.

OOP Basic Concepts Inheritance: The process by which object of one class acquire the properties of objects another class. The sub-class inherits the base class¶s data members and member functions 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 .

OOP Basic Concepts Animal Mammal Reptile Rodent Primate Cats Mouse Squirel Rabbit .

For ex.  A single function name can be used to handle different no and different arguments . For numbers. The behavior depends upon the type of data used in the operation. An operation may exhibit different behavior in different instances.OOP Basic Concepts  Polymorphism:  Ability to take more than one form.  For ex. The operation of addition. the operation will generate a sum and if the operands are strings then the operation would produce a third string by concatenation.

OOP Basic Concepts Types of Polymorphism: Compile Time Method overloading Operator overloading Run Time Overriding virtual function .

. A function call associated with a polymorphism reference depends on the dynamic type of that reference.Dynamic Binding  Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.

infornation message . The concept of message passing makes it easier to talk about building systems that directly model or simulate their realworld counterparts. involves the following basic steps:  Creating classes that define objects and their behavior.salary  Object (name). The process of programming in an object-oriented language.Message Passing  An object-oriented program consists of a set of objects that communicate with each other. therefore.  Example  employee .  Creating objects from class definitions.  Objects communicate with one another by sending and receiving information much the same way as people pass message to one another. and  Establishing communication among objects.

 modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program.  extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones. . Changes inside a class do not affect any other part of a program.Advantage of OOP  simplicity: software objects model real world objects. so the complexity is reduced and the program structure is very clear. since the only public interface that the external world has to a class is through the use of methods.  modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system.

Advantage of OOP  maintainability: objects can be maintained separately. This is particularly useful for developing graphical user interfaces. as new objects can be created with small differences to existing ones. .  Makes it easy to maintain and modify existing code.  Provides a good framework for code libraries. where implementation details are hidden and the unit has a clearly defined interface.  Provides a clear modular structure for programs. This makes it good for defining abstract data types. making locating and fixing problems easier  re-usability: objects can be reused in different programs. where supplied software components can be easily adapted and modified by the programmer.

.COMPARISON BETWEEN OOP & STRUCTURED PROGRAMMING Look through the graph above. procedural approach can be a good solution. For simple or easy (even for semi hard) programs.

It is however. On the other hand for simple problems. approaching OOP concept is rather lengthy. useful for tough problems. . time consuming and not useful.COMPARISON BETWEEN OOP & STRUCTURED PROGRAMMING But for the hard ones it is not.

account .COMPARISON BETWEEN OOP & STRUCTURED PROGRAMMING  Procedural  Object Oriented Withdraw. deposit. transfer Customer. money.

while object-oriented programming focus on both of them   . Procedural programming separates the data of the program from the operations that manipulate the data. and unit in object-oriented programming is class Procedural programming concentrates on creating functions. while object-oriented programming starts from isolating the classes. debug. and then look for the methods inside them. applications then structured programming and maintain  The unit in procedural programming is function.COMPARISON BETWEEN OOP & STRUCTURED PROGRAMMING  OOP is easier to develop.

It is function oriented. easily added. Data & function are not combined together. 4. It follows top town approach. 3. New data &function can easily added. 3. 2. It follows bottom up approach. 2. New data &function can not 5. . Data & function are combined together.Structure 1. Object Oriented 1. 5. 4. Data is secure. It is data oriented. Data is not secure.

INTRODUCTION TO C++ .

other features      Extension of C Early 1980s: Bjarne Stroustrup (Bell Laboratories) C with class C++ Provides capabilities for object-oriented programming  Objects: reusable software components  Model items in real world  Object-oriented programs  Easy to understand.History of C and C++  History of C  Evolved from two other programming languages  CPL  BCPL and B  ³Typeless´ languages  Dennis Ritchie (Bell Laboratories)  C  Added data typing. correct and modify .

Compile 4. . Load 6. Compiler creates object code and stores it on disk. Linker links the object code with the libraries. . .Compilation Model Phases of C++ Programs: 1. . Preprocess 3. . . creates a.out and stores it on disk Disk Disk Disk Primary Memory 5. Preprocessor program processes the code. . Link Loader Editor Preprocessor Compiler Linker Disk Program is created in the editor and stored on disk. Edit 2. Primary Memory CPU . . Loader puts program in memory. CPU takes each instruction and executes it. . . . Execute Disk . possibly storing new data values as the program executes.

return 0.Program Hello // This program is to print hello #include <iostream> using namespace std. int main() { cout <<"Hello!"<< endl. } .

 Obtain a value from the keyboard  Standard input stream (cin).  Output values to the screen  Standard output stream (cout). or ³see-in´  Stream extraction operator (>>).Output & Input cout << "Enter an integer: ". or ³get from´ . or ³see-out´  Stream insertion operator (<<). or ³put to´ cin >> int1.

This is the namespace in which the entire Standard C++ library is declared. The using statement informs the compiler that you want to use the std namespace. Namespaces help in the organization of large programs. By using the std namespace you simplify access to the standard library. Namespaces are a recent addition to C++.using namespace std. This tells the compiler to use the std namespace. A namespace creates a declarative region in which various program elements can be placed. .

cout << (x + y)/2. cin >> x >> y. int main() { int x. return 0. cout <<"Enter two numbers \n".Program Average #include <iostream. } . cout <<"Their average is: ".0 << endl.h> using namespace std. int y.

Out put Enter two numbers 8 10 Their average is: 9 .