CISC 192 - MyProgrammingLab - Chapter 14

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write a literal corresponding to the floating point value one-and-a-half.

1.5

Assume the existence of a class GraphicProgram with a string member,executableName, and data member, windowPtr, of type Window * (where Window is also a class type). Assume further that the Window class has a function called clone, that takes no parameters and returns a pointer to a new copy of the Window. Write a copy constructor for the GraphicProgram class that uses the clone function to make a proper copy of the Window member (rather than simple member assignment). The executable member may be copied using simple member assignment.

GraphicProgram(const GraphicProgram &a){ executableName=a.executableName; windowPtr=a.windowPtr->clone(); }

Write the definition of a function isMultipleOf, that receives two integer arguments and returns true if the first argument is a multiple of the second. Otherwise false is returned. So, if the arguments are 27 and 8 the function returns false because 27 is not a multiple of 8. But if the arguments are 34 and 17 the function returns true because 34 is a multiple of 17 (2*17 is 34).

bool isMultipleOf(int x, int y) { return x%y==0; }

Assume the existence of a File class with boolean data member, isValid, indicating the file is ready for I/O. Write a unary operator, !, that accepts a constant File reference, and returns true if the file is not ready for I/O and false otherwise.

bool operator!() { return !isValid; }

Write a full class definition for a class named Counter, and containing the following members: A data member counter of type int. A data member named limit of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes two int arguments and assigns the first one to counter and the second one to limit. It also adds one to the static variable nCounters A member function called increment that accepts no parameters and returns no value. If the data member counter is less than limit, increment just adds one to the instance variable counter. A member function called decrement that accepts no parameters and returns no value. If counter is greater than zero, decrement subtracts one from the counter. A member function called getValue that accepts no parameters. It returns the value of the instance variable counter. A static function named getNCounters that accepts no parameters and returns an int. getNCounters returns the value of the static variable nCounters.

class Counter { public: int counter; int limit; static int nCounters; Counter(int c, int l) { counter = c; limit = l; nCounters++; } void increment() { if (counter < limit) counter++; } void decrement() { if (counter > 0) counter--; } int getValue() { return counter; } static int getNCounters() { return nCounters; } }; int Counter::nCounters = 0;

Define the following Window class: - integer data members, width and height - a constructor that accepts two integer parameters (width followed by height) and uses them to initialize the data members - a friend function, areSameSize, that accepts two Window objects and returns a boolean indicating if they are the same size. Two windows are the same size if the widths and heights match.

class Window { private: int width, height; public: Window(int w,int h) { width = w; height = h; } friend bool areSameSize(Window a, Window b) { if ((a.height == b.height) && (a.width == b.width)) return true; else return false; } };

Assume the existence of a Window class with integer data members width and height. Overload the << operator for the Window class-- i.e., write a nonmember ostream-returning function that accepts a reference to an ostream object and a constant reference to a Window object and sends the following to the ostream: 'a (width x height) window' (without the quotes and with width and height replaced by the actual width and height of the window. Thus for example, if the window had width=80 and height=20, << would send 'a (80 x 20) window' to the ostream object.) Don't forget to have the function return the proper value as well. Assume the operator has been declared a friend in the Window class.

friend ostream& operator << (ostream& stm, Window& w){ return stm<<"a ("<<w.width<<" x "<<w.height<<") window"; }

Assume the existence of a Window class with integer data members width and height. Overload the >> operator for the Window class-- i.e., write a nonmember istream-returning function that accepts a reference to an istream object and a reference to a Window object and reads the next two values from the istream in to the width and height members respectively. Don't forget to have the function return the proper value as well. Assume the operator has been declared a friend in the Window class.

friend std::istream& operator>>(std::istream& input, Window& w) { input >> w.width >> w.height; return input; }

Write a full class definition for a class named Counter, and containing the following members: A data member counter of type int. A data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A member function called increment that accepts no parameters and returns no value. increment adds one to the instance variable counter. A member function called decrement that accepts no parameters and returns no value. decrement subtracts one from the counter. A member function called getValue that accepts no parameters. It returns the value of the instance variable counter. A member function named getCounterID that accepts no parameters and returns an int. getCounterID returns the value of the data member counterID.

public class Counter { private int counter; private int counterID; private static int nCounters=0; public Counter(int c) { counter=c; nCounters++; counterID=nCounters; } public void increment() { counter++; } public void decrement() { counter--; } public int getValue() { return counter; } public int getCounterID() { return counterID; } }

Write a fragment of code that reads a line of text (using getline) from standard input consisting of a last name followed by a first name (separated by exactly one blank), and prints out the first initial followed by a period then a blank then the full last name. Declare any necessary variables.(string processing)

string firstName, lastName, Name; int idx; getline(cin,Name); idx=Name.find(' ',0); lastName=Name.substr(0,idx); firstName=Name.substr(idx+1,Name.length()-idx); cout<<firstName[0]<<". "<<lastName<<endl;

Write a function, sort, that accepts an array of integers, and the number of elements in the array and sorts the array in ascending order (basic sort)

void sort(int array[],int size){ int hold; int i; for ( int pass = 0; (pass< size-1) ; pass++) for ( i = 0; i<size-1; i++) if (array[i] > array[i+1]){ hold = array[i]; array[i] = array[i+1]; array[i+1] = hold; } }


Conjuntos de estudio relacionados

End of Life, Palliative Care, Spirituality, and Pain

View Set

Vascular Disorders Ch 37 Evolve, NTB

View Set