Page 1 :
(ay Neal la), , , , , , , , , , , , , Classes and Objects, , , , , , Unit Outcomes, , Develop relevant friend functions to solve the given problem., , Write C++ program to use array of given objects,, , Write C++ program to create the given object using constructor., , Write program to delete the given object using destructor in C++ program., Topics and Sub-Topics, , — Class and Object : Introduction, Specifying a Class, Access Specifiers, Defining Member Functions,, Creating Objects and Memory Allocation for Objects., , - Static Data Members, Static Member Function, Friend Function., , — Array of Objects, Objects as Function Arguments., , — Concepts of Constructors, Types of Constructors., , = Multiple Constructors in a Class, Constructors with Default Arguments., , Destructors,, , , , , , Programming and Object Oriented, rogramming, , , , , , , , , , _(W-17, 4 Marks), , , , — As we have discussed in chapter 1, procedure oriented, , programming gives significance to procedure i.e. how to doa, task., , The structure of a procedure oriented programming as, discussed in chapter 1 is made up of functions. If a variable is, to be accessed by multiple functions then such a variable is, known as global variable,, , Disadvantages of procedure oriented programming language :, , 1. Global data is vulnerable to inadvertent changes. In, other words, the global variables may be changed, unintentionally,
Page 2 :
accessing this structure have to be, variables in the structure are chan;, , functions that use this structure will hay, 3. Procedure oriented programs cannot im, , world problems very well, i, importance to data rather than a procedure,, , chapter 1, is also shown in Fig, 5.1.1,, Object A, , , , , , , , , , , , , , , , , , , , , , , , , , Object C, , , , Funct ns, , Data, , , , , , , , BP ikig oat stenctace of af object oriented program, , ‘ Difference between structure and class, , , , , , , , Class is a reference type, and its object is created, , that is why its variable, on the heap memory., , , , , , 2. Class can inherit the, another class., , , , , , , , 3 Class can have the all, types of constructor and |, destructor., , Structure doesn’t have, Constructors or, destructors, , The member variable of, Sttucture “can not Be, initialized directly, —, , , , , , , , The member variable of, class can be initialized, directly., , , , , , eee), Object oriented Programming on the other hand gives more |, , The structure of object oriented programs as discussed in, , , , , , , , Structure is a Value type, , , , , , , , , , Object Oriented Pp, data rather than the, 1 P, , , , , , , — Object is given more, Othe procedure of doing it., , , , ics of abject orient!, , rogtamming gives significance to ob), Procedure. Let us see the features of More phasis is Biven to data rather than proce, ve Mie teal world problems that the dat, , More important than the procedure *, Perform a task, Hence, in object ‘, , , , 5 Marky, , (S-14, Marks), , (W-16, 4 Marks), , , , or th, , , , te", , nortan, impo, , , , , , , , , , , , , , , , , , , , , , , , , , , , |, (W.17, 4 Marks)!
Page 3 :
yotutes are designed such that they characteri, clerize, , ect i.e. all the information Tequired for an obj, d in the variables of that object, =, , data are associated with them as shown in, ae, , idden or cannot be access by extemal function., a of an object can be accessed only by the, ons of the same object,, , communicate with each other through functions,, , ion of an object wants the data of another, then it can be accessed only through the, ons., , a and functions are easily added when required., , never new data is to be added, all the functions, , not be changed; only that functions are to be, d, which require to access the data., , ws a bottom-up approach. In this type of, oach, each element is first specified in detail and, the entire system is made using these elements., | will notice in the programming of C++, that this, h of the, mming very simple., , bottom-up approach makes, , that we need to understand. First the concept of, classes and then the specialties of OOPs viz, Data, tl ion, Encapsulation, Inheritance, Polymorphism, etc., ferstand them one by one., , s. It is similar to 2 structure, besides data, , , , , , , , , , or a category of thing:, ference that it can also have functions, seen, can have only data variables, , Ire, we have i:, as function, , can have data members as well, , Programming using C++ (MSBTE) F, , = Glasses and bjs, , It is an instance or example of a class. You can imagine it to, be similar to a variable of class like we have a variable of a, , structure,, , > 3 Data abstraction, , Data abstraction is like defining or abstracting the object, according to the required parameters. For example; if there is, a class for circle we need to just define the radius of the, object of this class. We need not bother about anything else, of that object,, , > 4. Data encapsulation, , The data of an object is hidden from other objects. This is, called as encapsulation, Encapsulation is achieved by putting, data and functions associated with it into a class., , > 5, , The mechanism of deriving the properties of one class into, another class is known as inheritance. We will see in detail, in a special section dedicated on, , Inheritance, , about this concept, Inheritance., , => 6. Polymorphism, , Poly refers to multiple and morph refers to different forms., Hence, polymorphism means multiple forms of the same, thing. This topic will also be covered in detail in the later, , sections, , Syllabus Topic : Access Specifiers, , 5.1.2 Access Specifiers, , In C++, we have 3 access specifiers viz. public, protected and, private., , ‘These access specifiers are used to specify the access of the, data and function members of a class, , ‘As the name says, access specifier will indicate who can, access the data or function., , A “public” access specifier indicates that every function can, access this member., , Trhed bance, , A “protected” access specifier indicates that only classes, derieyed can use this member. A derieved class concept will, , be dealt later in the topic called as Inheritance., , , , y TechKnowledge, , Publications, , fothee £ Sane
Page 4 :
A “private” coke dete. indicates atic functions, , i of the same class can access the member., a __t, Syllabus Topic : Specifying a Class, , 5.1.3 Specifying a Class, , A class can be specified or created and then objects of that, class can be made., , A class will have data members and function members., , ‘All the objects of a class will have the same properties as that, of specified in the class, , , , , , , , , Syntax of specifying a class, , Syllabus Topic : Creating Objects and Memory, Ww # Allocation of Objects, , 5.1.4 Creating Objects and Memory, Allocation of Objects, D> (W-14, 5-15, w-16, $47), , , , An object of a class will be allocated the memory space as, required by the data members specified in the class, The, an instance (example) of the class., , ‘object is, , For example, if human being i is a class then we all are objects, pee oe en, , , , (instances or examples) of |, , , , , , 5-4, , , , Syntax for creating an object:, , class_name object_name;, , , , , re epciher aS, , the, , Example: Calculate the size of object BI define, following class :, , ee, Syllabus Topic : Defining Member Functions _, , 5.2 Defining Member Functions of a, , Class, > (w-i4, , , , we Can define memde! |, syntax., (W-44, 4 Marks, , , , To define the data members of a class the logic is simile!, that of the data members of a structure, But the new thin?, discussed some time back is the member functions of « ¢!*, , Tet us see how this can be done and what are the differ’, methods to do this.
Page 5 :
emally defined functions, functions, , Internally Defined Functions, > (S-15, W-16), , ‘see some examples of object oriented programs with, , T defined functions. We will see these examples for, , i of the concepts so as to make the understanding of the, a ept simple., , am 5.2.1 : Write a program to find area of circle, , 7 ig Object Oriented Programming such that the, ass circle must have three member functions, _ Ramely :, , _ (@) read() to accept the radius from the user., ( compute() for calculating the area., isplay() for displaying the result., , eee :, , , , , , , , and Objects, , , , , , Explanation :, , A class is created with the name ‘circle’. Two float type, variables are declared in it namely ‘r’ and ‘a’ for the radius, and area respectively., , ‘The three functions are written inside the class ‘circle’ namely, read(), compute() and display()., , The read() function accepts the value of radius from the user., The compute() function calculates the area of the circle. The, display() function displays the calculated area., , But all these functions will perform these tasks only when, , they are called. These functions can be called by the period, , operator used with an object of this class., , Ww TechKnowledge, , Publications