Fill in the blank: 1. The _____________ member variables have access to the clas
ID: 3582300 • Letter: F
Question
Fill in the blank:
1. The _____________ member variables have access to the class, its friends, and its children.
True or false?
(a) Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters
(b) A class may not have another class type object as a member.
1. Debugging:
The code below has five errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. Assume that include <string>, <iostream> are loaded as well as using namespace std.
The expected output is:
Ms. Ladybug eats.
The ladybug flew to a window.
You must find and correct all five of the errors. If there is a blank line, you may insert a command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Insect{
virtual void Fly() = 0;
virtual void Eat() = 0;
string GetName() {return m_name;}
void SetName(string name) {m_name = name;};
private:
int m_numLegs;
bool m_canFly;
string name;
}
class Ladybug : public Insect{
public:
Ladybug(){SetName("Ms. Ladybug");}
void Fly();
void Eat();
};
void Ladybug::Eat(){
cout << GetName() << " eats." << endl;
}
string Ladybug::Eat(){
cout << GetName() << " eats." << endl;
}
int main (){
Ladybug red;
Insect * red1 = red;
red1->Eat();
red1->Fly();
return 0;
}
(a) Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters
(b) A class may not have another class type object as a member.
1. Debugging:
The code below has five errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. Assume that include <string>, <iostream> are loaded as well as using namespace std.
The expected output is:
Ms. Ladybug eats.
The ladybug flew to a window.
You must find and correct all five of the errors. If there is a blank line, you may insert a command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Insect{
virtual void Fly() = 0;
virtual void Eat() = 0;
string GetName() {return m_name;}
void SetName(string name) {m_name = name;};
private:
int m_numLegs;
bool m_canFly;
string name;
}
class Ladybug : public Insect{
public:
Ladybug(){SetName("Ms. Ladybug");}
void Fly();
void Eat();
};
void Ladybug::Eat(){
cout << GetName() << " eats." << endl;
}
string Ladybug::Eat(){
cout << GetName() << " eats." << endl;
}
int main (){
Ladybug red;
Insect * red1 = red;
red1->Eat();
red1->Fly();
return 0;
}
Explanation / Answer
1. The ___public/protected___ member variables have access to the class, its friends, and its children.
2.
(a) Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters. True. This is what the drawback of recursive functions.
(b) A class may not have another class type object as a member. False. A class is allowed to have any kind of objects as its members.
Here is the error-free code for you:
#include <iostream>
using namespace std;
class Insect{
public: //The methods should be public.
virtual void Fly() = 0;
virtual void Eat() = 0;
string GetName() {return m_name;}
void SetName(string name) {m_name = name;};
private:
int m_numLegs;
bool m_canFly;
string m_name; //The variable should be declares as m_name.
}; //Class should end with a semicolon.
class Ladybug : public Insect{
public:
Ladybug(){SetName("Ms. Ladybug");}
void Fly();
void Eat();
};
void Ladybug::Eat(){
cout << GetName() << " eats." << endl;
}
void Ladybug::Fly(){
cout << GetName() << " flew to a window." << endl;
}
int main (){
Ladybug red;
Insect * red1 = &red; //It's a pointer, and should be assigned address.
red1->Eat();
red1->Fly();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.