True or False? Line 2 is a legal initialization of the dp pointer.
(1) int i;
(2) double *dp = &i;
False. dp is given a type of double that cannot point to a variable of an integer type.
Which of the following function definitions of operator+ will allow for cascading in the String class?
a) void operator+(String & two);
b)String operator+(String & two);
c)bool operator+(String & two);
d) char[] operator+(String & two);
e) string operator+(String & two);
B.
True or False?
friend String::operator<<(...) is a correct implementation for a friend function.
False.
char next(0), junk(0);
while(inFile.eof() == false)
{
inFile.get(next);
if(next != ' '){
outFile << next << ",";
}
}
Does this segment of code successfully implement a loop to read from input and write to output?
Yes.
What two sections do you usually declare within a header file to denote the scope of variables and functions of a class?
Public and Private
A pointer is a variable that holds a:
a) int
b) char array
c) memory address
d) double
True or False?
Overloading an operator cannot change the precedence of that operation with respect to other operators.
True. The precedence of operators is predefined and immutable.
(True/False)
A friend function has access only to the public members and member functions of the class (and all objects of that class) of which it is a friend.
False.
Which is more efficient in use: Dynamically Allocated Arrays or Vectors?
Dynamically Allocated Arrays
Consider the following definitions:
const int x = 17;
class A {
public:
A( );
A( int n);
int f( ) const;
int g( const A& x);
private:
int i;
};
Each of the three const keywords is a promise to the compiler that the compiler will enforce. What is the promise in each case?
In const int x = 17; , the const keyword promises the compiler that code written by the author will not change the value of x .
In the int f( ) const ; declaration, the const keyword is a promise to the compiler that code written by the author to implement function f will not change anything in the calling object.
In int g(const A& x); , the const keyword is a promise to the compiler that code written by the class author will not change the argument plugged in for x .
What is the output of this piece of code if it compile succesfully?
int i(200), j(30);
int * iprt, jprt;
iprt = &i;
jprt = &j;
*jptr = 1000;
cout << *iprt << " " << *jptr << endl;
This code will not compile.
True or False?
Overloading a binary operator as a member requires two arguments.
False. It only requires one argument, the other argument is assumed to be the object you are operating on. (this->..., member variables of the object etc.)
Can you treat operators as friend functions or do they have to be only declared as member functions?
You treat them as either. However, the implementation may look different.
What function is used to open a file stream?
ex:
ifstream input;
input.?(filename);
input.open(filename);
What part of a class is necessary if a member variable of the class is of a pointer type?
Destructor
Which of the following are correct?
a) A constructor must be declared for each class.
b) A constructor must be declared with a return type.
c) A default constructor can only be provided by the compiler.
d) A class can have only one default constructor
e) A class can have only one constructor
D.
True or False?
An operator overloading is essentially a function that uses a different syntax for invocation.
True. Operator overloading is "syntactic sugar".
Describe friend functions and their purpose.
Friend functions are nonmember functions that have all the privileges of member functions. Generally, they allow for an extension of the knowledge base of the object (or primitive) whose function is being borrowed.
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it; std::cout << '\n';
Describe the output of this segment of code.
This code uses iterators to move through the vector and output the contents at each index.
What is special about static qualified variables?
They are shared across all instances of the objects used.
What will the following print out if it compiles successfully?
int a = 100, b = 200;
int *p = &a, *q = &b;
p = q;
*q = 999;
cout << *p << " " << *q;
999 999
class IntPair {
int first;
int second;
public: IntPair(int firstValue, int secondValue); const IntPair operator++( ); //Prefix version const IntPair operator++(int); //Postfix version int getFirst( ) const;
int getSecond( ) const;
};
Is the following legal? Why or why not?
IntPair a(1,2);
(a++)++;
Suppose you have a programmer-defined data type Data and want to overload the << operator to output your data type to the screen in the form cout << dataToPrint; and allow cascaded function calls. The first line of the function definition would be:
a) ostream & operator<<(ostream &output, const Data &dataToPrint)
b) ostream operator<<(ostream &output, const Data &dataToPrint)
c) ostream & operator<<(const Data &dataToPrint, ostream &output)
d) ostream operator<<(const Data &dataToPrint, ostream &output)
A.
What is the difference between the size and the capacity of a vector?
Capacity indicates the number of spaces allocated currently to the vector. While size is the number of elements it holds.
Name all the parts that describe a class in C++.
1. Constructors
2. Member Variables
3. Public and Private Sections
4. Member Functions