CISC 192 - MyProgrammingLab - Chapter 13

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Declare a short variable named patientsAge.

short patientsAge;

Assume the existence of a class named Window with functions named close and freeResources, both of which accept no parameters and return no value. Write a destructor for the class that invokes close followed by freeResources.

~Window() {close();freeResources();};

Write the implementation (.cpp file) of the Acc2 class of the previous exercise. The full specification of the class is: An data member named sum of type integer. A constructor that accepts no parameters. THe constructor initializes the data member sum to 0. A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum.

Acc2::Acc2(){ sum=0;} int Acc2::getSum(){return sum;}

Objects of the BankAccount class require a name (string) and a social security number (string) be specified (in that order)upon creation. Define an object named account, of type BankAccount, using the values "John Smith" and "123-45-6789" as the name and social security number respectively.

BankAccount account ("John Smith", "123-45-6789");

Objects of the Calculator class require no additional information when created. Define an object named calc, of type Calculator.

Calculator calc;

Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named limit of type int. A constructor that takes two int arguments and assigns the first one to counter and the second one to limit A 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 function called decrement that accepts no parameters and returns no value. If counter is greater than zero, decrement subtracts one from the counter. A function called getValue that accepts no parameters. It returns the value of the instance variable counter.

Counter::Counter(int theCounter, int theLimit) { counter = theCounter; limit=theLimit; } void Counter::increment() { if (counter<limit) counter++; } void Counter::decrement() { if (counter>0) counter--; } int Counter::getValue() { return counter; }

Creating objects of the Currency class require a name (string), a currency symbol (string) and the number of decimal places (integer) usually associated with the currency (in that order). Creating objects of the Money class require a Currency object, and the actual amount (integer) (in that order). Define an object of type Currency named canadianDollars and corresponding to the name "Canadian Dollar", the symbol "C$", and 2 decimal places. Define an object of type Money named valueInCanadians corresponding to 230 units of canadianDollars.

Currency canadianDollars ("Canadian Dollar", "C$", 2); Money valueInCanadians (canadianDollars,230);

Creating objects of the Currency class requires a name (string), a currency symbol (string) and the number of decimal places (integer) usually associated with the currency (in that order). Define an object named curr, of type Currency corresponding to "US Dollar" with the symbol "$" and 2 decimal places.

Currency curr ("US Dollar", "$", 2);

Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type double. A constructor that no parameters. The constructor initializes the data member amount to 0. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

GasTank::GasTank() :amount(0) { } void GasTank::addGas(double b) { amount +=b; } void GasTank::useGas (double c) { amount-=c; if ( amount<0) amount=0; } bool GasTank::isEmpty() { if(amount<0.1) return true; else return false; } double GasTank::getGasLevel() { return amount; }

Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type double. A constructor that no parameters. The constructor initializes the data member amount to 0. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

GasTank::GasTank() :amount(0) { } void GasTank::addGas(double b) { amount +=b; } void GasTank::useGas (double c) { amount-=c; if ( amount<0) amount=0; } double GasTank::getGasLevel() { return amount; }

Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type double. An data member named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. The constructor will also set amount to zero. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity, amount is set to capacity. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns true if the value of amount is less than 0.1, and false otherwise. A function named isFull that accepts no parameters and returns a boolean value.. isFull returns true if the value of amount is greater than capacity-0.1, and false otherwise. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

GasTank::GasTank(double a) { capacity=a; amount=0; } void GasTank::addGas(double b) { amount +=b; if( amount >capacity) amount=capacity; } void GasTank::useGas (double c) { amount-=c; if ( amount<0) amount=0; } bool GasTank::isEmpty() { if(amount<0.1) return true; else return false; } bool GasTank::isFull() { if(amount>(capacity-0.1)) return true; else return false; } double GasTank::getGasLevel() { return amount; }

Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is: A data member named amount of type double. An data member named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. The constructor also sets amount to zero. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity, amount is set to capacity. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns true if the value of amount is less than 0.1, and false otherwise. A function named isFull that accepts no parameters and returns a boolean value.. isFull returns true if the value of amount is greater than capacity-0.1, and false otherwise. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member. A function named fillUp that accepts no parameters and returns a double. fillUp increases amount to capacity and returns the difference between the value of capacity and the original value of amount (that is, the amount of gas that is needed to fill the tank to capacity).

GasTank::GasTank(double a) { capacity=a; amount=0; } void GasTank::addGas(double b) { amount +=b; if( amount >capacity) amount=capacity; } void GasTank::useGas (double c) { amount-=c; if ( amount<0) amount=0; } bool GasTank::isEmpty() { if(amount<0.1) return true; else return false; } bool GasTank::isFull() { if(amount>(capacity-0.1)) return true; else return false; } double GasTank::getGasLevel() { return amount; } double GasTank::fillUp() { double temp = amount; amount=capacity; return capacity- temp; }

Objects of class Name are defined by supplying last name (string) and a first name (string)-- in that order. Define an object of type Name named name and corresponding to a last name of "Black" and a first name of "Susan".

Name name("Black","Susan");

Assume a class Window with a constructor that accepts two integer arguments: width and height (in that order). Declare an array named winarr, consisting of 3 Window objects, where the first element is an 80x20 window, the second a 10x10 window, and the third a 133x40 window.

Window winarr[] ={Window(80,20), Window(10,10), Window(133,40)};

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Define an object named window, of type Window, corresponding to a 80 x 20 window.

Window window (80,20);

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Define an object named window, of type Window, whose width and length are obtained from the (already declared and initialized) variables, w and l.

Window window (w,l);

Given a class Window, with integer data members width, height, xPos, and yPos, write the following two constructors: - a constructor accepting 4 integer arguments: width, height, horizontal position, and vertical position (in that order), that are used to initialize the corresponding members. - a constructor accepting 2 integer arguments: width and height (in that order), that are used to initialize the corresponding members. The xPos and yPos members should be initialized to 0.

Window(int w,int h,int horiz, int vertical) { width=w; height=h; xPos=horiz; yPos=vertical; } Window(int w,int h) { width=w; height=h; }

Write a full class definition for a class named Acc1, containing no constructors, functions, or data members (i.e., a totally empty class). (Note, the last character of the classname is "one" not "ell".)

class Acc1{ };

Write the interface (.h file) of a class Acc1 containing containing no constructors, functions, or instance variables. (Note, the last character of the classname is "one" not "ell".)

class Acc1{ };

Write a full class definition for a class named Acc2, and containing the following members: A data member named sum of type integer. A constructor that accepts no parameters. THe constructor initializes the data member sum to 0. A member function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum.

class Acc2 { private: int sum; public: Acc2() {sum=0;} int getSum() {return sum;} };

Write the interface (.h file) of a class Acc2 containing: An data member named sum of type integer. A constructor accepting no parameters. A function named getSum that accepts no parameters and returns an integer.

class Acc2 { private: int sum; public: Acc2(); int getSum(); };

Write the interface (.h file) of a class Accumulator containing: A data member named sum of type integer. A constructor accepting no parameters. A function named getSum that accepts no parameters and returns an integer. A function named add that accepts an integer parameter and returns no value.

class Accumulator { private: int sum; public: Accumulator(); int getSum(); void add(int); };

Write a full class definition for a class named Accumulator, and containing the following members: A data member named sum of type integer. A constructor that accepts no parameters. The constructor initializes the data member sum to 0. A member function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum. A member function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter.

class Accumulator { private: int sum; public: Accumulator(){sum=0;} int getSum(){return sum;} void add (int x) {sum+=x;} };

Write the interface (.h file) of a class Accumulator containing: A data member named sum of type integer. A constructor accepting an integer parameter. A function named getSum that accepts no parameters and returns an integer. A function named add that accepts an integer parameter and returns no value.

class Accumulator { private: int sum; public: Accumulator(int); int getSum(); void add(int); };

