C programming Flashcards
How to find string length
strlen(char *);
C++: How pair is used?
pair numCharPair; numCharPair.first = 78; numCharPair.second = 'M'; - or - numCharPair = make_pair(78, 'M');
C++: How to get current working directory from a running linux program?
getcwd(char[1024], 512);
What is C++ static_cast<>
static_cast<> can be used to convert pointers from base class to parent class & vice versa. static_cast<> between unrelated class pointers is compilation error.
How to compile C++ program with C++11 support?
g++ -std=c++11 xyz.cpp
Show an example of pointer to a member function (method).
class Foo { public: int func(char x) { return 7;}
int (Foo::*fptr)(char); fptr = &Foo::func; Foo obj; // Call below, (obj.*func)('A'); Foo *p = &obj; // Call below, (p->*func)('K');
C++: Can a static mmbr function call non-static mmbr function?
A static member function cannot access any non-static members. It is a compilation error.
Give example of typedef usage.
typedef existing_typeName new_typeName;
typedef unsigned int unsINT;
It is used to give more descriptive name to complicated types.
typedef struct student {
int roll_num;
char name[32];
} st;
st p1, p2;
What is a Makefile ‘pattern rule’?
It uses % wildcard. I can only be used in target : dependency line, never in action lines.
%.wc : %.x
wc -l $.x > $.wc
all : a.x b.x c.x
This produces line number files for each .x file
$ in the action line matches with the stem of match in pattern.
Makefile: Give an example of wildcard function usage.
HDRS = $(wildcard src/*.h)
HDRS is a list of header files in src directory.
Makefile: Give an examplle of patsubst function usage.
HDRS = $(patsubst %.cpp, %.hpp, a.cpp b.cpp x.cpp)
HDRS is
a.hpp b.hpp x.hpp
C++: Does catch block do type conversion?
No.
Exception catch block does not do type conversion.
catch(double d)
will not catch “int” exception.
How do you create a unique_ptr?
unique_ptr<int> ptr1(new int); unique_ptr<int> ptr2(new int(47); unique_ptr<int> ptr3 = make_unique<int>(47); unique_ptr<Person> p1(new Person("khan", 45)); unique_ptr<Person> p2 = make_unique<Person>("Khan", 45);