C++ Language Flashcards
(30 cards)
What can you tell us about it: a conditional operator?
Its a notion for some kind of if else
Conditional expression: If else
Difference between conditional statement and a condition expression
With a condition expression you are choosing one of two expression based on a boolean outcome
Condition statement: you are selecting one of two statements based on a boolean outcome.
All languages have condition statements not all languages have condition expressions
Abstract classes in C++
A class that doesn’t contain member functions, but only contains prototypes
Difference between a java abstract class and interface
If it was an interface you cant have variables In an abstract class you can have variable constants and prototypes In an abstract class you can have functions that are clearly defined
Major difference between java and c++?
The way they treat multiple inheritance
What is the difference between private and protected class members:
Private: are only accessible within the class defining them Protected: are accessible in the class that defines them and in classes that inherit from that class.
What is “this.” Give an example of when you would use “this.”
"This" refers to the current object. Example: public MyThisTest(int a){ this.a = a; // Assigns the value of the parameter a to the field of the same name }
What are the initialization and the assignment phases of a constructor? What is each used for?
The initialization phase, data members of class type are always initialized explicitly in the constructor initializer list. Initalization happens before any the statement execution of the constructor body. Assignment phase is when one object is assigned the value of another object. This happens in the constructor body.
Having a pointer as a class data member can lead to some undesirable side effects. What should you add to a class to avoid these side effects?
You can add a delete destructor (which guarantees the reset of the pointer) or add an implementation of the free() function
void you(long a, longb) {cout<< "long";} void you(double a, doubleb){cout<;} int main a. b; you(a,b); }
overloading function “you” while using same signature (is ambiguous). Code in 5a does not compile and thus does not run
int main(){ const char* what = "Is This"; what = "interesting"; cout<<< *what; }
Declaration of character array as a constant and cannot be changed does not compile and does not run
int& Now(){ int Where = 1; Where = Now(); cout << Where; }
Sends a warning that reference to local variable where is returned but it complies and runs with an output of 1
class Where{ public: int here; int foo() const; int bar() const; };
int Where :: foo() const{ here = here + 5; return here; } int Where ::bar() const{ return here + 5; } int OhNo(const Where* Now){ return now->bar(); } int main(){ Where* IsIt = new Where; cout
/* foo() and bar() are set as constant numbers and are thus read-only and you cannot manipulate any of the data members inside of it(the function(s)). Also, int here was never initialized. Any references to here are null pointer exceptions.
a. class Top{ public: int a; Top(int x) {a=x;} }; class Bottom : public Top{ public: int b; Bottom() : Top(5) {b = 0; a = 0;} }; int main(){ Bottom barrel; cout
/* compiles and runs with output of 0 0 */
class Top{ public: void Hat(char *string){cout Hat(5.5) Rung->Hat(“cat”);}
//overloading function you while using same signature (is ambiguous). Code in 7b does not compile and thus does not run */
class Top{ public: Top(){cout
compiles and runs with output of: start top, start bottom, start test, end bottom, end top */
Give the output of the following program:
#include
using namespace std;
class Top{ public: virtual void MyMemory(){cout MyMemory(); Hat->Disk(); Hat->ThisExam();
Top Dog = *(new Bottom); Dog.MyMemory(); Dog.Disk(); Dog.ThisExam(); }
/* this code does not compile and does not run because Erased() is not initialized in class Top. In inheritance parent’s method cannot be inherited from child.*/
- List the different ways to implement implicit type conversion in a class:
You can use a standard conversion. Example:
short a =2000;
int b;
b=a;
You could use constructor or operator conversions, which affect classes that include specific constructors or operator functions to perform conversions. Here, an implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Example: class A{}; class B {public: B (A a) {} }; A a; B b=a; //end example
- a. True or false. A copy of a constructor does not have an initialization phase.
False
How do the variables A and B differ? char * const A = “Hi”; const char* B = “Hi”;
char * const A = “Hi”; //constant pointer to character data const char* B = “Hi”; //character array that is constant
Explain the problems with the following uses of C and D
const char* C = “hi mom”;
C[3] = ‘a’;
char *const D = “hi mom”; D = “hi dad';
C[3] = 'a'; //can't change constant character array D = “hi dad'; //constant pointer, can change the data but //...can't change where the pointer points
#include using namespace std;
void functionA(int X, double Y) {cout
outputs: second function. Program has char set as x value so second function is implicitly called.
#include using namespace std;
class Test{ private: float data; public: void setData(int& value) {data = value;}; static float getData() {return data;}; Test(int value) : data(value) {}; };
int main(){ Test me = 10.5; cout
/* giving it 10.5 when it’s asking for an integer */
#include using namespace std;
int A = 10;
float functionB(int A, char B = 5, float C){ return ::A + B + C; }
int main(){ int A = 2; float X = 11.1; cout
/* cannot initialize a varianle in the argument section of a function */