Question

10. Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it? 1. #include < iostream > 2. using namespace std; 4 class Computer int Id; 7. public: Computer(int id) this -Id- id; ) void process() cout << Computer::process(); 9 10. H; 12. class Employee 13. 14. 15. public: 16 Computer* c; Employee )cnew Computer (123); 17.Employee() C 18. 19. 20 21. 22. ^; 23. 24. int main(){ 25. 26 27. 28 void foo() f cout << Employee::foo(); c-> process(); Employee ob; ob.foo); return 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer; The above code is valid and it will produce the below output as well

Employee::foo()Computer::process()

#include <iostream>

using namespace std;
class Computer{
int Id;
public:
Computer(int id){this->Id=id;};
void process(){cout<<"Computer::process()";}
};

class Employee{
Computer* c;
public:
Employee(){c=new Computer(123);
}
~Employee(){}
void foo(){
cout<<"Employee::foo()";
c->process();
}
};

int main()
{

Employee ob;
ob.foo();
return 0;
}

Output:

Employee::foo()Computer::process()

Add a comment
Know the answer?
Add Answer to:
10. Does the following code compile? Does it run? Is there any problem with the code?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with a multiple inheritance work Question Compile and run the file. If you find...

    Need help with a multiple inheritance work Question Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution inheritance.cpp file #include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << x; } }; class B:...

  • The following code will not compile. Fix it by inserting one line of code. Do not...

    The following code will not compile. Fix it by inserting one line of code. Do not change anything else. Hint: copy and paste the code below into your answer box, then make the required addition. Also include a comment indicating the line of code that you inserted, and why that line of code is required include <iostream> using namespace std int mainO cout <<"The word of the day is: sayword 0 return 0 void sayWordO f cout くく "WORD!";

  • Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...

    Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...

    Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...

  • C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code...

    C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; Answer #include using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; userNum = 1; cout << "Final: " << userNum << endl;

  • C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

    C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program. #include <iostream> using namespace std; class Shape { protected: // your code public: void setWidth (int w) { // your code } void setHeight (int h) { // your code } }; class Rectangle: public Shape { public: int...

  • When I run this code use ./main How to fix it when program reminds segmentation fault...

    When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)? #include <iostream> void set_num(int* i_prt, int num) { *i_prt = num; } int main(int argc, char** argv) { int x; set_num(&x, 13); std::cout << "x: " << x << std::endl; int* y; set_num(y, 4); std::cout << "*y:" << *y << std::endl; }

  • Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible...

    Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible would be really grateful to a quick solution. would give a thumbs up. Thank you! Q3: Question # 3 [20] Will the following code compile? If it does not, state the errors. If it does compile, write the output. //Function.cpp #include <iostream> using namespace std; void printData (long i) cout<<"In long print Data "«<i<<endl; } void printData(int i) cout<<"In int printData "<<i<<endl; ) void...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT