You are on page 1of 12

MEMORY ALLOCATION

Memory Allocation for Objects

Memory space for objects is allocated when they are declared & not when the class is specified. Is it TRUE? All the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created.

Static Data Members

A data member of a class can be qualified as static. The properties of a static member variable are similar to that of a C static variable. Used to maintain values common to the entire class.

Static Data Members

Contd

Characteristics It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for the entire class & is shared by all the objects of that class. No matter how many objects are created. Visible only within the class.

Example of static data member


#include<iostream.h> #include<conio.h> class abc { static int count; //static data member int number; //non static member public: void getdata(int a) //function declaration { number = a; count ++; } void getcountI(void) { cout<<count: ; cout<<count<<\n; } };

Contd
int item :: count; int main() { item a, b, c ; a.getcount(); b.getcount(); c.getcount(); a.getdata(20); a.getdata(30); //definition of static data member //count is initiated to zero //display count

//getting data into object a, b, c

a.getdata(40); cout<<After reading data; a.getcount(); //display count b.getcount(); c.getcount(); return 0; }

output:
count: 0 count: 0 count: 0 After reading data count: 3 count: 3 count: 3

Static Member Functions

A member function i.e. Declared static has the following properties:A static function can have access to only other static members (functions or variables) declared in the same class. static void getdata() //static member function A static member function can be called using the class name class_name :: function_name

Arrays of Objects

Array of any data type including struct. Array of variables that are of the type class. Such variables are called arrays of objects.
class emp { char name[20]; float age; public: void getdata(void); void putdata(void); };

Objects as Function Arguments


An object may be used as a function argument. This can be done in two ways: A copy of the entire object is passed to the function pass by value Only the address of the object is transferred to the function pass by reference Pass by reference method is more efficient.

Example: #include<iostream.h> #include<conio.h> class time { int hours; //variables declaration int minutes; public: void gettime(int h, int m) //function declaration { hours = h; minutes = m; } void putdata(void) { cout<<hours<<hours and; cout<<minutes<<minutes \n; } void sum(time, time); //declaration with objects as arguments };

Example: void time :: sum(time t1, time t2) //t1 & t2 are objects { minutes = t1.minutes + t2.minutes; hours = minutes/60; minutes = minutes%60; hours = hours + t1.hours + t2.hours; } int main() { time x, y, z; x.gettime(2,45); y.gettime(3,30); z.sum(x,y); //z=x+y cout<<x =; x.puttime(); OUTPUT: cout<<y =; y.puttime(); x = 2 hours and 45 minutes cout<<z =; z.puttime(); x = 3 hours and 30 minutes return 0; x = 6 hours and 15 minutes }

Thanks!!!

You might also like