C++ Midterm Flashcards
State the following C/C++ expression below abbreviates:
a[8] ______________
- (a + 8)
Expand the Acronym RTTI
Run time Type Identification
Briefly describe ‘reflection’ in java
The ability to learn at runtime the class to which an object belongs, and properties of that class. Basic tools: getClass(), instanceOf
T/F java allows multiple inheritance
F
T/F java does not allow pointer arithmetic
T
C allows branching into the middle of a block but c++ does not
F
in c++ an int must contain precisely 32 bits in binary
F
C++ allows the statement 17;
T
in C++ a struct can contain both fields and member functions
T
Java and C++ have precisely three access modes
F
the object class is inherited by each java class
T
Assume a class declared as class stuff { string a,b;} provide a single constructor for stuff which works as follows: the constructor can be called with two, one or zero parameters.
public stuff(string c = "", string d = ""){ a = c; b = d; }
Assume a class declared as class stuff { string a,b;} provide a copy constructor for this class which does not result in 'sharing' of the string value for the two copies
stuff(const staff &s) { stuff temp = new staff(); temp a = s *a; temp b = s *b; return temp;}
Assume a class ‘rational’ for rational number. Give two prototypes for an override of +, one a friend function, and the other not. Indicate how each is called by giving example code snippets.
friend rational operator + (rational r1, rational r2)
rational operator + (rational r)
first called by roat = r1 + r2; [r + (r1,r2)]
2nd called by roat = r1+(r2) [or r1+r2]
Suppose we have, assuming a rational number class,
const rational half = new rational (1,2);
Given the half is constant, what kind of messages can be sent to half? Indicate the syntax of a valid method being use this way.
Message must be “constant member function”
foo(___) const{____}
Briefly discuss whether Java or C++ is better about allowing a user to write his own exception handlers for runtime exceptions
Java is better: user can control dozens of checked exception handlers using catch
Describe a specific runtime exception in c++ where the user can redo the default exception handler
heap underflow
In C using the static modifier on a global constant affects scope: Explain
Changes scope from program scope to file scope
In C++ the scope resolution operator can be used as a unary operator (one input) although it usually has two. What is the unary version good for?
;;a is used to refer to a global entity named ‘a’
How was the io class fstream used in an example given in class? What ‘kind’ of io was it used for?
The two uses are random io and update io class example was random io
What is the main difference between a C++ parametrized macro and a C++ inline method that is declared in a class but defined external to a class? Give very short examples of each
Big difference is absense of type checking with a macro Ex: #define foo(a,b) (a+b)/2
inline int foo(int i, int i){
return (i +i)/2}
State how to determine, by looking at the code, if a class is C++ is an abstract class.
It should contain at least one pure fn,
ex: void foo() = 0;
Describe briefly the difference between an abstract Java class and a Java interface.
Abstract class can contain variables and defined methods, Interfaces only constants and method prototypes. Also can only inherit one abstract class, but can implement many interfaces
If subclass sub privately inherits public variable x from class super, state the accessibility of x within sub
private
If subclass sub privately inherits public variable x from class super, state the accessibility of x within subclass subsub of sub, assuming subsub publicly inherits from sub
completely inaccessible
The most important reason in C++ for using new instead of malloc is
ability to display any class, by over riding “from”
Briefly state how ambiguity result in ‘diamond’ situations, when multiple inheritance is allowed, What ‘kind’ of base class helps to avert this problem in c++
A var in class A would have onr or two copies in D, and virtual value can be unclear C++: virtual base class
State the default field width when using cout, and explain what it means
default width is zero, and space given is minimal amount to display the item
Give two ways to change the fill char., precisely one of which is a manipulator
«_space;setfill(‘ ‘)
count.fill(‘ ‘);
Discuss the significance of the C++ keyword ‘boolalpha’
when in effect, displays booleans as true or false
Which retains its effect the longer, oct, or setw?
Oct wins
Give the output for
cout««44;
2CoX2C
The 4 io status bits have the names, good,
bad, fail, eof.
Suzie Q, a produc of the legendary California K-12 system, says that Java has many more major releases and upgrades than C++ because the people that work for Java are all addicted to caffeine, and Duke is a slave driver. Is Suzie Q right?
The claim is correct, java has more releases! But reason is wrong: java is not standardized but C++ is, making release much slower
In java, a public method in a parent class can not be override in a derived class and make private. Why does java say it has this rule? Does C++ share this rule?
Making a method public is a contract fn outside world accessibility, that extends to descendants. C++ does not share this rule, it can demote access in children
Briefly explain the difference between overloading and overriding
Overloading: multiple fns in same class, with same name
Overriding: fns with same name in parent and child classes
Sate briefly what constitutes a valid overload in c++
fn defs must have distinct signatures to be a valid overload