(1) |
_____
|
All functions/methods in C++ must return a value. |
(2) |
_____
|
>> is the operator in the statement
cin >> x;
|
(3) |
_____
|
#define is a function. |
(4) |
_____
|
C++ only uses "call by value". |
(5) |
_____
|
A class can have more than one constructor. |
Pass by value |
|
|
Garbage |
|
|
Copy constructor |
|
|
Pointer variable |
|
|
cin
|
|
|
Executable file |
|
|
Memory leak |
|
|
Local variable |
|
|
Side effect |
|
|
void |
|
|
(1) |
_____
|
Dynamic memory allocation is rarely used in practice. |
(2) |
_____
|
Uninitialized pointers should never be set
to NULL . |
(3) |
_____
|
The "memory heap" uses a queue to allocate memory. |
(4) |
_____
|
new is a binary operator. |
(5) |
_____
|
In C++, exceptions are handled in
do -handle statements. |
#define QUIET #include <iostream.h> void main() { std::cout << "Execution begins...\n"; #ifndef QUIET for (int i=0; i<5; i++) { std::cout << "Loop: " << i << "\n"; } #endif std::cout << "Execution ends.\n"; }
what will be printed when it is executed?
Window.h
:
class Window { public: int CLOSED; int OPEN; int status; }; int Window::CLOSED = 0; int Window::OPEN = 1;
will the following program compile, link and execute? If so, what will be printed? If not, why won't it compile and/or execute and how would you fix those problems?
#include <iostream> #include "Window.h" int main(void) { Window w; w.status = 0; if (w.status == Window::CLOSED) std::cout << "Closed\n"; else std::cout << "Open\n"; }
Rectangle.h
:
#ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { protected: double height, width; public: Rectangle(void); Rectangle(double w, double h); double area(void); double flange(void); virtual double perimeter(void); }; #endif
Rectangle.cpp
:
#include "Rectangle.h" Rectangle::Rectangle(void) { width = 0.0; height = 0.0; } Rectangle::Rectangle(double w, double h) { width = w; height = h; } double Rectangle::area(void) { return width*height; } double Rectangle::flange(void) { return 3.0; } double Rectangle::perimeter(void) { return 2.0*width + 2.0*height; }
Square.h
:
#ifndef SQUARE_H #define SQUARE_H #include "Rectangle.h" class Square: public Rectangle { public: Square(double w); double flange(void); double perimeter(void); }; #endif
and Square.cpp
:
#include "Square.h" Square::Square(double w) { width = w; height = w; } double Square::flange(void) { return 17.0; } double Square::perimeter(void) { return 10.0; }
whate will be printed by the following driver?
#include <iostream> #include "Rectangle.h" #include "Square.h" using namespace std; int main(void) { Square *s; Rectangle *q; Rectangle *r; r = new Rectangle(2.0, 6.0); s = new Square(4.0); q = new Square(5.0); cout << "r->area(): " << r->area() << "\n"; cout << "r->perimeter(): " << r->perimeter() << "\n"; cout << "r->flange(): " << r->flange() << "\n"; cout << "\n"; cout << "s->area(): " << s->area() << "\n"; cout << "s->perimeter(): " << s->perimeter() << "\n"; cout << "s->flange(): " << s->flange() << "\n"; cout << "\n"; cout << "q->area(): " << q->area() << "\n"; cout << "q->perimeter(): " << q->perimeter() << "\n"; cout << "q->flange(): " << q->flange() << "\n"; cout << "\n"; }
cout
statements
in the following program:
#include <iostream> using namespace std; int main(void) { int i; int j; int *p; int *q; i = 5; cout << "i: " << i << "\n"; // 1 cout << "&i: " << &i << "\n"; // 2 j = 9; cout << "j: " << j << "\n"; // 3 cout << "&j: " << &j << "\n"; // 4 p = &i; cout << "p: " << p << "\n"; // 5 cout << "*p: " << *p << "\n"; // 6 j = i; q = &j; *q = 7; cout << "i: " << i << "\n"; // 7 cout << "j: " << j << "\n"; // 8 cout << "q: " << q << "\n"; // 9 cout << "*q: " << *q << "\n"; // 10 *p = *q; j = 3; cout << "i: " << i << "\n"; // 11 cout << "j: " << j << "\n"; // 12 cout << "*p: " << *p << "\n"; // 13 }
is as follows:
i: 5 &i: 0x22ff74 j: 9 &j: 0x22ff70
what will be printed by the remaining cout
statements?
change
is implemented as follows:
void change(int *a, int length) { int i; for (i=1; i<length; i++) { a[i] += *(a+i-1); } }
What will be printed by the following code snippet (assuming that everything is compiled and linked properly)?
int i, n; int *value; n = 4; value = new int[n]; for (i=0; i<n; i++) value[i] = 100 * i; change(value, n); for (i=0; i<n; i++) cout << value[i] << "\n";
add()
function:
int add(int &a, int &b) { a = a + b; return a; }
what will be printed by each of the following (assuming each is executed independently)? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location). (Hint: Be careful!)
int sum, x, y; x = 5; y = 7; sum = add(x, y); cout << sum << " is the sum of " << x << " and " << y << "\n";
int sum, x, y; x = 5; y = 7; sum = add(x, y); cout << x << " + " << y << " = " << sum << "\n";
double total(int[] a, int n) { int sum; sum = 0; for (int i=0; i<n; i++) sum += a[i]; return sum; }
total()
function:
int total(int *a, int n) { int sum; sum = 0; for (int i=0; i<n; i++) sum += a[i]; return sum; }
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4}; cout << total(x,4) << "\n";
total()
function:
int total(int *a, int n) { int sum; for (int i=0; i<n; i++) sum += a[i]; return sum; }
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4}; cout << total(x,4) << "\n";
total()
function:
int total(int *a, int n) { int sum; sum = 0; for (int i=0; i<n; i++) sum += *(a++); return sum; }
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4}; cout << total(x,4) << "\n";
total()
function:
int total(int *a, int n) { int sum; sum = 0; for (int i=0; i<n; i++) sum += *(a+i); return sum; }
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4}; cout << total(x,4) << "\n";
total()
function:
int total(int *a, int n) { int sum; sum = 0; for (int i=0; i<n; i++) sum += *(++a); return sum; }
what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).
int x[4] = {1,2,3,4}; cout << total(x,4) << "\n";
String.h
).
/** * Returns a substring of the given "string". * The correct amount of memory required * by the substring is allocated from the heap. * * @param s A pointer to the string * @param start Index of the first char in the substring * @param length Length of the substring */ char *substring(const char *s, int start, int length) { }
Car
class that has a default
constructor and a void
method named start()
.
Car
object named chevy
and constructs chevy
using memory from the stack.
Car
object named ford
and constructs ford
using memory from the heap (also known as the free store).
chevy
object's start()
method.
ford
object's start()
method.
Fraction
class in C++ that includes the ability
to multiply two Fraction
objects and the ability to
multiply a Fraction
by an int
. You may use
either methods or operator overloading.
BinaryMultiplicationExpression
template that
can be used with both an int
and a Fraction
.
It must have a constructor that is passed the two operands (of the same
type) and an evaluate()
method that returns the result
of the multiplcation.
Pong.h
:
#ifndef PONG_H #define PONG_H class Pong { private: int spaces; protected: int current; int direction; public: /** * Explicit Value Constructor * * @param width The width of the field */ Pong(int width); /** * Get the char that should be used to represent the ball. * * In Pong, the ball is always represented by a 'o'. */ virtual char getBall(void); /** * Move the ball to the next position (based on the direction) * * This method ensures that the ball does not leave the field * (i.e., it ensures that current is never greater than spaces * and never less than 0). */ void next(void); /** * Print the field (including the walls and the ball) */ void print(void); }; #endif
and the following partial implementation in a file
named Pong.cpp
,
complete the next()
. Your answer must be
consistent with the comments describing the methods.
#include "Pong.h" #include <iostream> using namespace std; Pong::Pong(int width) { spaces = width-1; // Total number of spaces direction = 1; // Current direction (Right:1, Left: -1) current = -1; // Current position of the ball } char Pong::getBall() { return 'o'; } void Pong::next(void) { } void Pong::print() { char ball; ball = getBall(); cout << "|"; // Left wall for (int i=0; i<current; i++) cout << " "; // Spaces to the left cout << ball; // The ball for (int i=current+1; i<=spaces; i++) cout << " "; // Spaces to the right cout << "|"; // Right wall cout << "\n"; }
When used with the following driver:
#include "Pong.h" int main(void) { Pong game = Pong(5); for (int i=0; i<21; i++) { game.next(); game.print(); } }
the following output should be generated:
|o | | o | | o | | o | | o| | o | | o | | o | |o | | o | | o | | o | | o| | o | | o | | o | |o | | o | | o | | o | | o|
Tron.h
:
#ifndef TRON_H #define TRON_H #include "Pong.h" class Tron: public Pong { private: int sDirection; public: /** * Explicit Value Constructor * * @param width The width of the field * @param starDirection The direction in which the ball should be a '*' */ Tron(int width, int starDirection); /** * Get the char that should be used to represent the ball. * * In Tron, the ball is represented by a '*' if the current * direction is equal to the "star direction". Otherwise, * the ball from Pong is used. */ char getBall(void); }; #endif
fully implement the Tron
class (in other words,
create a complete Tron.cpp
"file").
You may not change the visibility/accessibility of any attributes
or methods in the Tron
class, nor may
you add attributes or methods to that class. However, you may
make methods in that class virtual
if necessary.
When used with the following driver:
#include "Tron.h" int main(void) { Tron *game = new Tron(5,-1); for (int i=0; i<21; i++) { game->next(); game->print(); } }
the following output should be generated:
|o | | o | | o | | o | | *| | * | | * | | * | |o | | o | | o | | o | | *| | * | | * | | * | |o | | o | | o | | o | | *|
Copyright 2020