C++ Language Flashcards
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 */
Amend the following code so that both C and C++ compilers will approve it
int *p; p = malloc(20*sizeof(int));
//cast malloc to int( (int*)malloc(…);
In C++ a class and a struct are extremely similar, and differ in only one way. How do they differ?
//structs are by default public and classes are by default private
Explain how virtual base classes are used to solve one of the problems of multiple inheritance, and say which problem they solve.
Solve the problem of multiple inheritance ambiguity. There would be a problem if a grandchild class inherited from 2 parent classes and a grandparent method was called.
What is the output of
cout «_space;oct «_space;44 «_space;hex «_space;55;
–> outputs 44 in octal and 55 in hexadecimal.
- set_new_handler(…) serves what purpose as a C++ supplied method?
Make more storage available for a new attempt to allocate storage.
Discuss the significant different between c++ and java as oop languages
- Only c++ has multiple inheritance
\ Java does nto have the ability to get the address of the variable - Java has no address arithmetic (ability to grab a address)
- Dynamic polymorphism is the only Java option
- Java has no external constants, variables, methods (more purely object oriented)
Because you can’t have external functions - c++ allows primitive operator overriding
// In c++ you can over ride a + sign - c++ is better at rpg (meaning it give you finer control over information displayed
on a screen) - How does java manage memory(Garbage collector)
c++ does not do garbage collection therefore the programmer is requrired to manage his own memory
Historically alot of people have made a good living by making it known by word of mouth that
they were good a retofitting memory management - Java handles memory by garbage collection
- c++leaves it up to the programmer
when you delete an array
/////////////
int (*a) [100000];
delete [] a; // is how you delete an array
// You need to clue the complier in that you want to delete the whole thing
- c++leaves it up to the programmer
- Java is more strongly typed
because c++ inherits from c you aren’t really obligated to specify a return try on a function
classic different between java 1.1 and c++
java source code is translated into a byte code
c++ complier could produce byte code, trees, but they aren’t any obligation to produce the same intermediate form of code
9. Java has byte code and is interpretable: there are interpreters
Java is more portable
c++ has the potential to be faster
10. The fact that java is not standardized and c++ is
c++ get redefined promptly every 12 years
java is far more agile in terms of changes and api additions
the difference is standardization