Chapter: Object Oriented Programming and Data Structure : Data Abstraction & Overloading

Dynamic memory in C

C++ integrates the operators new and delete for allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions malloc, calloc, realloc and free, defined in the header (known as in C).

Dynamic memory in C

 

C++ integrates the operators new and delete for allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions malloc, calloc, realloc and free, defined in the header <cstdlib> (known as <stdlib.h> in C). The functions are also available in C++ and can also be used to allocate and deallocate dynamic memory.

 

Note, though, that the memory blocks allocated by these functions are not necessarily compatible with those returned by new, so they should not be mixed; each one should be handled with its own set of functions or operators.

 

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

 

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

 

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

 

class Box

{

 

double width; public:

double length;

friend void printWidth( Box box );

voidsetWidth( double wid );

};

 

To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne: friend class ClassTwo;

 

Consider the following program:

 

#include <iostream>

 

using namespace std;

class Box

{

 

double width; public:

friend void printWidth( Box box );

voidsetWidth( double wid );

};

// Member function definition

void Box::setWidth( double wid )

{

width = wid;

}

 

// Note: printWidth() is not a member function of any class. voidprintWidth( Box box )

{

 

/* Because printWidth() is a friend of Box, it can directly access any member of this class */

 

 

 

cout<< "Width of box : " <<box.width<<endl;

}

 

//  Main function for the program int main( )

{

Box box;

 

//  set box width without member function

box.setWidth(10.0);

 

//  Use friend function to print the wdith.

printWidth( box );

return 0;

}

 

When the above code is compiled and executed, it produces the following result: Width of box : 10

 

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.

 

Let us take previously defined class to access the members of the class using a member function instead of directly accessing them:

class Box

{

public:

 

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

doublegetVolume(void);// Returns box volume

};

 

Member functions can be defined within the class definition or separately using scope resolution operator, ::. Defining a member function within the class definition declares the function inline, even if you do not use the inline specifier. So either you can

 

define Volume() function as below: class Box

 

{

public:

 

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

doublegetVolume(void)

 

{

return length * breadth * height;

}

};

 

If you like you can define same function outside the class using scope resolution operator, :: as follows:

 

double Box::getVolume(void)

{

return length * breadth * height;

}

 

Here, only important point is that you would have to use class name just before :: operator. A member function will be called using a dot operator (.) on a object where it will manipulate data related to that object only as follows:

 

Box myBox; // Create an object

myBox.getVolume(); // Call member function for the object

 

Let us put above concepts to set and get the value of different class members in a class:

#include <iostream>

 

using namespace std;

class Box

{

public:

 

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

// Member functions declaration

doublegetVolume(void);

voidsetLength( double len );

voidsetBreadth( double bre );

voidsetHeight( double hei );

};

 

//Member functions definitions

double Box::getVolume(void)

{

return length * breadth * height;

}

void Box::setLength( double len )

{

length = len;

}

 

void Box::setBreadth( double bre )

{

breadth = bre;

}

void Box::setHeight( double hei )

{

height = hei;

}

 

///Main function for the program

int main( )

{

 

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

double volume = 0.0; // Store the volume of a box here

//  box 1 specification

Box1.setLength(6.0);

Box1.setBreadth(7.0);

Box1.setHeight(5.0);

 

//box 2 specification

Box2.setLength(12.0);

Box2.setBreadth(13.0);

Box2.setHeight(10.0);

 

//  volume of box 1

volume = Box1.getVolume();

 

cout<< "Volume of Box1 : " << volume <<endl; // volume of box 2

volume = Box2.getVolume();

 

cout<< "Volume of Box2 : " << volume <<endl;

return 0;

}

 

When the above code is compiled and executed, it produces the following result: Volume of Box1 : 210

Volume of Box2 : 1560

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Data Abstraction & Overloading : Dynamic memory in C |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.