Posts

Differentiate between structure and union?

Both are user defined data types. Structure allocate memory for all its members while union allocates memory for biggest element only. Unions are mainly used to better optimization of structures. Example: without union struct student {     int rollo;     char name[51];     char course[21];     float fees; }; struct faculty {     int empid;     char name[51];     char dept[51];     char desig[51];     float salary;   }; main() {     struct student s;     struct faculty f;     printf("Memory usage %d",sizeof(s)+sizeof(f)); //244 bytes } Example: with union struct student {     int rollo;     char name[51];     char course[21];     float fees; }; struct faculty {     int empid;     char name[51];  ...

What is template?

Generally we define argument types of the functions and the data type of data members while creating the functions and the class. Template is the mechanism which allows to pass the data to a function or data member of an instance while using them rather than creating them. Templates can be of two types 1. Function Template 2. Class Template Use template keyword along with class keyword to define the templates. Example: Function Template #include<iostream> using namespace std; template <class T1, class T2> void area(T1 x, T2 y) {     cout <<"Area is "<< x * y << endl; } main() {     area(5,6);     area(5.0,6);     area(5.6,3.4);     area('a','b'); } Example : Class Template #include<iostream> using namespace std; template <class T> class General {     private:         T x;     public:  ...

What is friend function?

Special function which can access private members of an instance which is generally not allowed. The function must be marked as friend using friend keyword inside the class. Example #include<iostream> #include<math.h> using namespace std; class Number { private: int num; public: Number(int num) { this->num=num; } void cube() { cout <<"Cube of "<< num << " is "<< num*num*num<<endl; } friend void cubeRoot(Number x); }; void cubeRoot(Number x) { cout << "Cube root of "<< x.num << " is " << pow(x.num,1.0/3) << endl; } main() { Number x(6); x.cube(); cubeRoot(x); }

Can we access private members of an instance?

Yes in C++, No in Java C++ has special concept of friends which allows to access private members of an instance. C++ gives  friend keyword to mark a friend. Friends can be of two types 1. friend function 2. friend class When a class declares a function as friend, the function gets the power to access the private members of its instance. When a class declares another class as friend, the functions of that class gets the power to accss the private members of its instance. Example: friend function #include<iostream> #include<math.h> using namespace std; class Number { private: int num; public: Number(int num) { this->num=num; } void cube() { cout <<"Cube of "<< num << " is "<< num*num*num<<endl; } friend void cubeRoot(Number x); }; void cubeRoot(Number x) { cout << "Cube root of "<< x.num << " is " << pow(x.num,1.0/...

What are inline functions?

Special functions in C++ which has the facilities of macros and functions both. Macros do not have any data type and get replaced Functions have data types and get called. Inline functions are special functions which look like a function, have the data type but do not generate a function call but get replaced like a macro. Use inline keyword to mark a function as inline function and behave like a macro. Example #include<iostream> using namespace std; inline int greatest(int a, int b, int c) { return a>b && a>c ? a : b> c ? b : c; } main() { int a,b,c; cout << "Enter three numbers : "; cin >> a >> b >> c; cout << "Greatest number is "<< greatest(a,b,c); }

Differentiate between macro and function?

A function get called while macro get replaced. Function gives an extra overhead to the CPU to leave the current statement, execute the function then come back to next statement. If we have some less number of statements in a function we can also use the macro, which are simply replace the code without generating any function call. Use #define pre-processor directive to create the macros. Macros do not have any data type while functions have. Example 1: using macro #include<iostream> #define greatest(a,b,c) (a>b && a>c ? a : b>c ? b : c) using namespace std; main() { int a,b,c; cout << "Enter three numbers : "; cin >> a >> b >> c; cout << "Greatest number is "<< greatest(a,b,c); } Example 2: using function #include<iostream> using namespace std; int greatest(int a, int b, int c) { return a>b && a>c ? a : b> c ? b : c; } main() { int a,b,c; cout ...

What is macro?

Special program code which get replaced at the time of compilation is called as macro. C and C++ provides the concept of macros Use #define pre-processor directive to create a macro Example #include<iostream> #define greatest(a,b,c) (a>b && a>c ? a : b>c ? b : c) using namespace std; main() { int a,b,c; cout << "Enter three numbers : "; cin >> a >> b >> c; cout << "Greatest number is "<< greatest(a,b,c); }