Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

how to create object

408 views

Published on

passing object

Published in: Education
  • Be the first to comment

  • Be the first to like this

how to create object

  1. 1. Marshal art with objects Author – Subhasis Nayak
  2. 2. Rule - 1 <ul><li>Defining class does not create object of that class. </li></ul><ul><li>It just tell compiler what type of information the object of this class type will hold. </li></ul><ul><li>You can not create object of a class before the defining the class. </li></ul><ul><li>Objects can be created like any other variable, using the class name. </li></ul>
  3. 3. Let’s define a class & create it’s object <ul><li>Defining class </li></ul><ul><li>Creating object </li></ul><ul><ul><li>class student { </li></ul></ul><ul><ul><li>int id; </li></ul></ul><ul><ul><li>char name[25]; </li></ul></ul><ul><ul><li>}; </li></ul></ul>student stu1,stu2;
  4. 4. Rule -2 <ul><li>Member function memory allocation done at when they art defined it means before the object created ,we can say when class is created. </li></ul><ul><li>No separated memory space is allocated for member function when object is created. </li></ul><ul><li>But the data members memory allocation is done when object is created. </li></ul>Why all these happened so?
  5. 5. Array of objects <ul><li>An array having class type elements known as array of objects. </li></ul><ul><li>We can create an array which each block will hold an object . </li></ul><ul><li>The objects must be of a single class. Why? </li></ul><ul><ul><li>class student { </li></ul></ul><ul><ul><li>int id; </li></ul></ul><ul><ul><li>char name[25]; </li></ul></ul><ul><ul><li>}; </li></ul></ul>student stu[6];
  6. 6. Objects as function <ul><li>We can use our objects as function argument as we do for variables. </li></ul><ul><li>There are two way of passing object as arguments: </li></ul><ul><ul><li>By value – function creates a copy of that object and used it. Here the original will not affected </li></ul></ul><ul><ul><li>By reference – here the object’s memory address passed. Hence function work with original object not with it’s copy. Original will affected. </li></ul></ul>
  7. 7. Syntax for object as arguments for function <ul><li>By value </li></ul><ul><li>By reference </li></ul><ul><li>Class math{ </li></ul><ul><li>Int x =100; </li></ul><ul><li>Void multi(math & m){ </li></ul><ul><li>m.x= m.x+100; </li></ul><ul><li>Cout<<“ ”<<m.x; </li></ul><ul><li>} </li></ul><ul><li>Void display(math m){ </li></ul><ul><li>Cout<<“ ”<<m.x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class math{ </li></ul><ul><li>Int x =100; </li></ul><ul><li>Void multi(math m){ </li></ul><ul><li>m.x= m.x+100; </li></ul><ul><li>Cout<<“ ”<<m.x; </li></ul><ul><li>} </li></ul><ul><li>Void display(math m){ </li></ul><ul><li>Cout<<“ ”<<m.x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class math{ Int x =100; Void multi(math m){ t.x= x+100; Cout<<“ ”<<m.x; } Void display(math m){ Cout<<“ ”<<m.x; } };
  8. 8. Program

×