Write the interface (.h file) of a class Averager containing: A data member named sum of type integer. A data member named count of type integer. A constructor accepting no parameters. A function named getSum that accepts no parameters and returns an integer. A function named add that accepts an integer parameter and returns no value. A function named getCount that accepts no parameters and returns an integer. A function named getAverage that accepts no parameters and returns a double.

class Averager { private: int sum; int count; public: Averager(); int getSum(); void add(int); int getCount(); double getAverage(); };

Write a full class definition for a class named Counter, and containing the following members: A data member counter of type int. A constructor that takes one int argument and assigns its value to counter A function called increment that accepts no parameters and returns no value. increment adds one to the counter data member. A function called decrement that accepts no parameters and returns no value. decrement subtracts one from the counter data member. A function called getValue that accepts no parameters. It returns the value of the instance variable counter.

class Counter { private: int counter; public: Counter(int a) { counter=a; } void increment(); void decrement(); int getValue(); }; void Counter::increment() { counter++; } void Counter::decrement() { counter--; } int Counter::getValue() { return counter; }

Write a full class definition for a class named Averager, and containing the following members: An data member named sum of type integer. An data member named count of type integer. A constructor with no parameters. The constructor initializes the data members sum and the data member count to 0. A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum. A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter, and increments the value of count by one. A function named getCount that accepts no parameters and returns an integer. getCount returns the value of the count data member, that is, the number of values added to sum. A function named getAverage that accepts no parameters and returns a double. getAverage returns the average of the values added to sum. The value returned should be a value of type double (and therefore you must cast the data members to double prior to performing the division).

class Averager{ int sum, count; public: Averager(){ sum=count=0;} int getSum(){ return sum;} void add(int x){ sum+=x; count++;} int getCount(){ return count;} double getAverage(){ return (double)sum/count;} };

Write a full class definition for a class named ContestResult, and containing the following members: A data member winner of type string, initialized to the empty string. A data member secondPlace of type string, initialized to the empty string. A data member thirdPlace of type string, initialized to the empty string. A member function called setWinner that has one parameter, whose value it assigns to the data member winner. A member function called setSecondPlace that has one parameter, whose value it assigns to the data member secondPlace. A member function called setThirdPlace that has one parameter, whose value it assigns to the data member thirdPlace. A member function called getWinner that has no parameters and that returns the value of the data member winner. A member function called getSecondPlace that has no parameters and that returns the value of the data member secondPlace. A member function called getThirdPlace that has no parameters and that returns the value of the data member thirdPlace.

class ContestResult { private: string winner=""; string secondPlace=""; string thirdPlace=""; public: void setWinner(string a){ winner=a;} void setSecondPlace(string b){secondPlace=b;} void setThirdPlace(string c){thirdPlace=c;} string getWinner() {return winner;} string getSecondPlace() {return secondPlace;} string getThirdPlace() {return thirdPlace;} };

Write the definition of a class ContestResult containing: An data member winner of type string, initialized to the empty string. An data member secondPlace of type string, initialized to the empty string. An data member thirdPlace of type string, initialized to the empty string. A member function called setWinner that has one parameter, whose value it assigns to the data member winner. A member function called setSecondPlace that has one parameter, whose value it assigns to the data member secondPlace. A member function called setThirdPlace that has one parameter, whose value it assigns to the data member thirdPlace. A member function called getWinner that has no parameters and that returns the value of the data member winner. A member function called getSecondPlace that has no parameters and that returns the value of the data member secondPlace. A member function called getThirdPlace that has no parameters and that returns the value of the data member thirdPlace.

class ContestResult { public: void setWinner(string winner); void setSecondPlace(string secondPlace); void setThirdPlace(string thirdPlace); string getWinner(); string getSecondPlace(); string getThirdPlace(); private: string winner; string secondPlace; string thirdPlace; };

