Compare Macros and Inline function

0
237
views
COMPARE MACROS AND INLINE FUNCTION

DIFFERENCE BETWEEN MACROS AND INLINE FUNCTION:

Macros inline
1. A macro is a per processed statement. 1. Compiler inserts function body when there is a call for inline function.
2.  #define is required for defining macros. 2. Inline keyword is required foe defining inline function.
3. A single line definition. 3. Allows multiple lines of definition.
4. Compiler cannot ignore #define. 4. Compiler can ignore inline keyword.

PROGRAM ON MACROS AND INLINE FUNCTION:

#include<stdio.h>

#define macrosq(x) x*x

Inline int insq(int x)

{

return x*x;

}

void main()

{

clrscr();

cout<<”\n macrosq 5 is “<<macrosq(5);

cout<<”\n macrosq 2+3 is “<<macrosq(2+3);

cout<<”\n inlinesq  5 is “<<insq(5);

cout<<”\n inline sq 2+3 is ”<<insq(2+3);

getch();

}

Output :

macrosq 5 is 25

macrosq 2+3 is 11

inlinesq 5 is 25

inlinesq 2+3 is 25

This pointer

This is a pointer variable which

contains the address of the current

object.

Keyword this represents a pointer to

the object whose member function being

executed.

In simple manner we can say this is a

pointer to the object to itself.

this pointer is implicitely defined in

each member function.

Instead of calling a data member or

member function directly you can use

this  pointer for calling them.

cpp program to illustrate the usage of

this pointer

#includeusing namespace std; /* local variable is same as a member’s name */class Test{private:int x;public:void setX (int x){// The ‘this’ pointer is used to retrieve the object’s x// hidden by the local variable ‘x’this->x = x;}void print() { cout << “x = ” << x << endl; }}; int main(){Test obj;int x = 20;obj.setX(x);obj.print();return 0;}

Output:

x = 20

Static data member

It allows a local  variable to retain its  previous value when the block is reentered.

Syntax:-

Static datatype datamember;

Syntax  for initialising static data member :-

Datatype classname::static datatype=initialvalue;

Accessing of static datamember

Classname ::datamember;

Static datamembers of a class are also known as “class members” because they are shared between all the objects of the class .

Program to illustrate the usage of static data member

#include

Class empstatic

{ static int cnt;

Public: empstatic()

{ cnt++;

}

~empstatic()

{      cnt–;

}

Int totemp()

{ return cnt;

}};

Int empstatic::cnt=0;

Int main()

{  empstatic a,b,c;

Cout<<”number of objects in main “<<a.totemp()<<endl;

{ empstatic p,q;

Cout<<”number of objects in the block “<<b.totemp()<<endl;

}

Cout<<”number of objects after the block “<<a.totemp()<<endl;

Return 0;

}

Output:-

Number of objects in the main 3

Number of objects in the block 5

Number of objects after the block 3

FRIEND FUNCTION AND FRIEND CLASSES

Private and protected members of a class cannot be accessed from outside

the class , but this rule is not affecting friends

According to encapsulation and data hiding ,non member functions should not be allowed to access private and protected members of other class.

Friend function can violate the rules of data hiding and encapsulation.

There are two kinds of friends:-

Friend function

Friend class

FRIEND CLASS

A friend class has the privileges to access protected and private members of other class.

Friend function declaration should be preceded by “friend” keyword.

Friend function definition is just like normal CPP function.

Friend function definition does not use scope resolution operator and friend keyword.

A function can be declared as a friend in any number of classes.

Usually it has the objects as the arguments.

It cannot access the member function directly and has to use an object and dot (.) operator.

It can be invoked like normal function without the help of any object.

It cannot be called using the objects of the class.

Scope of friend function not in the class in which it has been declared as friend.

FRIEND CLASS PROGRAM

#include

#include

Class A

{

Int a,b;

public:

A()

{

a=5;

b=6;

}

friend class B;

}

class B:public A

{

public:

void show()

{

Cout<<”value of a&b”<<a+b;

}};

void main()

{

clrscr();

B.show();

getch();

}

OUTPUT:-

Value of a&b 11

 FRIEND FUNCTION

Definition of friend function:-

In object oriented  programming ,a friend function is a “friend”  of a given  class ,is a function that is given the same access as methods to private and protected data.

A  friend function is declared by the class that is granting access ,so friend functions are part of class interface.

 Syntax of friend function :-

class classname

{

//Body of the class

public:

friend returntype functionname (class name);// friend protype

}

C++ program using friend function

#include

#include

class base

{

int val1,val2;

public:

void get()

{

cout<<”Enter two values”<<endl;

cin>>val1>>val2;

}

friend  float  mean(base ob);

};

float mean(base ob)

{

return float ((ob.val1+ob.val2)/2);

}

void main()

{

clrscr();

base obj;

obj.get();

cout<<”Mean value is:    “<<mean(obj);

getch();

}

Output:

Enter two values:

10     20

Mean value is:  15.000000

Dynamic Memory Allocation and Deallocation

To allocate memory dynamically in c++, we use the new operator. Deallocation is the cleanup of space being used for variables or other data storage.

Compile-time variables are automatically deallocated based on their known extent (some as scope for automatic variables). new, delete operators in c++ are used for dynamic memory allocation and deallocation. One use of dynamically allocated memory is to allocate memory of variable size which is not possible with compiler. The most important  uses is flexibility provided to programmers. We are free to allocate and deallocate memory whenever we need and whenever we don’t need. There are many cases where this flexibility helps.

