R
Ramesh Tharma
Hi Guys,
I was asked the following question in an interview, is anyone knows the
anwers for these questions?
What output does the following code generate? Why?
What output does it generate if you make A::Foo() a pure virtual function?
#include <iostream>
using namespace std;
class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
};
class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
};
int main(int, char**)
{
B objectB;
return 0;
}
--------------------------------------------------------------------------------
What output does this program generate as shown? Why?
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw
"A::exception";
}
};
class B {
public:
B() {
cout << "B::B()" << endl; throw
"B::exception";
}
~B() {
cout << "B::~B()";
}
};
int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
} catch (char* ex) {
cout << ex << endl;
}
return 0;
}Thanks,Ramesh
I was asked the following question in an interview, is anyone knows the
anwers for these questions?
What output does the following code generate? Why?
What output does it generate if you make A::Foo() a pure virtual function?
#include <iostream>
using namespace std;
class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
};
class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
};
int main(int, char**)
{
B objectB;
return 0;
}
--------------------------------------------------------------------------------
What output does this program generate as shown? Why?
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw
"A::exception";
}
};
class B {
public:
B() {
cout << "B::B()" << endl; throw
"B::exception";
}
~B() {
cout << "B::~B()";
}
};
int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
} catch (char* ex) {
cout << ex << endl;
}
return 0;
}Thanks,Ramesh