Write the interface (.h file) of a class Counter containing: A data member counter of type int. A data member named counterID of type int. A static int data member named nCounters. A constructor that takes an int argument. A function called increment that accepts no parameters and returns no value. A function called decrement that accepts no parameters and returns no value. A function called getValue that accepts no parameters. A function named getCounterID that accepts no parameters and returns an int.

class Counter { private: int counter; int counterID; static int nCounters; public: Counter(int); void increment(); void decrement(); int getValue(); int getCounterID(); };

Write the interface (.h file) of a class Counter containing: * A data member counter of type int. * A constructor that takes one int argument. * A function called increment that accepts no parameters and returns no value. * A function called decrement that accepts no parameters and returns no value. * A function called getValue that accepts no parameters. ------- ignore: Write the interface (.h file) of a class Counter containing: A data member counter of type int. A constructor that takes one int argument. A function called increment that accepts no parameters and returns no value. A function called decrement that accepts no parameters and returns no value. A function called getValue that accepts no parameters and returns an int.

class Counter { private: int counter; public: Counter(int a); void increment(); void decrement(); int getValue(); };

Write the interface (.h file) of a class Counter containing: A data member counter of type int. A data member named limit of type int. A static int data member named nCounters. A constructor that takes two int arguments. A function called increment that accepts no parameters and returns no value. A function called decrement that accepts no parameters and returns no value. A function called getValue that accepts no parameters and returns an int. A static function named getNCounters that accepts no parameters and returns an int.

class Counter { private: int counter; int limit; static int nCounters; public: Counter(int, int); void increment(); void decrement(); int getValue(); static int getNCounters(); };

Write the interface (.h file) of a class Counter containing: A data member counter of type int. A data member named limit of type int. A constructor that takes two int arguments. A function called increment that accepts no parameters and returns no value. A function called decrement that accepts no parameters and returns no value. A function called getValue that accepts no parameters and returns an int.

class Counter { private: int counter; int limit; static int nCounters; public: Counter(int, int); void increment(); void decrement(); int getValue(); static int getNCounters(); };

Write the interface (.h file) of a class Counter containing: * A data member counter of type int. * A constructor that accepts no arguments. * A function called increment that accepts no parameters and returns no value. * A function called getValue that accepts no parameters. IGNORE THE FOLLOWING: Write the interface (.h file) of a class Counter containing: A data member counter of type int. A constructor that accepts no arguments. A function called increment that accepts no parameters and returns no value. A function called getValue that accepts no parameters.

class Counter { private: int counter; public: Counter(); void increment(); int getValue(); };

Write the interface (.h file) of a class Counter containing: An instance variable counter of type int, initialized to 0. A function called increment that adds one to the instance variable counter. It does not accept parameters or return a value. A function called getValue that doesn't accept any parameters. It returns the value of the instance variable counter. A default constructor.

class Counter { private: int counter; public: void increment(); int getValue(); Counter(); };

Write a full class definition for a class named Counter, and containing the following members: A data member counter of type int. A constructor that accepts no arguments and initializes the counter data member to 0. A function called increment that accepts no parameters and returns no value. increment adds one to the counter data member. A function called getValue that accepts no parameters. It returns the value of the instance variable counter.

class Counter{ private: int counter = 0; public: void increment(){ counter++; } public: int getValue(){ return counter; } };

Write the interface (.h file) of a class GasTank containing: An data member named amount of type double. A constructor that accepts no parameters. A function named addGas that accepts a parameter of type double and returns no value. A function named useGas that accepts a parameter of type double and returns no value. A function named isEmpty that accepts no parameters and returns a boolean value. A function named getGasLevel that accepts no parameters and returns double.

class GasTank { private: double amount; public: GasTank(); void addGas(double); void useGas(double); bool isEmpty(); double getGasLevel(); };

Write the interface (.h file) of a class GasTank containing: An data member named amount of type double. A constructor that accepts no parameters. A function named addGas that accepts a parameter of type double and returns no value. A function named useGas that accepts a parameter of type double and returns no value. A function named getGasLevel that accepts no parameters and returns double.

