Question 1
Question 2
1) Comparison Operator ( == ) 2) Assignment Operator ( = )
Question 3
Question 4
Question 5
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class Test {
int x;
public:
void* operator new(size_t size);
void operator delete(void*);
Test(int i) {
x = i;
cout << "Constructor called \\n";
}
~Test() { cout << "Destructor called \\n"; }
};
void* Test::operator new(size_t size)
{
void *storage = malloc(size);
cout << "new called \\n";
return storage;
}
void Test::operator delete(void *p )
{
cout<<"delete called \\n";
free(p);
}
int main()
{
Test *m = new Test(5);
delete m;
return 0;
}
new called Constructor called delete called Destructor called
new called Constructor called Destructor called delete called
Constructor called new called Destructor called delete called
Constructor called new called delete called Destructor called
Question 6
#include<iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point() : x(0), y(0) { }
Point& operator()(int dx, int dy);
void show() {cout << "x = " << x << ", y = " << y; }
};
Point& Point::operator()(int dx, int dy)
{
x = dx;
y = dy;
return *this;
}
int main()
{
Point pt;
pt(3, 2);
pt.show();
return 0;
}
Question 7
Question 8
Question 9
#include <iostream>
using namespace std;
class Test2
{
int y;
};
class Test
{
int x;
Test2 t2;
public:
operator Test2 () { return t2; }
operator int () { return x; }
};
void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }
int main()
{
Test t;
fun(t);
return 0;
}
Question 10
#include<iostream>
using namespace std;
class A
{
int i;
public:
A(int ii = 0) : i(ii) {}
void show() { cout << i << endl; }
};
class B
{
int x;
public:
B(int xx) : x(xx) {}
operator A() const { return A(x); }
};
void g(A a)
{
a.show();
}
int main()
{
B b(10);
g(b);
g(20);
return 0;
}
10 20
20 20
10 10
There are 11 questions to complete.