1. Programming Paradigms OBJECT ORIENTED PROGRAMMING A programming methodology defines the methodology of designing and implementing programs using the key features and other building blocks (such as key words, functions, pre processor directives etc) of a programming language. Some of the important paradigms are: Procedural Programming Object based programming Object oriented programming 1.1 Procedural Programming Definition : Procedural programming is a programming paradigm that gives more emphasis to procedure than data. Advantage : Procedural Programming separates functions and data. So writing procedures separately was easy. Disadvantage : The Procedural programming is susceptible to design changes. The changes should be affecting more than one procedure in the program. The Procedural Programming leads to increased time and cost overheads during design changes. Example : C, Pascal etc 1.2 Object based Programming Definition : Object based programming is a programming paradigm that encloses data and associated meaningful functions in one single entity called a class. Advantage : i) Object based Programming localizes the implementation details. When any change is made the user interface is not affected. ii) It is considered as a sub set of Object oriented programming. iii) It overcomes the difficulties of Procedural Programming iv) It supports user defined types v) It implements information hiding and abstraction etc. Disadvantage : Object based programming cannot implement real world relationships that exist among objects. Example : Visual Basic 1.3 Object oriented programming Definition : Object oriented Programming is a programming paradigm that combines data and procedures in one single unit called object. Advantage : i) Object oriented Programming enables re use of code ii) It provides ease of comprehension. It makes the implementation of real world models easier.
iii) It provides ease of fabrication and maintenance. The concepts of data abstraction and data encapsulation provides a very clean design. iv) It can be easily redesigned and extended. The above facilities facilitate redesign and maintenance. Example : C++, Java 2. OOP Concepts: The Important OOP concepts are: Data Abstraction Data Hiding Data Encapsulation Object inheritance Polymorphism 2.1 Object: Object is an identifiable entity with some characteristics and behaviour. Examples: Car, Apple, Chair etc. A car is a collection of wheels, doors, seats, windows. These are its characteristics. A car can move, speed up, slow down, stop, park, etc. These are its behaviours. Thus we can put everything we know about a car in one object. 2.2 Implementing Objects: The characteristics or states are maintained with the help of variables or data items. The behaviour is implemented through functions or methods. 2.3 Encapsulation: Encapsulation is the wrapping up of characteristics and behaviour into one unit. The following things should be taken care during implementation of Encapsulation. i. Any thing that an object does not know or cannot do is excluded from the object. ii. Encapsulation is used to hide unimportant implementation details from other objects. iii) Packaging an object s variables within the protective custody of its methods is called encapsulation & this task is accomplished through Classes. 2.4 Classes: A class is a group of similar objects. Examples: Vehicle, Fruits, Furniture. A class can also be defined as a way to bind the data describing an entity and its associated functions together. Thus the class implements Encapsulation.
Encapsulation is having the benefits of modularity and information hiding. A class syntax for class definition is : class<class name> { private : //hidden data members/methods here protected : //(un important implementation details here public : //exposed implementation details here }; 2.5 Data Hiding: Data Hiding is a related concept of data abstraction. Unessential features or background details are hidden from the world. A class groups its members into three: Private Protected and Public The private & protected members remain hidden from outside world. Thus through private & protected members class enforces data hiding. 2.6 Data Abstraction: It is the act of representing the essential features without including the background details and explanations. The class gives the outside world only the essential data by using the concept of data hiding and the Public members contains the essential data for all the members. 2.7 Implementing Inheritance: Inheritance is implemented by specifying the name of the base class from which the class being defined (derived class) has to inherit from. class <class name> : <base class name> { derived class own features }
2.8 Abstract class: Abstract class is the one which defines an interface, but does not necessarily provide implementations for all its member functions. It is meant to be used as the base class from which other classes are derived. Example: Shape. It can suggest that shapes can have colour, size, sides etc. 2.9 Concrete class: A derived class that implements all the missing functionality is called a Concrete class. It is derived from an abstract class. Example: circle, rectangle etc. 2.10 Implementing Polymorphism: Polymorphism is the attribute that allows one interface to be used with different situation. Polymorphism is implemented through virtual functions, overloaded functions and overloaded operators. Overloading: The term overloading means a name having two or more distinct meanings. Thus an overloaded function refers to a function having more than one distinct meaning. Similarly when two or more distinct meanings are defined for an operator, it is said to be an overloaded operator. 2.11 Function overloading: A function name having several definitions that are differentiable by the number or types of their arguments is known as an overloaded function and this process is known as function overloading. float add(int a, int b); float add( float x, float y);
SOLVED PROBLEMS 1. What are the OOP concepts? 2 marks Ans: The Important OOP concepts are: Data Abstraction Data Hiding Data Encapsulation Object inheritance Polymorphism 2. Explain the concept of data abstraction and its implementation in C++. 2 marks Ans. It is the act of representing the essential features without including the background details and explanations. The class gives the outside world only the essential data by using the concept of data hiding and the Public members contains the essential data for all the members. Thus data abstraction is achieved with the help of class. 3. Explain the concept of data encapsulation and its implementation in C++. 2 marks Ans. Encapsulation is the wrapping up of characteristics and behaviour into one unit. A class is defined as a way to bind the data describing an entity and its associated functions together. Thus the class implements Encapsulation. 4. Explain the concept of data hiding and its implementation in C++. 2 marks Data Hiding is a related concept of data abstraction. Unessential features or background details are hidden from the world. A class groups its members into three: Private Protected and Public The private & protected members remain hidden from outside world. Thus through private & protected members class enforces data hiding. 5. Explain the benefits of data encapsulation. 1 mark Ans. Encapsulation is having the benefits of modularity and information hiding. 6. Explain the concept of object and its implementation in C++. 1 mark Ans. Object is an identifiable entity with some characteristics and behaviour. Examples: Car, Apple, Chair etc. The characteristics or states are maintained with the help of variables or data items. The behaviour is implemented through functions or methods. 7. Explain the concept of polymorphism and its implementation in C++. 1 mark Ans. Polymorphism is the attribute that allows one interface to be used with different situation.
Polymorphism is implemented through virtual functions, overloaded functions and overloaded operators. 8. Differentiate between abstract and concrete classes 2 marks Ans. Abstract class is the one which defines an interface, but does not necessarily provide implementations for all its member functions. It is meant to be used as the base class from which other classes are derived. Example: Shape. It can suggest that shapes can have colour, size, sides etc. A derived class that implements all the missing functionality is called a Concrete class. It is derived from an abstract class. Example: circle, rectangle etc. 9. State the advantages of Object Oriented Programming. 2 Marks Ans. i) Object oriented Programming enables re use of code ii) It provides ease of comprehension. It makes the implementation of real world models easier. iii) It provides ease of fabrication and maintenance. The concepts of data abstraction and data encapsulation provides a very clean design. iv) It can be easily redesigned and extended. The above facilities facilitate redesign and maintenance. 10Ḋifferentiate between Object Oriented and Object Based Programming. 2 Marks Ans. Though both OBP and OOP are object oriented, OBP does not support inheritance and there by polymorphism whereas these are supported by OOP. So OBP is considered as a subset of OOP. 11. Give any 2 examples for Object Oriented Programming. 1 Marks Ans. C++, Java 12. A function printchar is defined as 2 marks void printchar(char ch = *, int len = 40) { for(x = 0;x<len;x++) cout<<ch; cout<<endl; } How will you invoke the function for following output? i. to print * 40 times ii. to print = 40 times Ans. (i) printchar( ) (ii)printchar( = )