class GasTank { private: double amount; public: GasTank(); void addGas(double); void useGas(double); double getGasLevel(); };

Write the interface (.h file) of a class GasTank containing: A data member named amount of type double. A data member named capacity of type double. A constructor that accepts a parameter of type double. A function named addGas that accepts a parameter of type double and returns no value. A function named useGas that accepts a parameter of type double and returns no value. A function named isEmpty that accepts no parameters and returns a boolean value. A function named isFull that accepts no parameters and returns a boolean value. A function named getGasLevel that accepts no parameters and returns a double. A function named fillUp that accepts no parameters and returns a double.

class GasTank { private: double amount; double capacity; public: GasTank(double); void addGas(double); void useGas(double); bool isEmpty(); bool isFull(); double getGasLevel(); double fillUp(); };

Write the interface (.h file) of a class GasTank containing: An data member named amount of type double. An data member named capacity of type double. A constructor that accepts a parameters of type double. A function named addGas that accepts a parameter of type double and returns no value. A function named useGas that accepts a parameter of type double and returns no value. A function named isEmpty that accepts no parameters and returns a boolean value. A function named isFull that accepts no parameters and returns a boolean value. A function named getGasLevel that accepts no parameters and returns double.

class GasTank { private: double amount; double capacity; public: GasTank(double); void addGas(double); void useGas(double); bool isEmpty(); bool isFull(); double getGasLevel(); };

Write a full class definition for a class named GasTank, and containing the following members: A data member named amount of type double. A constructor that accepts no parameters. The constructor initializes the data member amount to 0. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

class GasTank { private: double amount; public: GasTank(){amount=0;} void addGas(double a){amount+=a;} void useGas(double b){ if (amount<=0) amount=0; else amount-=b;} bool isEmpty(){if (amount<0.1) return true; else return false;} double getGasLevel(){return amount;} };

Write a full class definition for a class named GasTank, and containing the following members: A data member named amount of type double. A constructor that accepts no parameters. The constructor initializes the data member amount to 0. A function named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. A function named useGas that accepts a parameter of type double. The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0. A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise. A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

class GasTank { private: double amount; public: GasTank(){amount=0;} void addGas(double a){amount+=a;} void useGas(double b){ if (amount<=0) amount=0; else amount-=b;} bool isEmpty(){if (amount<0.1) return true; else return false;} double getGasLevel(){return amount;} };

Write the interface (.h file) of a class Player containing: A data member name of type string. A data member score of type int. A member function called setName that accepts a parameter and assigns it to name. The function returns no value. A member function called setScore that accepts a parameter and assigns it to score. The function returns no value. A member function called getName that accepts no parameters and returns the value of name. A member function called getScore that accepts no parameters and returns the value of score.

class Player { private: string name; int score; public: void setName(string); void setScore(int); string getName(); int getScore(); };

Write a full class definition for a class named Player, and containing the following members: A data member name of type string. A data member score of type int. A member function called setName that accepts a parameter and assigns it to name. The function returns no value. A member function called setScore that accepts a parameter and assigns it to score. The function returns no value. A member function called getName that accepts no parameters and returns the value of name. A member function called getScore that accepts no parameters and returns the value of score.

class Player{ public: void setName (string a){ name = a;} void setScore (int b){ score = b;} string getName(){ return name;} int getScore(){ return score;} private: string name; int score; };

Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An 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 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 function called decrement that accepts no parameters and returns no value. If counter is greater than zero, decrement subtracts one from the counter. A 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 return an int. getNCounters returns the value of the static variable nCounters.

int Counter::nCounters = 0; Counter::Counter(int theCounter, int theLimit) { nCounters++; counter = theCounter; limit=theLimit; } void Counter::increment() { if (counter<limit) counter++; } void Counter::decrement() { if (counter>0) counter--; } int Counter::getValue() { return counter; } int Counter::getNCounters() { return nCounters; }

Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An 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 function called increment that accepts no parameters and returns no value. increment adds one to the instance variable counter. A function called decrement that accepts no parameters and returns no value. decrement subtracts one from the counter. A function called getValue that accepts no parameters. It returns the value of the instance variable counter. A function named getCounterID that accepts no parameters and returns an int. getCounterID returns the value of the data member counterID.

