Question

C++ 5a)We have the following class: class Banana { private:      string s;      int y;...

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


Homework Answers

Answer #1

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.

Know the answer?
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
Similar Questions
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
C++ 1a) Assume that you have an integer variable named myint. Call xyzfunc and pass myint...
C++ 1a) Assume that you have an integer variable named myint. Call xyzfunc and pass myint as a parameter. a) xyzfunc(myint) b) myint(xyzfunc) c) xyzfunc(&myint) c) call xyzfunc(myint) 1b) Code the function definition for xyzfunc, picking up a local copy of myint. xyzfunc has no return value. a) void xyzfunc (int &myint); b) void xyzfunc (myint); c) void xyzfunc (local myint); d) void xyzfunc (int myint); 1c) If xyzfunc executes myint++, what happens to the copy of myint in the...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT