Professional Documents
Culture Documents
Oops
Oops
Object- It is identifiable entity with some characteristics(data) and behaviour(functions) . It is an instance of a class. Class: It is a template representing a group of objects that share a common properties and relationships. OOP Here emphasis on objects(data) It follows bottom up approach in program design Its data hiding features prevents accidental change in data Its features like data encapsulation, polymorphism, inheritance are present. Structure It is declared with the keyword struct By default all members are public It consist of data. Procedural Programming Here emphasis is on doing things(functions) It follows top down approach in program design Data can freely move among functions Such features are not available in this type of programming. Class It is declared with the keyword class By default all members are private It consists of data and functions together.
CONCEPTS OF OOP 1. Data Hiding: A class groups its members into three sections: private, protected and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class enforces data hiding. (The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. The act of representing only essential features without including background details is known as abstraction.) Eg: class ABC In the above class public members(ie e,f and disp( )) only will be available to outside the {private: int a,b; class.. The other private protected: int c,d; members (a,b), protected members (c,d) will not be available to outside the class. This public: int e,f; concept is called data hiding. void disp( ) { } 2. Encapsulation : The wrapping up of data and functions into a single unit (class) is called as encapsulation. A class binds together data and its associated functions under one unit thereby enforcing encapsulation. It is a way of implementing abstraction. Eg: class Rectangle { private: float len,bre,area; public: void readData( ) { cout<<\nEnter the length and breadth..; cin>>len>>bre; } void calculate( ) { area=len*bre; } void display( ) { cout<<\nThe area of the rectangle = <<area;}}; Eg: Here in the above class the data members ie len,bre,area and the member functions ie readData( ), calculate( ), display( ) are bind together in a class named as Rectangle. Ie The member functions can access any data member in the class. Benefits with encapsulation: (i) Modularity. (ii) Information hiding.
3. Data Abstraction: It refers to the act of representing essential features without including the background details or explanations. 4. Modularity: It is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
5. Polymorphism : It means many (poly) forms (morphism)It is the ability for a message or data to be processed in more than one form. It is a property by which the same message can be sent to objects of several different classes, and each object can respond in a different way depending on its class. e.g Function Overloading: A function name having several definitions that are differentiable by the Signature( i.e. number or types of their arguments), is known as an overloaded function and this process is known as function overloading. Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster. void area(float r){} void area(float l,float b) {} 6. Inheritance: The capability of one class to inherit properties from another class is called as inheritance. The class inheritance, lets you generate a model that is closer to the real world. The class whose properties are inherited is called base class and the class that inherits the properties is known as derived class. The most important advantage of inheritance is code reusability. There are 5 types of inheritance: (i) Single Inheritance): When a sub class inherits only from one base class, it is known as single inheritance. (ii) Multiple Inheritance: When a sub class inherits from multiple base classes, it is known as multiple inheritance. (iii) Hierarchical Inheritance: When many sub classes inherit from a single base class, it is known as hierarchical inheritance. (iv) Multilevel Inheritance: When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance. (v) Hybrid Inheritance: Hybrid inheritance combines two or more forms of inheritance. Uses of Inheritance: 1) Capability to express the inheritance relationship which ensures the closeness with the real world models. 2) Code Reusability. 3) Transitive nature of inheritance. public and private access specifiers in context of OOP: A member declared as private remains hidden from outside world and it can only be accessed by the member functions of the class. A member declared as public is made available to the outside world. That is, it can be accessed by any function (member or non member function), any expression in the program but only by using an object of the same class type. These access labels enforce data hiding and abstraction, OOP concepts. Eg: class student Here, since rno and name are declared in private, they { private: int rno; char name[21]; can be accessed only inside the class. Since age,input( ) public: int age; void input( ); anddisplay() are declared in public, they can be accessed void display( ); } from outside class also. private and protected visibility modes in context of OOP: At the time of inheritance, protected variables can be inherited to the derived class but private cannot be inherited. Except regarding inheritance, both access specifiers ie private and protected will work same. Eg: class student Here, since rno and name are declared in private, they can be { private: int rno; char name[21]; accessed only inside the class. Since age,input( ) and display() are protected: int age; void input( ); declared in protected, they also can be accessed only inside the class void display( ); } but they can be inherited, where as private members (rno and name) cannot be inherited.
INLINE FUNCTIONS: The inline functions are a C++ enhancement designed to speed up programs. The coding of normal functions and inline functions is similar except that inline functions definitions start with the keyword inline. With inline code, the compiler replaces the function call statement with the function code itself (this process is called expansion) and then compiles the entire code. Inline functions run a little faster than the normal functions as function calling overheads are saved, however there is a memory penalty. If 10 times an inline function is called, there will be 10 copies of the function inserted into the code. A function can be declared inline by placing the keyword inline before it. An inline function definition should be placed above all the functions that call it. The functions should be inlined only when they are small. Since for large functions, they will become memory penalty. The inlining does not work for following situations: a. For functions that return values and are having a loop or a switch or a goto. b. For functions not returning values, if a return statement exists. c. If functions contain static variables. d. If the function is recursive(a function that calls itself).
Difference between #define and const: #define can only define simple constants, while const can define
almost any type of constant, including things like classes, structures etc. So 2 ways to define the same constant are: 1. #define PI 3.14 2. const float PI=3.14; Const offers several advantage over #define The #define constants used in the program are not visible to compiler because they get replaced with their value by the pre-processor prior to compilation. But the constant defined with const are visible to the compiler and the compiler checks the syntax of const statements as per the language rules that follows normal C++ scope rules. #define are also used to define macros e.g. It is used to define words to enhance readability and understandability. Before compilation the pre- processor replaces every occurrence of macro as per the definition #define TRUE 1 #define sqr(x) (x*x) void main() {cout<<sqr(9)<<sqr(5.67)<<sqr(a);} //where ever sqr(9) will be seen will be replaced by 9*9, sqr(5.67) will be replaced by 5.67*5.67 and sqr(a) will be replaced by a*a i.e 65*65
typedef command defines a new name or an alias name for an existing type. For example,
typedef double amount; Now we can define any amount using the datatype Amount as Amount loan, balance, installment, interest; OR Typedef char text[80]; and now we can create character array of size 80 bytes by text str1; text str1=computer
CALL BY VALUE AND REFERENCE: In by value the called function creates its own copies of the
original values sent to it. Any changes, that are made, occur on the called functions copy of values and are not reflected back to the calling function. By reference the called function accesses and works with the original values and are reflected back to the calling code.