12438_8. Memory Allocat...

download 12438_8. Memory Allocat...

of 12

Transcript of 12438_8. Memory Allocat...

  • 7/29/2019 12438_8. Memory Allocat...

    1/12

    MEMORYALLOCATION

  • 7/29/2019 12438_8. Memory Allocat...

    2/12

    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 theobjects are created.

  • 7/29/2019 12438_8. Memory Allocat...

    3/12

    Static Data Members

    A data member of a class can be

    qualified as static.

    The properties of a static membervariable are similar to that of a C static

    variable.

    Used to maintain values common to the

    entire class.

  • 7/29/2019 12438_8. Memory Allocat...

    4/12

    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.

  • 7/29/2019 12438_8. Memory Allocat...

    5/12

    Example of static data member#include

    #include

    class abc

    {

    static int count; //static data member

    int number; //non static memberpublic:

    void getdata(int a) //function declaration

    { number = a;

    count ++;

    }

    void getcountI(void)

    { cout

  • 7/29/2019 12438_8. Memory Allocat...

    6/12

    Contdint item :: count; //definition of static data member

    int main()

    { item a, b, c ; //count is initiated to zero

    a.getcount(); //display count

    b.getcount();

    c.getcount();

    a.getdata(20); //getting data into object a, b, c

    a.getdata(30);

    a.getdata(40); output:

    cout

  • 7/29/2019 12438_8. Memory Allocat...

    7/12

    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 sameclass.

    static void getdata() //static member function

    A static member function can be called using the class

    nameclass_name :: function_name

  • 7/29/2019 12438_8. Memory Allocat...

    8/12

    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);

    };

  • 7/29/2019 12438_8. Memory Allocat...

    9/12

    Objects as FunctionArguments

    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 thefunction pass by value

    Only the address of the object is transferred

    to the function pass by reference

    Pass by reference method is more efficient.

  • 7/29/2019 12438_8. Memory Allocat...

    10/12

    Example: -#include

    #includeclass time

    {

    int hours; //variables declaration

    int minutes;public:

    void gettime(int h, int m) //function declaration

    { hours = h; minutes = m; }

    void putdata(void)

    { cout

  • 7/29/2019 12438_8. Memory Allocat...

    11/12

    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+ycout

  • 7/29/2019 12438_8. Memory Allocat...

    12/12

    Thanks!!!