0% found this document useful (0 votes)
15 views14 pages

Lec-15 Dynamic Memory Allocation New

The document discusses the concept of virtual base classes in object-oriented programming to resolve ambiguity in multiple inheritance scenarios. It explains how declaring a base class as virtual allows for a single copy of its data members to be shared among derived classes, thus avoiding duplication. Additionally, it covers dynamic memory allocation in C++, detailing the use of the 'new' and 'delete' operators for managing memory at runtime.

Uploaded by

Shreya Singh
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)
15 views14 pages

Lec-15 Dynamic Memory Allocation New

The document discusses the concept of virtual base classes in object-oriented programming to resolve ambiguity in multiple inheritance scenarios. It explains how declaring a base class as virtual allows for a single copy of its data members to be shared among derived classes, thus avoiding duplication. Additionally, it covers dynamic memory allocation in C++, detailing the use of the 'new' and 'delete' operators for managing memory at runtime.

Uploaded by

Shreya Singh
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/ 14

Object Oriented Programming

Mohona Ghosh
Virtual Base Class
• Consider the situation where we have one
class A . This class A is inherited by two
other classes B and C. Both these class are
inherited into another in a new class D as
shown.
• As we can see from the figure that data
members/function of class A are inherited
twice to class D. One through class B and
second through class C.
• When any data / function member of
class A is accessed by an object of class D,
ambiguity arises as to which data/function
member would be called? One inherited
through B or the other inherited through C.
This confuses compiler and it displays
error.
#include <iostream>
using namespace std;

class A {
public:
Virtual Base Class
void show()
{
cout << "Hello form A \n";
}
};

class B : public A {
};

class C : public A {
};

class D : public B, public C {


};

int main()
{
D object;
object.show();
}
Virtual Base Class
• To resolve this ambiguity when class A is
inherited in both class B and class C, it is
declared as virtual base class by placing a
keyword virtual.
• virtual can be written before or after
the public.
• Now only one copy of data/function member
will be copied to class C and class B and
class A becomes the virtual base class.
• Virtual base classes offer a way to save space
and avoid ambiguities in class hierarchies that
use multiple inheritances.
• When a base class is specified as a virtual
base, it can act as an indirect base more than
once without duplication of its data members.
• A single copy of its data members is shared by
all the base classes that use virtual base.
#include <iostream>
using namespace std;

class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};

int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;
return 0;
}
Dynamic Memory Allocation
• Many times, you are not aware in advance how much memory you will
need to store particular information in a defined variable and the size of
required memory can be determined at run time.
• C++ allows us to allocate the memory of a variable or an array in run time. This
is known as dynamic memory allocation.
• Also, we need to deallocate the dynamically allocated memory manually after
we have no use for the variable.
• We can allocate and then deallocate memory dynamically using the new and
delete operators respectively.
• One use of dynamically allocated memory is to allocate memory of variable size,
which is not possible with compiler allocated memory.
• The most important use is the flexibility provided to programmers. We are free
to allocate and deallocate memory whenever we need it and whenever we
don’t need it anymore.
Dynamic Memory Allocation
New Operator
• We can allocate memory at run time within the heap for the
variable of a given type using a special operator in C++ which
returns the address of the space allocated.
• This operator is called new operator.
• The new operator denotes a request for memory allocation on the
Heap. If sufficient memory is available, a new operator initializes the
memory and returns the address of the newly allocated and initialized
memory to the pointer variable.
• Syntax: pointer-variable = new data-type;
New Operator
New Operator
#include <iostream>
• In the case of an using namespace std;
array, the new int main() {
operator returns int num;
cout << "Enter total number of students: ";
the address of cin >> num;
the first element float* ptr;

of the array. // memory allocation of num number of floats


ptr = new float[num];

cout << "Enter GPA of students." << endl;


for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}

cout << "\nDisplaying GPA of students." << endl;


for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": " << *(ptr + i) << endl;
}

// ptr memory is released


delete[] ptr;
return 0;
}
Delete Operator
Delete Operator
• Misuse of delete operator can be disastrous

You might also like