Basic C++ Flashcards
Mutable
Allow update non <test> static member from const function</test>
Compiler constant trick
Enum enum { dimension = 10}
double array[dimension];
Explicit Inlining
Class Test {
Inline void setdate();
};
void Test::setdate() { ….. }
Why function style cast
Optimize constructor call when return an object by function by value.
Complex getComplexnumber() {
return Complex( real, image)
}
Array initialization
int array[3] = {1,2,3}
Placement new
Syntax T *ptr = new (/* address */) T ( optional args )
inheritance: Access Privileges Public, Protected and private
if Public on derived class everything is the same, if Protected then Public of base class will be protected, if private then Public and protected of base class will be private
How to change inherited access for some function of base class?
By keyword “using :: under Public: or Protected: or Private:
inheritance: Function Hiding
if derived class has the same function name then that function no longer access on base class. We can use “using ::; to make it available. or you can explicit specify ::
what function never inherited by derived class
Constructor, destructor and assignment operator
what is upcast and sliced off in term of Derived-to-Base class
upcast is casting pointer of derived class to base class sliced off is create base class by derived class
why do we need virtual function?
to able to have pointer of base class but the actual call will call derived class function.
Note: If derived class object call base class function and inside base class function call function that have the same name as base and derived then the function on base class will be called if it is not virtual function.
Ex:
class dog {
void bark() { cout << “I’m dog”; }
void seesomething() { bark(); }
}
class yellowdog : public dog {
void bark() { cout << “I’m yellow dog”; }
}
function bark should be virtual function
why destructor should declare as virtual function
to make sure that derived class destructor wiil get called.
Can we less restrict exception specification in virtual function?
No.
why use virtual in part of derivation process?
to solve problem of two subclass of class has common class (diamond problem). For instance, base class “Appliance” is derived by “AlarmClock” also derived by “Radio”. And class “AlarmClockRadio” derived both “Radio” and “AlarmClock”. We want to create only one base class “Appliance”