int Counter::nCounters=0; Counter::Counter(int value) { counter = value; nCounters=nCounters+1; counterID = nCounters; } void Counter::increment() { counter++; } void Counter::decrement() { counter--; } int Counter::getValue() { return counter; } int Counter::getCounterID() { return counterID; }

Write the implementation (.cpp file) of the Counter class from the previous exercise. Here is the full specification of the class: The class contains: * A data member counter of type int. * A constructor that accepts no arguments and initializes the counter data member to 0. * A function called increment that accepts no parameters and returns no value. increment adds one to the counter data member. * A function called getValue that accepts no parameters. It returns the value of the instance variable counter.

int counter; Counter::Counter() { counter=0; } void Counter::increment() { counter++; } int Counter::getValue() { return counter; }

Write the implementation (.cpp file) of the Counter class from the previous exercise. Here is the full specification of the class: A data member counter of type int. A constructor that takes one int argument and assigns its value to counter A function called increment that accepts no parameters and returns no value. increment adds one to the counter data member. A function called decrement that accepts no parameters and returns no value. decrement subtracts one to the counter data member. A function called getValue that accepts no parameters. It returns the value of the instance variable counter.

int counter; Counter::Counter(int c) { counter=c; } void Counter::increment() { counter++; } void Counter::decrement() { counter--; } int Counter::getValue() { return counter; }

Write a fragment of code that reads in a header value from the standard input and then reads in that many integers (also from standard input) and prints their sum to standard output (for loops, header values, accumulators, basic arithmetic). Declare any variables you use.

int header = 0; int sum = 0; int input; cin >> header; for (int i = 0; i < header; i++){ cin >> input; sum += input; } cout << sum;

Assume a class Window with accessor method getWidth that accepts no parameters and returns an integer. Assume further an array of 3 Window elements named winarr, has been declared and initialized. Write a sequence of statements that prints out the width of the widest window in the array.

int max_width = winarr[0].getWidth(); if (winarr[0].getWidth()==winarr[1].getWidth() && winarr[1].getWidth()==winarr[2].getWidth()) cout<<"0"; if (max_width < winarr[1].getWidth()) cout<<winarr[1].getWidth(); if (max_width < winarr[2].getWidth()) cout<<winarr[2].getWidth(); if (max_width>winarr[1].getWidth() && max_width>winarr[2].getWidth()) cout<< winarr[0].getWidth();

Write the implementation (.cpp file) of the Accumulator class of the previous exercise. The full specification of the class is: An data member named sum of type integer. A constructor that accepts no parameters. The constructor initializes the data member sum to 0. A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum. A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter.

int sum; Accumulator::Accumulator() { sum=0; } int Accumulator::getSum() { return sum; } void Accumulator::add(int b) { sum+=b; }

Write the implementation (.cpp file) of the Accumulator class of the previous exercise. The full specification of the class is: An data member named sum of type integer. A constructor that accepts an integer parameter. THe constructor initializes the data member sum to the value of the parameter. A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum. A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter.

int sum; Accumulator::Accumulator(int a) { sum=a; } int Accumulator::getSum() { return sum; } void Accumulator::add(int b) { sum+=b; }

Write the implementation (.cpp file) of the Averager class of the previous exercise. The full specification of the class is: An data member named sum of type integer. An data member named count of type integer. A constructor with no parameters. The constructor initializes the data members sum and the data member count to 0. A function named getSum that accepts no parameters and returns an integer. getSum returns the value of sum. A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of the parameter, and increments the value of count by one. A function named getCount that accepts no parameters and returns an integer. getCount returns the value of the count data member, that is, the number of values added to sum. A function named getAverage that accepts no parameters and returns a double. getAverage returns the average of the values added to sum. The value returned should be a value of type double (and therefore you must cast the data members to double prior to performing the division).