Example:-  LinkedLists,Trees… etc.

For normal variables like int a, char str [0] memory is automatically allocated and deallocated for dynamically allocated memory like

int *p=new int [10].

It is programmers responsibility to deallocate memory when no longer needed.  If user does not deallocate then memory leak can occur. C uses malloc( ) and calloc ( ) functions to allocate dynamically and uses free ( ) function to deallocate c++ supports these functions of also has two operators NEW,DELETE ; that perform task of allocation and deallocation memory in a better and easier way.

NEW: denotes a request for memory allocation on the heap. If sufficient memory is available new operator initializes the memory and returns the address of the newly allocated and initilaized memory to pointer variable.

ptr variable=new data type;

Here pointer variable is a ptr of type,data type. It could be any built in (or) user defined data type.

Memory for named variables is allocated by compiler.

Exact size and  type of storage must be known at compile time.

Program on dynamic memory allocation and deallocation:

#include

#include

class base

{

int c;

public:

void sum (int a,int b)

{

c=a+b;

}

void display()

{

cout<<“sum is c”>>;

}

};

void main ()

{

clrscr ();

base *obj;

obj=new base;

obj->sum (5,6);

obj->display ();

delete (obj);

getch ();

}

Output:-

Sum is 11

MULTIPLE    CHOICE   QUESTIONS

      1  )What is object in c++?

A ) object is a part of  syntax of a class .

B ) object is a  datatype of class.

C ) object is an instance of class.

D )  object is a function of class.                                                          (c)

2)what is the default visibility mode for members of classes in c++?

A ) private

B )public

C )protected

D )depends                                                                                                   (a)

3)How can be define member function outside the class?

a)using union

b)using structure

c)using pointers

d)using scope resolution operator                                                                (d)

4)How can we access data members using objects?

a)object@datamember

b)object*datamember

c)object->datamember

d)object.datamember                                                                                     (d)

5)what is syntax of destructor in c++?

a)!classname

b)@classname

c)$classname

d)~classname                                                                                                    (d)

6)Can we overload a constructor ?

a)true

b)false                                                                                                                     (a)

7)constructors has return type void?

  1. a) true

b)false                                                                                                                       (a)

8) int *ptr[5];

a)a pointer of 5 int elements

b)an array of 5 int pointers

c)an array of 5 elements returning an int value

d)none of the above                                                                                             (b)

9)Destructors are called ?

A)not defined

b)in any order

c)in the reverse order of constructor calls

d)in the same order of constructor calls                                                                (c)

10)explicit call to a constructor means?

a)not providing constructor name at all

b)is the shorthand method

c)providing the constructor name explicitly to invoke it

d)providing the constructor name implicitly to invoke it                                     (c)

11)In cpp, dynamic  memory allocation is done using ——operator

a)calloc()

  1. b) malloc()

c)allocate

d)new                                                                                                                              (d)

12) which of the following is not a false statement about new operator?

a)it can’t be overloaded

  1. b) it returns garbage value when memory allocation fails

c)it automatically computes the size of the data object

d)all of these                                                                                                                (c)

13)which of the following operator is used to release the dynamically allocated

  memory in cpp?

a)remove

b)free

c)delete

d)both b & c.                                                                                                                  (C)

14)During dynamic memory allocation in cpp,new operator returns —-value if

Memory allocation is unsuccessful.

a)false

b)NULL

c)zero

d)none                                                                                                                         ( b)

15)Assume class TEST . which of the following statements is/are responsible to invoke copy constructor?

  1. a) TEST T2(T1)
  2. b) TEST T4=T1

c ) T2=T1

d)all of these                                                                                             

   both(a)&(b)

16)If default constructor is not defined ,then how the objects of the class will be created?

a)the compiler will generate error

b)error will occur at runtime

c)compiler provides its default constructor to build the object

d)none                                                                                                      

          (c)

17) If a class contains static variable ,then every object of the class has its copy of static variable.

a)true

b)false                                                                                                            

         (b)

18)static variable must be declared in public section of the class

a)true

b)false                                                                                                              

     (b)

19) Which of the following is true about static data members?

1 .They can access non-static data members

2 . They can call only other static member functions

3 .they can access global functions and data

  1. They can have this pointer
  2. They cannot be declared as const or volatile

a)only 2

b)only 2,5

c)only 2,3,4,5

d)only 2,3,5                                                                                                        

      (d)

20)If a program uses inline function , then the function is expanded inline at

a)compile time

b)run time

c)both a and b

d)none                                                                                                                    

    (b)

21)default value of static variable is –

a)0

b)1

c)garbage value

d)compiler dependent                                                                                        

  (a)

22)where does keyword friend should be placed?

a)function declaration

b)function definition

c)main unction

d)none                                                                                                                          (a)

23) choose the right option

              String*x,y

a)x is a pointer to string , y is a string

b ) y is a pointer to a string ,x is a string

c ) both x and y are pointer to string types

d)none

24)which of the following is illegal?

a)int *ip;

b)string s,*sp=0;

c)int  i ; double *dp=&i;

d)int *pi=0;                                                                                                          

     (c)

25)what will happen in this code?

int a=100,b=200;

int *p=&a,*q=&b;

p=q;

a)b is assigned to a

b)p now points to b

c)a is assigned to b

d)q now points to a                                                                                                     (b)

For more details on Please Click Here