Basic C++ Flashcards

1
Q

Mutable

A

Allow update non <test> static member from const function</test>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Compiler constant trick

A

Enum enum { dimension = 10}

double array[dimension];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explicit Inlining

A

Class Test {

Inline void setdate();

};

void Test::setdate() { ….. }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why function style cast

A

Optimize constructor call when return an object by function by value.

Complex getComplexnumber() {

return Complex( real, image)

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Array initialization

A

int array[3] = {1,2,3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Placement new

A

Syntax T *ptr = new (/* address */) T ( optional args )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

inheritance: Access Privileges Public, Protected and private

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to change inherited access for some function of base class?

A

By keyword “using :: under Public: or Protected: or Private:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

inheritance: Function Hiding

A

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 ::

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what function never inherited by derived class

A

Constructor, destructor and assignment operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is upcast and sliced off in term of Derived-to-Base class

A

upcast is casting pointer of derived class to base class sliced off is create base class by derived class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

why do we need virtual function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

why destructor should declare as virtual function

A

to make sure that derived class destructor wiil get called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can we less restrict exception specification in virtual function?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

why use virtual in part of derivation process?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to choose to call base class constructor?

A

Write base class name followed by a parenthetical list of arguments to be passed to constructor.

17
Q

to use “atol”, what is include file?

A
18
Q

is this program call copy constructor of MyClass?

MyClass a, b;

a = b;

A

No This program will call assignment operator on a=b.

19
Q

Is copy constructor called or assignment operator called from the following snippet code:

MyClass a;

Const MyClass b = a;

A

Copy constructor is called because: Copy constructor is called when a new object is created from an existing object, as a copy of the existing object

20
Q

What is Conversion Operator

A

We can also write conversion operators that can be used to convert one type to another type.

21
Q

What will happen?

Test *ptr = new Test;

A

There are actually two things that happen in the above statement–memory allocation and object construction; the new keyword is responsible for both. One step in the process is to call operator new in order to allocate memory; the other step is to actually invoke the constructor. Operator new only allows us to change the memory allocation method, but does not do anything with the constructor calling method. Keyword new is responsible for calling the constructor, not operator new.

22
Q

How can overload operator?

A

It is started by “operator” keyword then by symbol of operator.

int operator+(int y); // t+y means t.operator+(y)

We can use operator to convert object to other type

operator string() const { return to_string(a); }

23
Q

What is global operator and why do we need it?

A

If that operator is not the memeber of that class then we need to create friend class in that object to support that operator.

Ex: operator “<<”, we want ostream to work with our object with ostream. ostream class is in the left side (cout << myobj; // cout.operator<<(myobj)).

We can’t change ostream class then we have to add global operator in our class then it will work with ostream.

friend ostream& operator<<(ostream &out, myclass) { ???; return out;}

24
Q

When to use static_cast

A
  • to convert object from one type to another, operator type conversion need to be defined
  • convert pointer/reference from one type to related type
25
Q

When to use dynamic_cast

A

convert base class to derieved class

Note: need to have at least one virtual function

26
Q

When to use reinterpret_cast

A

to cast address value to object.

Ex: long p = 510000;

Dog *p = reinterpret_cast&ampDog *&amp(p)

27
Q

How to cast shared_ptr?

A

static_pointer_cast

dynamic_pointer_cast

const_pointer_cast

28
Q

Date time functions in C++

A

time_t cur_time;

cur_time = chrono::system_clock::to_time_t(chrono::system_clock::now());

struct tm _tm;

char buffer[80];

localtime_r(&cur_time, &_tm);

strftime(buffer, 80, “%Y/%m/%d”, &_tm);