Yahoo Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

VIRTUAL FUNCTION

A member function that is declared within a base class and redefined by a derived class.

Precede the function's declaration in the base class with the keyword virtual

Implement the "one interface, multiple methods" philosophy that underlies Polymorphism.

The virtual function within the base class defines the form of the interface to that function.

Each redefinition of the virtual function by a derived class implements its operation as it relates specifically to the derived class.

class base {
public:
virtual void vfunc() {
cout << "This is base's vfunc().\n";
}
};

class derived1 : public base {
public:
void vfunc() {
cout << "This is derived1's vfunc().\n";
}
};

class derived2 : public base {
public:
void vfunc() {
cout << "This is derived2's vfunc().\n";
}
};

int main()
{
base *p, b;
derived1 d1;
derived2 d2;
// point to base
p = &b;
p->vfunc(); // access base's vfunc()
// point to derived1
p = &d1;
p->vfunc(); // access derived1's 	            // vfunc()
// point to derived2
p = &d2;
p->vfunc(); // access derived2's              	            //vfunc()
return 0;
}

a virtual function that has no definition within the base class.

virtual type func-name(parameter-list) = 0;

If the derived class fails to override the pure virtual function, a compile-time error will result

class number {
protected:
int val;
public:
void setval(int i) { val = i; }
// show() is a pure virtual function
virtual void show() = 0;
};

class hextype : public number {
public:
void show() {
cout << hex << val << "\n";
}
};

class dectype : public number {
public:
void show() {
cout << val << "\n";
}
};

class octtype : public number {
public:
void show() {
cout << oct << val << "\n";
}
};
int main()
{
dectype d;
hextype h;
octtype o;
d.setval(20);
d.show(); // displays 20  decimal
h.setval(20);
h.show(); // displays 14 - hexa
o.setval(20);
o.show(); // displays 24 - octal
}

- Anonymous December 10, 2007 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More