Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS

Similar presentations


Presentation on theme: "UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS"— Presentation transcript:

1 UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS

2 Introduction: Object Oriented Paradigm
1.The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach. 2.Treats data as a critical element and does not move data to flow free around the system.

3 1.It ties data more closely to the function that operate on it, and protects it from accidental modification from outside function. 2.OOP allows decomposition of a problem into a number of entities called objects and then builds data and function around these objects. 3.The data of an object can be accessed only by the function associated with that object. However, function of one object can access the function of other objects.

4 Key features of Object Oriented Programming
Emphasis is on data rather than procedure. Programs are divided into what are known as objects. Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are ties together in the data structure.

5 5.Data is hidden and cannot be accessed by external function.
6.Objects may communicate with each other through function. 7.New data and functions can be easily added whenever necessary. 8.Follows bottom up approach in program design.

6 Basic Concepts of Object Oriented Programming
Objects Classes Data abstraction and encapsulation Inheritance Polymorphism Dynamic binding Message passing

7 Objects Objects are the basic run time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists

8

9 Classes 1.Objects contain data, and code to manipulate that data.
2. The entire set of data and code of an object can be made a user-defined data type called class. 3. Objects are variables of the type class. 4. We can create any number of objects belonging to that class.

10 5. A class is thus a collection of objects similar types
5.A class is thus a collection of objects similar types. For examples, Mango, Apple and orange members of class fruit. Fruit Mango; Will create an object mango belonging to the class fruit

11 Data Abstraction and Encapsulation
The wrapping up of data and function into a single unit (called class) is known as encapsulation. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program.

12 4. This insulation of the data from direct access by the program is called data hiding or information hiding. 5.Abstraction refers to the act of representing essential features without including the background details or explanation. 6.Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, wait, and cost, and function operate on these attributes.

13 Inheritance Inheritance is the process by which objects of one class acquired the properties of objects of another classes. It supports the concept of hierarchical classification. Provides the idea of reusability

14

15 Polymorphism Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in different instances.

16

17 Dynamic Binding 1.Binding refers to the linking of a procedure call to the code to be executed in response to the call. 2.Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time.

18 Message Passing An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, involves the following basic steps: Creating classes that define object and their behavior.

19 4. Creating objects from class definitions 5
4.Creating objects from class definitions 5.Establishing communication among objects. 6.Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another

20

21 Constructors   Definition: 1.It is a special member function. 2.Its task is to initialize the objects. 3.Its name is the same as the class name. 4.It is invoked when an object is created.

22 Ex: class circle { public: circle( ){ } //constructor is defined };

23 Properties: Should be declared in the public section.
Cannot be inherited. Can have default arguments. No return type can be specified for constructors. Not even void type can be specified for return type in constructor. So constructor can't return values.

24 Types Default constructor Parameterized constructor Copy constructor
Dynamic constructor (dynamical initialization of objects) Default argument constructor

25 Default constructor A Constructor that accepts no parameters is called the Default constructor. Syntax: A constructor is declared and defined as follows class s { public: int a; s( ); // constructor is declared };

26 s::s( ) // constructor is defined
{ a=0; } void main() { s o; // constructor is invoked }

27 Parameterized Constructor:
The constructors that can take arguments are called Parameterized constructors. Example: class s { public: int a; s(int x); // constructor is declared };

28 s::s(int x) // constructor is defined
{ a=x; } void main() { int a1; a1=12; s o(a1); / s o(12); // constructor is invoked

29 Copy Constructor A copy constructor is used to declare and initialize an object from another object. s (s &o); 2.There are two ways to invoke a copy constructor as follows i. s o(o1) -define the object o and at the same time initialize it to the values of o1 ii. s o = o1 -define the object o and at the same time initialize it to the values of o1

30 Example: class s { public: int a; s ( ) cin>>a; } s(s &o); // constructor is declared void display() cout<<a; } };

31 s::s(s &o) // constructor is defined { a=o
s::s(s &o) // constructor is defined { a=o.a; } void main() s o; s o1(o); //copy constructor is called s o2=o; //copy constructor is called o1.display(); o2.display();

32 Static Member A static member is shared by all objects of the class.
All static data is initialized to zero when the first object is created, if no other initialization is present. It can be initialized outside the class using the scope resolution operator :: to identify which class it belongs to.

33 Example: #include <iostream> using namespace std; class Box { public: static int objectCount; // Constructor definition Box(double l=2.0, double b=2.0, double h=2.0)

34 // Initialize static member of class Box
int Box::objectCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects. cout << "Total objects: " << Box::objectCount << endl; return 0; } When the above code is compiled and executed, it produces the following result: Constructor called. Total objects: 2

35 The Role of this pointer
The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.

36 3.A static member function does not have a this pointer.
4.The type of the this pointer for a member function of a class type X, is X* const. 5. If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X, is const X* const. 6.A const this pointer can by used only with const member functions

37 #include <iostream>
using namespace std; struct X { private: int a; public: void Set_a(int a) {

38 // The 'this' pointer is used to retrieve 'xobj
// The 'this' pointer is used to retrieve 'xobj.a' // hidden by the automatic variable 'a' this->a = a; } void Print_a() { cout << "a = " << a << endl; } }; int main() { X xobj; int a = 5; xobj.Set_a(a); xobj.Print_a();

39 In the member function Set_a(), the statement this->a = a uses the this pointer to retrieve xobj.a hidden by the automatic variable a. Unless a class member name is hidden, using the class member name is equivalent to using the class member name with the this pointer and the class member access operator (->).

40 Storage class A storage class defines the visibility and life-time of variables within a Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a Program auto register static extern mutable

41 i.The auto Storage Class
1.The auto storage class is the default storage class for all local variables. Example:  { int mount; auto int month; }

42 The register Storage Class
1.The register storage class is used to define local variables that should be stored in a register instead of RAM. 2. This means that the variable has a maximum size equal to the register size and cannot have the unary '&' operator applied to it . Example: { register int miles; }

43 3.The register should only be used for variables that require quick access such as counters.
4.It should also be noted that defining 'register' does not mean that the variable will be stored in a register. 5.It means that it might be stored in a register depending on hardware and implementation restrictions.

44 iii.The static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

45 The static modifier may also be applied to global variables.
When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

46 Example: #include <iostream> // Function declaration void func(void); static int count = 10; /* Global variable */ main() { while(count--) func(); } return 0;

47 } // Function definition void func( void ) { static int i = 5; // local static variable i++; std::cout << "i is " << i ; the program files.

48 iv.The extern Storage Class
1.The extern storage class is used to give a reference of a global variable that is visible to all. 2.When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

49 3.The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

50 v.The mutable Storage Class
1.The mutable specifier applies only to class objects.It allows a member of an object to override constant member functions. 2.That is, a mutable member can be modified by a constant member function.


Download ppt "UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS"

Similar presentations


Ads by Google