int sum; int count; Averager::Averager() { sum=0; count=0; } int Averager::getSum() {return sum;} void Averager::add(int a) { sum+=a; count++; } int Averager::getCount() { return count; } double Averager::getAverage() { return (double)sum/(double)count; }

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Declare two integers corresponding to a width and a length and read values into them from standard input (in that order). Use these value to define an object of type Window named newWindow.

int width, length; cin >> width >> length; Window newWindow (width, length);

Declare a long integer variable named grossNationalProduct.

long grossNationalProduct;

The pay of an hourly worker is calculated by multiplying the hours worked by the hourly rate—up to 40 hours; any hours worked beyond 40 are paid at time-and-a-half (one and a half times the hourly rate). Given the variables hours, rate, and pay have already been declared and assigned values, write a fragment of code (one or more statements) that assigns to pay the proper value, taking into account time-and-a-half. (basic arithmetic, basic conditional)

pay = ((hours > 40)?(40*rate + (hours-40)*1.5*rate):(hours*rate));

Objects of the BankAccount class require a name (string) and a social security number (string) be specified (in that order)upon creation. Declare two strings corresponding to a name and a social security number and read values into them from standard input (in that order). Use these values to define an object of type BankAccount named newAccount.

string name; string social; cin>>name>>social; BankAccount newAccount(name ,social);

Write a fragment of code that reads in strings from standard input, until end-of-file and prints to standard output the largest value. You may assume there is at least one value. (cascading/streaming logic, basic string processing)

string s, largest=""; while(cin>>s) { if(s>largest) {largest=s;} } cout<<largest;

Write the implementation (.cpp file) of the ContestResult class from the previous exercise. Again, the class contains: An instance variable winner of type string, initialized to the empty string. An instance variable secondPlace of type string, initialized to the empty string. An instance variable thirdPlace of type String, initialized to the empty string. A function called setWinner that has one parameter, whose value it assigns to the instance variable winner. A function called setSecondPlace that has one parameter, whose value it assigns to the instance variable secondPlace. A function called setThirdPlace that has one parameter, whose value it assigns to the instance variable thirdPlace. A function called getWinner that has no parameters and that returns the value of the instance variable winner. A function called getSecondPlace that has no parameters and that returns the value of the instance variable secondPlace. A function called getThirdPlace that has no parameters and that returns the value of the instance variable thirdPlace.

void ContestResult::setWinner (string theWinner) {winner = theWinner; } void ContestResult::setSecondPlace (string theSecondPlace) {secondPlace = theSecondPlace; } void ContestResult::setThirdPlace (string theThirdPlace) {thirdPlace = theThirdPlace; } string ContestResult::getWinner () {return winner; } string ContestResult::getSecondPlace() {return secondPlace; } string ContestResult::getThirdPlace() {return thirdPlace; }

Write the implementation (.cpp file) of the Player class from the previous exercise. Again, the class contains: A data member name of type string. A data member score of type int. A member function called setName that accepts a parameter and assigns it to name. The function returns no value. A member function called setScore that accepts a parameter and assigns it to score. The function returns no value. A member function called getName that accepts no parameters and returns the value of name. A member function called getScore that accepts no parameters and returns the value of score.

void Player::setName (string playerName) { name = playerName; } void Player::setScore (int playerScore){ score = playerScore; } string Player::getName (){ return name; } int Player::getScore(){ return score; }


Set pelajaran terkait

Chapter 2: Trends in HRM, HRIR 3021 (UMN-Peter Ronza)

View Set

The Legislative Branch: The Senate (Article 1,Section 3)

View Set

CH.7- Structuring System Process Requirements

View Set

Week 2- The Immune System Pharmacology ATI Questions (Exam 1)

View Set

Videbeck Psychiatric Mental Health Nursing Chapter 5 Therapeutic Relationships NCLEX

View Set

Science Chapter 7.3 What are Viruses?

View Set