C++
5a)We have the following class:
class Banana
{
private:
string s;
int y;
public:
bool theMemberFunc (string);
void setterForS(string); // setter for s
string getterForS(); // getter for s
};
Instantiate a static object of Banana named co.
a)int Banana;
b)co.Banana = new;
c)Banana = new class;
d)Banana co;
5b)Code a call to function aNonclassFunction passing co.
a)aNonclassFunction (co);
b)aNonclassFunction (* co);
c)aNonclassFunction (aGoodClass.co);
d)aNonclassFunction (aGoodClass);
5c)Code the function definition for aNonclassFunction, picking up co. aNonclassFunction has no return value.
a)void aNonclassFunction (co);
b)void aNonclassFunction (Banana co);
c)void aNonclassFunction (* co);
d)void aNonclassFunction (Banana * co);
5d)Assume aNonclassFunction changes a member of co by using one of the setters provided by the class. What happens to the value of co in the calling program?
a)Nothing.
b)It changes to whatever the function changed it to.
c)This can't happen because the setter is only available to main.
d)This can't happen because the setter can only be used once.
5e)Change the function definition for aNonclassFunction so that aNonclassFunction uses the same memory locations for co as in the calling program.
a)void aNonclassFunction (* co);
b)void aNonclassFunction (Banana &co);
c)void aNonclassFunction (Banana * co);
d)void aNonclassFunction (&co Banana);
5f)Assume that the newly changed aNonclassFunction changes a member of co by using one of the setters provided by the class. What happens to the value of co in the calling program?
a)Nothing
b)A memory exception will result if aNonclassFunctions attempts to modify the class object
c)Getters and setters are only available in the function where the object is defined
d)It will change to whatever the function changed it to
5g)Define a pointer variable named daco that can be used for objects of the class Banana.
a)new int daco;
b)new Banana daco;
c)Banana * daco;
d)new Banana * daco;
5h)Dynamically allocate an object of Banana, using the pointer variable daco.
a)Banana * = new daco;
b)daco = new * Banana;
c)daco = new & Banana;
d)daco = new Banana;
5i)Code a call to function anotherNonclassFunction passing this new object.
a)anotherNonclassFunction (new daco);
b)anotherNonclassFunction (daco);
c)anotherNonclassFunction (Banana daco);
d)anotherNonclassFunction (Banana * daco);
5j)Code the function definition for anotherNonclassFunction, picking up this new object. anotherNonclassFunction has no return value.
a)void anotherNonclassFunction (Banana * p);
b)void anotherNonclassFunction (Banana co);
c)void anotherNonclassFunction (Banana & co);
d)void anotherNonclassFunction (daco *);
5k)Back in the calling program, how do you free up the memory allocated for this new Banana object?
a)delete p;
b)delete Banana;
c)delete daco;
d)delete daco Banana;
5l)Recall that a moment ago, you used daco to allocate a new Banana object. Now this code is executed:
daco = new Banana;
What problem does this cause?
a)None.
b)The address of the old Banana is overwritten by the new address, so the old Banana cannot be deleted.
c)daco is already in use, so an addressing exception will be thrown.
d)We now have two different Bananas with the same address.
5m)Assume we've recorded the class definition for class Banana slightly:
{
public:
string s;
int y;
bool theMemberFunc (string);
void setterForS(string); // setter for s
string getterForS(); // getter for s
};
Now we're in main and we create a new Banana object:
daco = new Banana;
How can we access members? There may be more than one correct answer
a)We can use the getter for s.
b)We can code s = something
c)We can access by coding daco.s = something
d)We can access by coding daco -> s = something
5a) d)Banana co;
5b) a)aNonclassFunction (co);
function call uses object name
5c) b)void aNonclassFunction (Banana co);
function definition use parameter type also
5d) a)Nothing.
The change is local inside the function
5e) b)void aNonclassFunction (Banana &co);
Reference variable is used.
5f) d)It will change to whatever the function changed it to
5g) c)Banana * daco;
5h) d)daco = new Banana;
5i) b)anotherNonclassFunction (daco);
function is called using pointer
5j) a)void anotherNonclassFunction (Banana * p);
Function parameter type is pointer to class Banana
5k) c)delete daco;
delete ptr; is used
5l) c)daco is already in use, so an addressing exception will be thrown.
5m) d)We can access by coding daco -> s = something
pointers use -> to access members
Do ask if any doubt. Please upvote.
Get Answers For Free
Most questions answered within 1 hours.