1 C++ Programming Module-I Introduction To Object Oriented Programming There are many different programming languages, and many ways to classify them. Example, "high-level" programming languages are languages whose syntax is relatively close to natural language, whereas the syntax of "low-level" languages includes many technical references to the nuts and bolts (0's and 1's, etc.) of the computer. "Declarative" languages (as opposed to "imperative" or "procedural" languages) enable the programmer to minimize his or her account of how the computer is to solve a problem or produce a particular output. "Objectoriented languages" reflect a particular way of thinking about problems and tasks in terms of identifying and describing the behavior of the relevant "objects". Smalltalk is an example of a pure object-oriented language. C++ includes facilities for object-oriented programming, as well as for more conventional procedural programming. Object Oriented Programming-Philosophy In The Design and Evolution of C++ (1994), Bjarne Stroustrup describes some rules that he uses for the design of C++: C++ is designed to be a statically typed, general-purpose language that is as efficient and portable as C . C++ is designed to directly and comprehensively support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming) . C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly. C++ is designed to be as compatible with C as possible, therefore providing a smooth transition from C . C++ avoids features that are platform specific or not general purpose. C++ does not incur overhead for features that are not used (the "zero-overhead principle") . C++ is designed to function without a sophisticated programming environment. Stroustrup also mentions that C++ was always intended to make programming more fun and that many of the double meanings in the language are intentional.
2 Object Oriented Approach The key ideas of the object oriented approach are: Object Message Passing OOP Class Conce pts Data Abstraction Dynamic Binding Polymorphism Conce pts Data Encapsulation Inheritance Objects: Definition: Objects are primary runtime entities in an OOP. Programming problem is analyzed in terms of objects and the nature of communication between them. Occupy space in memory and have an associated address like structure in C. Every object has its own properties. It is the instance of a class. The state of object varies according to procedure used is called the action of the object. Each object contains data and code to manipulate the data. A program is executed; the objects interacted by sending message to one another. It is generated at run time. Two ways to represent an Object. Object: Student DATA Name Marks FUNCTIONS Total Average Display…. Total Average STUDENT Display E.g.: OOP approach, the traffic-flow problem will be viewed in terms of the objects involved i.e. car, trucks, scooters, auto, taxi etc are objects.
3 Pen, pencil, scale, mango, apple, monkey, human, program etc are some real-world objects. In case of a pen and scale being objects, their communication yields a line on the paper. 1.1. Classes: Definition: A Class is group of objects having identical properties, common behavior and shared relationship. Class is a template for defining objects. Objects are variable of type class. Once a class is defined, create number of objects belonging to that class. These are used to implement the concept of Abstract Data Type(ADT). The entire group of data and code of an object can be built as a user defined data types using Class. A class is a collection of objects of similar type. Every object has its own value for each of its member variables. It shares the property name or operations with other instances of the class. Thus ,class defines the characteristics and actions of different objects. A group of objects that share common properties and relationships. In C++, a class is a new data type that contains member variables and member functions that operate on the variables called Object of this class. Class is a type just like int. A class is a definition of an object. A class resembles a struct with just one difference: all struct members are public by default. All classes’ members are private. E.g.: 1. 2. Class: Car Properties: Company, model, color and capacity. Action: Speed (), Break (). Class: Computer Properties: brand, price, monitor resolution, hard disk and RAM size. Action: processing (), display (). The syntax of creating an object: class_name object_name1, object_name2…,object_namen; E.g.: A fruit to be a class, then mango, orange, apple etc will be objects in this class. fruit mango, orange, apple; 1.2. Data Abstraction: Definition: Abstraction directs to the procedure of representing essential features without including the background details.
4 Classes use the concept of abstraction and are defined as a list of attributes such as size, weight and cost, and functions to operate these attributes. They encapsulate all the essential properties of objects that are to be created. Since the classes use the concept of data abstraction, they are known as Abstract Data Type (ADT). E.g.: in switch broad press certain switches according to our requirements, what is happening inside, how it is happening etc.we needn’t know. This is abstraction, only the essential things to operate on switch board without knowing the background details of switchboard. 1.3. Data Encapsulation: Definition: the packing of data and functions into a single unit (class) is known as Encapsulation. The data is not accessible by outside functions. Only those functions that are able to access the data are defined with in the class. It hides the functional details of a class from objects that send messages to it. It accomplishes data hiding. By data hiding an object can be used by the user without knowing how it works internally. The goal of the class is to encapsulate complication and also has mechanism for hiding the data. Each member in the class may be private or public. Therefore any non-member function cannot access the data of the class. Encapsulation is a way to implement data abstraction. E.g.: consider fopen () function, it is used to open the file .several internal variables are created and initialized .In user program is concerned, these variables are hidden and not accessible. 1.4. Polymorphism: Definition: Polymorphism allows the same function to act differently in different class. It has ability to take more than one form. An important role in allowing objects having different internal structure to share the same external interface. It accomplishes an important part in allowing objects of different classes to share the same external interface. An operation may show different behavior in different objects/instances. The behavior depends upon the types of data used in the operation. E.g.: two number, operation will generate a sum, if operands are string, then the operation will generate string concatenation. The process of making an operator to show different behavior in different object is called operator overloading. Using a single function name can be used to handle different number and different type of arguments to perform different types of operations is called function overloading. E.g.: