Dynamic Memory Allocation
Dynamic Memory Allocation
OR
}
Example 2: Allocating memory to ‘n’ number
objects: Dynamic object
#include <iostream> int main()
using namespace std;
class student
{
{ student *s;
int roll_no;
int i,n;
string name;
public: cout<<"enter the number of
void get() students\n";
{ cin>>n;
cout<<"Enter roll_no and name\n";
cin>>roll_no; s=new student[n];
fflush(stdin); for(i=0;i<=n-i;i++)
getline(cin,name);
s[i].get();
}
void show() for(i=0;i<=n-i;i++)
{ s[i].show();
cout<<endl<<roll_no<<" "<<name;
}
}; }
MCQ 1
• In CPP, dynamic memory allocation is done
using ______________ operator.
a. calloc()
b. malloc()
c. allocate
d. New
• Which of the following is/are valid ways to
allocate memory for an integer by dynamic
memory allocation in CPP?
Syntax:
delete pointer_variable
Or
delete [] pointer_variable
• Can we allocate memory for the objects
dynamically in CPP?
Yes
No
The correct syntax of new operator is:
a. data_type * var= new data_type(value);
b. data_type * var= new data_type;
c. data_type * var= new data_type[size];
d. All of mentioned above
• Which of the following is true about new when
compared with malloc. 1) new is an operator, malloc is
a function 2) new calls constructor, malloc doesn't 3)
new returns appropriate pointer, malloc returns void *
and pointer needs to typecast to appropriate type.
A. 1 and 3
B. 2 and 3
C. 1 and 2
D. All 1, 2 and 3
Predict the output?
#include <iostream>
using namespace std;
class Test
{
int x;
Test() { x = 5;}
};
int main()
{
Test *t = new Test;
cout << t->x;
}
a. Compiler Error
b. 5
c. 0
d. Garbage Value
• Write a program to declare a class employee
with id, name and salary. Allocate memory to
‘n’ number of employees and write the details
onto file emp.txt and then read the file print
the details.
Virtual Function in C++
void show()
// Virtual function, binded at
{ runtime
cout << "show base class\n";
} bptr->print();
};
void show()
{ return 0;
cout << "show derived class\n";
} }
};
Explanation
Runtime polymorphism is achieved only through a pointer (or
reference) of base class type. Also, a base class pointer can point to
the objects of base class as well as to the objects of derived class. In
above code, base class pointer ‘bptr’ contains the address of object
‘d’ of derived class.
Late binding (Runtime) is done in accordance with the content of
pointer (i.e. location pointed to by pointer) and Early binding
(Compile time) is done according to the type of pointer, since print()
function is declared with virtual keyword so it will be bound at
runtime (output is print derived class as pointer is pointing to object
of derived class) and show() is non-virtual so it will be bound during
compile time (output is show base class as pointer is of base type).
Problem on virtual function
• Write a program to declare a class shape with 2 data
member and 1 virtual function area(). Make 2 derived
classes rectangle and triangle which will calculate the area
of triangle and rectangle by overriding area() function.
• Write a program to declare a class person with name and
city as data members. Declare student and teacher class
that inherits from person class to show the details of
respective classes. Assume parameterized constructor in
both the classes. Assume virtual function void show() in
base class person that will be overridden in later inherited
classes.
C++ Function Overriding
/* Other members */
};
#include<iostream> // This class inherits from Base and
implements fun()
using namespace std; class Derived: public Base
{
int y;
class Base public:
void fun() { cout << "fun() called"; }
{ };
int x;
int main(void)
public: {
virtual void fun() = 0; Derived d;
d.fun();
int getX() { return x; } return 0;
}; }
Program Example
Write a program to declare a class shape with 2
data member and 1 pure virtual function area().
Make 2 derived classes rectangle and triangle
which will calculate the area of triangle and
rectangle by overriding area() function.
Which among the following best describes polymorphism?
class A {
int* p;
public:
A()
{
p = new int[3]{ 1, 2, 3 };
for (int i = 0; i < 3; i++)
{
cout << p[i] << " ";
}
cout << endl;
}
};
int main()
{
A* ptr = new A[5];
}
//In this program we have created array of object dynamically. The first object is ptr[0], second is ptr[1] and so on . For each
object creation default constructor is called and for each object memory is allocated to pointer type variable by new operator.
In the case of friend operator functions how
many object arguments a binary operator
overloaded function can take?
a) 1
b) 2
c) 3
d) 0
Choose the correct sequence of destructors bein
g called for the following code.
class A
{
};
class B
{
};
class C: public A, public B
{
};
a) ~A(), ~B(), ~C()
b) ~B(), ~C(), ~A()
c) ~A(), ~C(), ~B()
d) ~C(), ~B(), ~A()
Destructors can be ________
a) Abstract type
b) Virtual
c) Void
d) Any type depending on situation
Which among the following is correct for the
destructors concept?
a) Destructors can be overloaded
b) Destructors can have only one parameter at
maximum
c) Destructors are always called after object goes
out of scope
d) There can be only one destructor in a class