Welcome to the Class Hierarchies,Library and Containers MCQs Page
Dive deep into the fascinating world of Class Hierarchies,Library and Containers with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Class Hierarchies,Library and Containers, a crucial aspect of Object Oriented Programming Using C++. In this section, you will encounter a diverse range of MCQs that cover various aspects of Class Hierarchies,Library and Containers, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Object Oriented Programming Using C++.
Check out the MCQs below to embark on an enriching journey through Class Hierarchies,Library and Containers. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Object Oriented Programming Using C++.
Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Class Hierarchies,Library and Containers. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!
Class Hierarchies,Library and Containers MCQs | Page 6 of 15
Explore more Topics under Object Oriented Programming Using C++
#include <new>
#include <iostream>
using namespace std;
struct A
{
virtual ~A() { };
void operator delete(void* p)
{
cout << "A :: operator delete" << endl;
}
};
struct B : A
{
void operator delete(void* p)
{
cout << "B :: operator delete" << endl;
}
};
int main()
{
A* ap = new B;
delete ap;
}
#include <iostream>
using namespace std;
struct A
{
virtual ~A()
{
cout << "~A()" << endl;
}
void operator delete[](void* p, size_t)
{
cout << "A :: operator delete[]" << endl;
delete [] p;
}
};
struct B : A
{
void operator delete[](void* p, size_t)
{
cout << "B :: operator delete[]" << endl;
delete [] p;
}
};
int main()
{
A* bp = new B[3];
delete[] bp;
};
#include <cstdlib>
#include <iostream>
using namespace std;
class X
{
public:
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (p == 0)
throw "malloc() failed";
return p;
}
void operator delete(void* p)
{
cout << "X :: operator delete(void*)" << endl;
free(p);
}
};
class Y
{
int filler[100];
public:
void operator delete(void* p, size_t sz) throw (const char*)
{
cout << "Freeing " << sz << " bytes" << endl;
free(p);
};
};
int main()
{
X* ptr = new X;
delete ptr;
Y* yptr = new Y;
delete yptr;
}
#include <new>
#include<cstdlib>
#include <iostream>
using namespace std;
class X;
struct Node
{
X* data;
bool filled;
Node() : filled(false) { }
};
class X
{
static Node buffer[];
public:
int number;
enum { size = 3};
void* operator new(size_t sz) throw (const char*)
{
void* p = malloc(sz);
if (sz == 0)
throw "Error: malloc() failed";
cout << "X :: operator new(size_t)" << endl;
return p;
}
void *operator new(size_t sz, int location) throw (const char*)
{
cout << "X :: operator new(size_t, " << location << ")" << endl;
void* p = 0;
if (location < 0 || location >= size || buffer[location].filled == true)
{
throw "Error: buffer location occupied";
}
else
{
p = malloc(sizeof(X));
if (p == 0)
throw "Error: Creating X object failed";
buffer[location].filled = true;
buffer[location].data = (X*) p;
}
return p;
}
static void printbuffer()
{
for (int i = 0; i < size; i++)
{
cout << buffer[i].data->number << endl;
}
}
};
Node X::buffer[size];
int main()
{
try
{
X* ptr1 = new X;
X* ptr2 = new(0) X;
X* ptr3 = new(1) X;
X* ptr4 = new(2) X;
ptr2->number = 10000;
ptr3->number = 10001;
ptr4->number = 10002;
X :: printbuffer();
X* ptr5 = new(0) X;
}
catch (const char* message)
{
cout << message << endl;
}
}
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
const int bsize = 512;
int *pa;
bool allocate = true;
void get_memory()
{
cerr << "free store exhausted" << endl;
delete [] pa;
allocate = false;
}
void eat_memory(int size)
{
int *p = new int[size];
if (allocate)
eat_memory(size);
else
cerr << "free store addr = " << p << endl;
}
int main()
{
set_new_handler(get_memory);
pa = new int[bsize];
cerr << "free store addr = " << pa << endl;
eat_memory(bsize);
return 0;
}
Suggested Topics
Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!
Cyber Security
Understand the fundamentals of safeguarding digital assets with our Cyber Security...
Operating System
Dive deep into the core of computers with our Operating System MCQs. Learn about...