[go: up one dir, main page]

0% found this document useful (0 votes)
48 views10 pages

BCA Students: Memory Allocation Basics

1) Memory is allocated to class member functions only once, at the time of class definition and is shared between all objects, but memory for data members is allocated separately for each object when it is created. 2) A static data member is used to share a single data member between all objects of a class. It must be defined outside the class using the class name scope resolution operator. 3) A static member function can only access static data members and can be called using the class name without an object. It is useful when we want all objects to share the same implementation of a function.

Uploaded by

horizonsahib1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views10 pages

BCA Students: Memory Allocation Basics

1) Memory is allocated to class member functions only once, at the time of class definition and is shared between all objects, but memory for data members is allocated separately for each object when it is created. 2) A static data member is used to share a single data member between all objects of a class. It must be defined outside the class using the class name scope resolution operator. 3) A static member function can only access static data members and can be called using the class name without an object. It is useful when we want all objects to share the same implementation of a function.

Uploaded by

horizonsahib1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Sanskriti Computer Education College, Beawar

BCA Part – II | Kshitij Sir

Memory Allocation to Objects:

Previously it is stated that, there is no memory allocation to


class members when class is defined and memory allocation is
done only when objects are created.

This is partially true, because all member functions stored


into the memory only once, at the time of class definition.

That single copy of member functions will be shared by all


objects of the class, but memory allocation to data members
will be only at the creation of object and will be stored
separately, that’s why we called them partitioned memory area.
Example:
class item
{
int code;
float price;
public:
void getitem(int c,float p) //Memory Allocated
{
code=c;
price=p;
}
void putitem() //Memory Allocated
{
cout<<code<<price<<endl;
}
};
int main()
{
item x,y; //Memory allocated to code and price for each
//object x and y separately
x.getitem(101,250);
y.getitem(102,300);
int z; //Memory allocation to code and price for z
z.getitem(103,500);

}
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Memory Allocation Process

getitem()
class definition
putitem()

object creation
x y z

Code=101 Code=102 Code=103

Price=250 Price=300 Price=500


Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Static Data Member: (class Variables)

When we want to share a single data member with all objects of


a class, than it must be declared as static data member.

Syntax to declare static data member:


static type variablename;

static data members are by-default initialized with zero(0).

Note:
Static Data Member must be compulsorily defined outside class
using class membership label.

datatype classname:: staticvariablename[=initial value];

int item::count; //bydefault initialized to zero


int item::count=5; //explicitly initialized to 5

Setitem()
class definition Getitem()
Count=0

object creation
x y z

Code=101 Code=102 Code=103

Price=250 Price=300 Price=500


Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Example:

#include <iostream>
#include <string.h>
using namespace std;

class item
{
int code;
float price;
static int count; //memory allocated

public:
void setitem(int c,float p) //memory allocated
{
count++;
code=c;
price=p;
}
void getitem() //memory allocated
{
cout<<"\n\n\tItem Code : "<<code<<endl;
cout<<"\n\n\tItem Price : "<<price<<endl;
}
void showcount()
{
cout<<"\n\n\tTotal Objects Count = "<<count<<endl;
}

};
int item::count; //Compulsory
int main()
{
item x,y,z;

x.setitem(101,250); //count=1
x.showcount(); //1
y.showcount(); //1
z.showcount(); //1
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

y.setitem(102,300); //count=2
x.showcount(); //2
y.showcount(); //2
z.showcount(); //2

x.getitem();

cout<<"\n\n\t-------"<<endl;
y.getitem();

z.setitem(103,500); //count=3
x.showcount(); //3
y.showcount(); //3
z.showcount(); //3
cout<<"\n\n\t-------"<<endl;
z.getitem();

cout << endl << endl;


return 0;
}
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Static Member Function:

A member function can also be declared as static by prefix


static keyword in function definition.

A static member function can only contain static data members


not any non-static data member in its definition or body.

Note:
A normal member function can handle static and non-static.
But a static member function can handle only static.

static void showcount()


{
//Error if code and price (non-static) written
cout<<"\n\n\tTotal Objects Count = "<<count<<endl;
}

A static member function can be called without object using


class name and scope resolution.

item::showcount();

Remember following calls still valid. But without benefit of


static member function.
x.showcount();
y.showcount();
z.showcount();
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Example of Static Member Function:

#include <iostream>
#include <string.h>
using namespace std;

class item
{
int code;
float price;
static int count; //memory allocated

public:
void setitem(int c,float p) //memory allocated
{
count++;
code=c;
price=p;
}
void getitem() //memory allocated
{
cout<<"\n\n\tItem Code : "<<code<<endl;
cout<<"\n\n\tItem Price : "<<price<<endl;
}
static void showcount()
{
cout<<"\n\n\tTotal Objects Count = "<<count<<endl;
}

};
int item::count; //Compulsory
int main()
{
item x,y,z;

item::showcount(); //0

x.setitem(101,250); //count=1
x.getitem();
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

cout<<"\n\n\t-------"<<endl;

//item::showcount(); //1

y.setitem(102,300); //count=2
y.getitem();
cout<<"\n\n\t-------"<<endl;
//item::showcount(); //2

z.setitem(103,500); //count=3
z.getitem();

item::showcount(); //3
cout << endl << endl;
return 0;
}

Constant Member Function:

In case, we don’t want to permit the function or protect the


arguments by altering inside the function, we declare the
arguments as constant - const argument.

In C++, we can declare a member function as constant member


function by appending const keyword in declaration and
definition.
int xyz::add(int &a,int &b) const
{
//a=10;
//b=20;
return a+b;
}

Furthermore, a const member function will be called using a


const object.
const item x(105,25.66); //const object
//x.setitem(); //Error can’t modify the data member using any
function
Sanskriti Computer Education College, Beawar
BCA Part – II | Kshitij Sir

Local Classes:
We can create a local class but it have so many restriction
such as we cannot create static data members and even the
objects outside function, so local class have very limited
purpose and rarely useful, that’s why it is not recommended.

#include <iostream>
#include <string.h>
using namespace std;
test()
{
class alpha
{
int a;
//static int m; //not allowed
public:
void set()
{
a=10;
}
void get()
{
cout<<a;
}

};
alpha A;
A.set();
A.get();
}
int main()
{
//alpha X; NOT ALLOWED
test();

cout << endl << endl;


return 0;
}
--End--

You might also like