Basic Concepts of Oops & Classes and Objects
Total Page:16
File Type:pdf, Size:1020Kb
BASIC CONCEPTS OF OOPS & CLASSES AND OBJECTS Object-Oriented Programming groups related data and functions together in a class, generally making data private and only some functions public. Restricting access decreases coupling and increases cohesion. It has proven to be very effective in reducing the complexity increase with large programs. For small programs may be difficult to see the advantage of OOP over, eg, structured programming because there is little complexity regardless of how it's written. It helps in programming approach in order to built robust user friendly and efficient software and provides the efficient way to maintain real world software. OOP is an Object Oriented Programming language which is the extension of Procedure Oriented Programming language. OOPs reduce the code of the program because of the extensive feature of Polymorphism. OOPs have many properties such as DataHiding, Inheritance Data Abstraction, Data Encapsulation and many more. The main aim is to creating an Object to the Entire program and that to we can control entire program using the Object. The main features of OOPS is Polymorphism, Multiple Inheritance, Abstraction and Encapsulation. Objects: Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. An Object is a collection of data members and associated member functions also known as methods. Classes: • Classes are data types based on which objects are created. • Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and are referred to as Methods. • Classes are the blueprints upon which objects are created. E.g when we design a map of the house, the architect first designs it. Once it is designed, the raw material is used to build the house. In this example, Design of the house is CLASS (blueprint) and the house built on the basis of design is an object. No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created. Inheritance: • Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class. The new class that is formed is called derived class. • Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming. It is the process by which one class inherits the properties of another Class. Data Abstraction: • Data Abstraction increases the power of programming language by creating user defined data types. • Data Abstraction also represents the needed information in the program without presenting the details. 11 The concept of abstraction relates to the idea of hiding data that are not needed for presentation. The main idea behind data abstraction is to give a clear separation between properties of data type and the associated implementation details. An Abstract Data Type is defined as a data type that is defined in terms of the operations that it supports and not in terms of its structure or implementation. Data Encapsulation: • Data Encapsulation combines data and functions into a single unit called class. • Data Encapsulation enables the important concept of data hiding possible. Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class. Data hiding is the implementation details of a class that are hidden from the user. class MyClass {public: int sample(); int example(char *se) int endfunc(); ......... //Other member functions private: int x; float sq; ......... //Other data members }; In the above example, the data members integer x, float sq and other data members and member functions sample(),example(char* se),endfunc() and other member functions are bundled and put inside a single autonomous entity called class MyClass. This exemplifies the concept of Encapsulation. Encapsulation alone is a powerful feature that leads to information hiding, abstract data type and friend functions. Polymorphism: Poly means many and morphs mean form, so polymorphism means one name multiple form. There are two types of polymorphism: compile time and run time polymorphism.Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways. Overloading: • Overloading is one type of Polymorphism. • It allows an object to have different meanings, depending on its context. • When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded. Reusability: • This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. • The programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance. OOP Term Definition Method Same as function, but the typical OO notation is used for the call, i.e. f(x,y) is written x.f(y) where x is an object of class that contains this f 12 method. Send a message Call a function (method) Instantiate Allocate a class/struct object (ie, instance) with new Class A struct with both data and functions Object Memory allocated to a class/struct. Often allocated with new. Member A field or function is a member of a class if it's defined in that class Constructor A member function having same name as that of the class that initializes data members of an object at the time of creation of the object. Destructor Function-like code that is called when an object is deleted to free any resources (e.g. memory) that is has pointers to. It is automatically invoked when object goes out of scope. Inheritance Deriving properties of parent class to the child class. Polymorphism Defining functions with the same name, but different parameters. Overload A function is overloaded if there is more than one definition. See polymorphism. Sub class Same as child, derived, or inherited class. Super class Same as parent or base class. Attribute Same as data member or member field IMPLEMENTATION OF OOPS IN C++ Classes Public and Private sections All member fields in a class are private by default (no code outside the class can reference them), whereas fields of a struct are public, meaning that anyone can use them. For example, struct Product {char mfg_id[4]; // 4 char code for the manufacturer. char prod_id[8]; // 8-char code for the product int price; // price of the product in dollars. int qty_on_hand; // quantity on hand in inventory }; Could be rewritten as a class like this class Product { public: char mfg_id[4]; // 4 char code for the manufacturer. char prod_id[8]; // 8-char code for the product int price; // price of the product in dollars. int qty_on_hand; // quantity on hand in inventory }; A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members that can either be data or function declarations, and optionally access specifiers. 13 All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire: • private members of a class are accessible only from within other members of the same class or from their friends. • protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. • Finally, public members are accessible from anywhere where the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example: class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition. Notice the difference between the class name and the object name: In the previous example, CRectangle was the class name (i.e., the type), whereas rect was an object of type CRectangle. It is the same relationship int and a have in the following declaration: int a; where int is the type name (the class) and a is the variable name (the object). After the previous declarations of CRectangle and rect, we can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member.