Debug the given code to print the desired output.Sample InputCC++Sample OutputC++ Font SizeLanguage Editor Theme #include<iostream>#include<string>using namespace std;1234567891011121314151617181920212223class A{ public: string word1; string word2; A() { cin>>word1; cin>>word2; } void display() { cout <<word1<<endl; } }; class B:public A { public: void display() { cout <<word2<<endl; } }; int main() { A* a; B b; a = &b; a->display(); }
Question
Debug the given code to print the desired output.Sample InputCC++Sample OutputC++ Font SizeLanguage Editor Theme #include<iostream>#include<string>using namespace std;1234567891011121314151617181920212223class A{ public: string word1; string word2; A() { cin>>word1; cin>>word2; } void display() { cout <<word1<<endl; } }; class B:public A { public: void display() { cout <<word2<<endl; } }; int main() { A* a; B b; a = &b; a->display(); }
Solution
The code you provided is written in C++. The problem with the code is that it's trying to use polymorphism without declaring the display() function in the base class (A) as virtual.
In C++, for polymorphism to work, you need to declare the function you want to override in the derived class (B) as virtual in the base class (A).
Here's the corrected code:
#include<iostream>
#include<string>
using namespace std;
class A{
public:
string word1;
string word2;
A() {
cin>>word1;
cin>>word2;
}
virtual void display() {
cout <<word1<<endl;
}
};
class B:public A
{
public:
void display() {
cout <<word2<<endl;
}
};
int main()
{
A* a;
B b;
a = &b;
a->display();
}
Now, when you call a->display(), it will correctly call the display() function in class B, because we've declared display() as virtual in class A. This is a fundamental concept in C++ known as polymorphism.
Similar Questions
Debug the given code to print the desired output.Sample InputCC++Sample OutputC++ Font SizeLanguage Editor Theme #include<iostream>#include<string>using namespace std;1234567891011121314151617181920212223class A{ public: string word1; string word2; A() { cin>>word1; cin>>word2; } void display() { cout <<word1<<endl; } }; class B:public A { public: void display() { cout <<word2<<endl; } }; int main() { A* a; B b; a = &b; a->display(); }
Select the correct answerWhat is the output of the following code snippet?printf("%d", sizeof(double));Options8416Compiler-dependent
elect the correct answerWhat is the output of the following code snippet?printf("%d", sizeof(double));Options4168Compiler-dependent
PreprocessorWhat will be the output of the following C code?#include <stdio.h>#define int charvoid main() { int i = 65; printf("%d", sizeof(i));}Options42Compilation Error1
Arun is learning C language. His first program is to print his name in the console with proper spacing and spelling. Help him to achieve the task.Input format :No console input.Output format :The output displays the name as follows: "My Name is Arun."
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.