Showing posts with label Programming Skills. Show all posts
Showing posts with label Programming Skills. Show all posts

Inline Functions and their Uses

It’s a good practice to divide the program into several functions such
that parts of the program don’t get repeated a lot and to make the code
easily understandable.


We all know that calling and returning from a function generates some overhead.
The overhead is sometimes to such an extent that it makes significant effect
on the overall speed of certain complex and function-oriented programs. In most
cases, we have only a few functions that have extensive use and make significant
impact on the performance of the whole program.


Not using functions is not an option, using function-like macros is an option,
but there is a better solution, to use Inline Functions.


Yes, like it sounds, inline functions are expanded at the place of calling
rather than being “really called” thus reducing the overhead. It
means wherever we call an inline function, compiler will expand the code there
and no actual calling will be done.


Member functions of classes are generally made inline as in many cases these
functions are short but any other function can be inline too. Functions are
made inline by preceding its definition with the keyword “inline”
as shown in the example below:



// precede the definition
// of function with "inline"
// it's optional to do so for
// the prototype
inline ret-type func-name(arg-list...)
{
...
...
...
}


Member functions (class) can be made inline as below:



class myclass
{
private:
...
...

public:
ret-type func-name(arg-list...);
...
...
};

inline ret-type myclass::func-name(arg-list...)
{
...
...
}



Or simply as



class myclass
{
private:
...
...

public:
inline ret-type myclass::func-name(arg-list...)
{
...
...
}
...
...
};


Short member functions are usually made inline as above.


Please mote one thing though, inline is a request to the compiler and not a
command which means it depends on the compiler and the conditions whether a
particular function will be really made “inline” or not.


As inlining increases duplication of parts of code, performance will be gained
at the expense of program size, which is pretty obvious. As I’ve said
not all functions make major impact on the overall performance of the program.
So we should carefully select which functions to inline and which not to, because
if we inline many functions we can’t be sure of whether it’d do
any better to the performance, but it’s for sure that program size will
increase unnecessarily.


Related Articles:


Problems on Operator Overloading II

This is the second part of the artcile Problems
on Operator Overloading I
.


Problem #4: What would be the
output of the following code:



1 // Problem #4:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 friend myclass operator++(myclass);
20 };
21
22 myclass operator++(myclass ob)
23 {
24 ob.a++;
25 ob.b++;
26
27 return ob;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

Problem #5: What would be the
output of the following code:



1 // Problem #5:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 friend myclass operator+=(myclass,
20
21 myclass);
22 };
23
24 myclass operator+=(myclass ob1, myclass ob2 )
25 {
26 ob1.a+=ob2.a;
27 ob1.b+=ob2.b;
28
29 return ob1;
30 }
31
32 void main()
33 {
34 myclass a(10,20);
35 myclass b(100,200);
36
37 a+=b;
38 b+=a;
39
40 a.show();
41 b.show();
42 }

Problem #6: Is there any error(s)
in the following code:



1 // Problem #6:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator++(myclass &);
20 };
21
22 myclass myclass::operator++(myclass &ob)
23 {
24 ob.a++;
25 ob.b++;
26
27 return ob;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

ANSWERS:




  1. 10 20, Refer to this
    article
    .




  2. 10 20 100 200. Same reason as above.



  3. Has errors inline no. 19, '++' is a unary operator and hence doesn't need
    any other argument.


Related Articles:


Problems on Operator Overloading I

Here I'm listing some problems related to Operator Overloading to spice up
the discussion a bit. This is a TWO part series so read the next
article
after solving each of the problems listed here.



Problems
on Operator Overloading I


Problems
on Operator Overloading II



Problem #1: Point out the error(s)
if any in the following code:



1 // Problem #1:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator+(int);
20 };
21
22 myclass myclass::operator+(int x)
23 {
24 myclass temp;
25
26 temp.a=a + x;
27 temp.b=b + x;
28
29 return temp;
30 }
31
32 void main()
33 {
34 myclass a(10,20);
35
36 a=a+10;
37 a.show();
38 }

Problem #2: Point out the error(s)
if any in the following code:



1 // Problem #2:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 int operator=(int);
20 };
21
22 int myclass::operator=(int x)
23 {
24 a=x;
25 b=x;
26
27 return x;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33 myclass b,c;
34
35 c=b=a;
36
37 c.show();
38 }

Problem #3: Point out the error(s)
if any in the following code:



1 // Problem #6:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator++();
20 };
21
22 myclass myclass::operator++()
23 {
24 a++;
25 b++;
26
27 return this;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

ANSWERS:




  1. No Errors




  2. No Errors



  3. We need to return the object and not the pointer
    (this)
    . The statement should have been, return *this;


Related Articles:


Problems Related to Operator Overloading

To make our ongoing discussion on Operator Overloading more interesting, here
I have listed some problems related to operator overloading.


Problem #1: Point out the errors(s)
if any, in the following program:



1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8
9 public:
10 myclass(int);
11 void show();
12
13 myclass operator ++();
14 myclass operator --();
15 };
16
17 myclass::myclass(int x)
18 {
19 a=x;
20 }
21
22 void myclass::show()
23 {
24 cout<<a<<endl;
25 }
26
27 myclass myclass::operator ++()
28 {
29 a++;
30
31 return *this;
32 }
33
34 myclass myclass::operator --()
35 {
36 a--;
37
38 return *this;
39 }
40
41 // main
42 void main()
43 {
44 myclass ob(10);
45
46 ob.show();
47
48 ob++;
49 ob.show();
50 }


Problem #2: Point out the errors(s)
if any, in the following program:



1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8 int b;
9
10 public:
11 myclass(int, int);
12 void show();
13
14 myclass operator=(myclass);
15 };
16
17 myclass myclass::operator=(myclass ob)
18 {
19 a=ob.a;
20 b=ob.b;
21 };
22
23 myclass::myclass(int x,int y)
24 {
25 a=x;
26 b=y;
27 }
28
29 void myclass::show()
30 {
31 cout<<a<<endl<<b<<endl;
32 }
33
34 // main
35 void main()
36 {
37 myclass ob(10,11);
38 myclass ob2(20,21);
39 myclass ob3(30,41);
40
41 int x,y,z;
42
43 x=y=z=10;
44 cout<<z<<endl;
45
46 ob=ob2=ob3;
47 ob.show();
48 }


Problem #3: Point out the errors(s)
if any, in the following program:



1 #include <iostream.h>
2
3 // class
4 class myclass
5 {
6 int a;
7
8 public:
9 myclass(int);
10 void show();
11
12 void operator ++();
13 void operator --();
14 };
15
16 myclass::myclass(int x)
17 {
18 a=x;
19 }
20
21 void myclass::show()
22 {
23 cout<<a<<endl;
24 }
25
26 void myclass::operator ++()
27 {
28 a++;
29 }
30
31 void myclass::operator --()
32 {
33 a--;
34 }
35
36 // main
37 void main()
38 {
39 myclass ob(10);
40 myclass ob2(100);
41
42 ob.show();
43 ob2.show();
44
45 ob2=++ob;
46 ob.show();
47 ob2.show();
48 }


Answers:




  1. Although no error is reported (MS Visual C++) but line number 48 has a
    problem because postfix version of the operators have not been overloaded.
    This way the compile uses prefix form instead besides giving warning.




  2. ERROR in line number 46. Here ob2=ob3 is first evaluated by the overloaded
    assignment operator as defined but it is not returning any value which can
    be further assigned to ob.



  3. ERROR in line number 45. Operator function is not returning anything which
    can be assigned to ob2.


Related Articles:


Problems Related to Class and Inheritance

To make our ongoing discussion on Inheritance a little more interesting, I
have listed some problems or questions here. They all are related to Classes
in general and Inheritance in particular.


Problem #1: This program contains
some error(s), can you spot which line(s) contains error(s)?



1 // Problems No.1
2 // Related to Inheritance
3 #include<iostream.h>
4
5 // base class
6 class base
7 {
8 int a;
9
10 public:
11 void seta(int num){a=num;}
12 };
13
14 // derived class
15 class derived:public base
16 {
17 int b;
18
19 public:
20 void setb(int num){b=num;}
21
22 void show()
23 {
24 cout<<a<<"\n"<<b;
25 }
26 };
27
28 // main
29 void main()
30 {
31 base b;
32 derived d;
33
34 b.seta(10);
35 d.setb(100);
36
37 d.show();
38 }


Problem #2:
This program contains some error(s), can you spot which line(s) contains error(s)?



1 // Problems No.2
2 // Related to Inheritance
3 #include<iostream.h>
4
5 // base class
6 class base
7 {
8 protected:
9 int a;
10
11 public:
12 void seta(int num){a=num;}
13 };
14
15 // derived class
16 class derived:protected base
17 {
18 protected:
19 int b;
20
21 public:
22 void setb(int num){b=num;}
23 void show()
24 {
25 cout<<a<<"\n"<<b;
26 }
27 };
28
29 // main
30 void main()
31 {
32 base b;
33 derived d;
34
35 b.seta(10);
36 d.setb(20);
37
38 d.show();
39
40 b.a=5;
41 d.b=10;
42
43 d.show();
44 }


Problem #3: This program contains
some error(s), can you spot which line(s) contains error(s)?



1 // Problems No.2
2 // Related to Inheritance
3 #include<iostream.h>
4
5 // base class (1)
6 class base1
7 {
8 protected:
9 int a;
10 };
11
12 // base class (2) derived from
13 // base1
14 class base2:private base1
15 {
16 protected:
17 int b;
18 };
19
20 // derived from base2
21 class derived:public base2
22 {
23 public:
24 void set(int num1,int num2)
25 {
26 a=num1;b=num2;
27 }
28
29 void show()
30 {
31 cout<<a<<"\n"<<b;
32 }
33 };
34
35 void main()
36 {
37 derived d;
38 d.set(10,20);
39
40 d.show();
41 }


ANSWERS:




  1. Line No. 24




  2. Line No. 40 and 41



  3. Line No. 26 and 31


Related Articles:


How String Functions Work? Part II

This is the second part of the article How
String Functions (strinh.h) Work?


Here we'll be designing our own version of some other commonly used standard
library string manipulation function that we discussed in the article String
Manipulation Functions (string.h) II.


These will help increase your programming skills further.


mystrlwr():



// mystrlwr function
#include<iostream.h>

// -- FUNCTION PROTOTYPES --
char *mystrlwr(char *);
// -- ENDS --

void main()
{
char ch[]="C++ ProGramming123";

cout<<mystrlwr(ch);
}

char *mystrlwr(char *str)
{
char *temp;
temp=str;

while(*str!='\0')
{
// change only if its a
// UPPER case character
// intelligent enough not to
// temper with special
// symbols and numbers
if(*str>=65 && *str<=90)
*str+=32;
str++;
}
return temp;;
}


mystrupr():



// mystrupr function
#include<iostream.h>

// -- FUNCTION PROTOTYPES --
char *mystrupr(char *);
// -- ENDS --

void main()
{
char ch[]="C++ ProGramming123";

cout<<mystrupr(ch);
}

char *mystrupr(char *str)
{
char *temp;
temp=str;

while(*str!='\0')
{
// change only if its a
// UPPER case character
// intelligent enough not to
// temper with special
// symbols and numbers
if(*str>=97 && *str<=122)
*str-=32;

str++;
}
return temp;
}

mystrncat():



// mystrncat- function
#include<iostream.h>

void mystrncat(char *,const char *,int);

void main(void)
{
char ch[]="This is great!";
char ch2[25]="Yes ";
mystrncat(ch2,ch,6);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrncat(char *str1,const char *str2,int n)
{
int i=0;
int len=0;

// skip to the end of the first
// string(target)
while(str1[len]!='\0')
len++;

// start copying 'n' characters
// from the start of the
// second string (source)
// to the end of the
// first (target)
while(i<n)
{
str1[len]=str2[i];
i++;len++;
}
str1[len]='\0';
}

mystrncpy():



// mystrncpy- function
#include<iostream.h>

void mystrncpy(char *,const char *,int);

void main(void)
{
char ch[]="This is great!";
char ch2[20];

mystrncpy(ch2,ch,6);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrncpy(char *str1,const char *str2,int n)
{
int i=0;

// copy 'n' characters
// from str2 to str1
while(i<n)
{
str1[i]=str2[i];
i++;
}
// put the end of
// string identifier
str1[i]='\0';
}

mystrrev():



// mysrtrev function

#include<iostream.h>

char *mystrrev(char *);

void main(void)
{
char ch[]="Programming";
cout<<mystrrev(ch);

cout<<ch;
}

char *mystrrev(char *str)
{
char temp;
int len=0;
int i,j;

// find the length
while(str[len]!='\0')
len++;

j=len-1;

for(i=0;i<len/2;i++)
{
// interchange the
// characters from
// the beginning to
// the end
temp=str[j];
str[j]=str[i];
str[i]=temp;
j--;
}

return str;
}

Related Articles:


How String Functions (string.h) Work?

In the previous article String
Manipulation Functions (string.h)
, we had a look at some of the commonly
used string manipulation functions. There is no denying the fact that those
functions are useful but have you ever wondered how those functions actually
work or what is the algorithm behind their working?


If yes then read on…


In this article I am going to present you with our own version of the string
manipulation functions that we had discussed, namely strlen(), strcpy(),
strcat() and strcmp()
. Our versions will do the same thing as done
by the original functions but surely they would teach us a lot!


Let's have a look at them one-by-one:


mystrlen


  // mystrlen- function 
#include<iostream.h>

int mystrlen(const char *);

void main(void)
{
char ch[]="This is great!";
cout<<"Length:"<<mystrlen(ch);
}

int mystrlen(const char *str)
{
int len=0;

while(str[len]!='\0')
len++;
return len;
}

mystrcpy


  // mystrcpy- function 
#include<iostream.h>

void mystrcpy(char *,const char *);

void main(void)
{
char ch[]="This is great!";
char ch2[20];

mystrcpy(ch2,ch);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrcpy(char *str1,const char *str2)
{
int i=0;

// copy each character
while(str2[i]!='\0')
{
str1[i]=str2[i];
i++;
}
// put the end of
// string identifier
str1[i]='\0';
}

mystrcat


  // mystrcat- function 
#include<iostream.h>

void mystrcat(char *,const char *);

void main(void)
{
char ch[]="This is great!";
char ch2[25]="Yes ";

mystrcat(ch2,ch);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrcat(char *str1,const char *str2)
{
int i=0;
int len=0;

// skip to the end of the first
// string(target)
while(str1[len]!='\0')
len++;

// start copying characters
// from the start of the
// second string (source)
// to the end of the
// first (target)
while(str2[i]!='\0')
{
str1[len]=str2[i];
i++;len++;
}
str1[len]='\0';
}

mystrcmp


  // mystrcmp- function
#include<iostream.h>

int mystrcmp(char *,const char *);

void main(void)
{
char ch[]="C++";
char ch2[]="C++";

cout<<mystrcmp(ch2,ch);
}

int mystrcmp(char *str1,const char *str2)
{
int i=0,cmp=-1;

while(str1[i]!='\0')
{
if(str1[i]==str2[i])
{
// check one character
// following it, so that
// end of string is also
// compared
if(str1[i+1]==str2[i+1])
cmp=0;
// if not same then check
// to see which string has
// higher ASCII value for the
// non-matching charcter
else if(str1[i+1]<str2[i+1])
{
cmp=-1;
break;
}
else
{
cmp=1;
break;
}
}
else if(str1[i]<str2[i])
{
cmp=-1;
break;
}
else
{
cmp=1;
break;
}
i++;
}
return cmp;
}

Good-Bye!


Related Articles:


Increase your Programming Skills III

Here I have listed some questions related to strings in c++, solve them to
increase your programming skills.


Problem #1:


  // Problem related to strings

#include<iostream.h>

void main(void)
{
char ch[]="Programming Skills";
int i=0;

cout<<ch[++i];
cout<<ch[i++];
cout<<i++[ch];
cout<<++i[ch];
}

Problem #2:


  // Problems related to strings

#include<iostream.h>

void main(void)
{
char ch[]="Programming Skills";
char *s="Programming Skills";

cout<<sizeof(ch)<<sizeof(s);
cout<<sizeof(*ch)<<sizeof(*s);
}

Problem #3:


  // Problems related to strings

#include<iostream.h>

void main(void)
{
int n[]={4,3,2,1,0};

for(int i=0;i<5;i++)
cout<<n[n[i]];
}

Problem #4:


  // Problems related to strings

  #include<iostream.h>

void main(void)
{
int n[]={11,10,9,8,7,6,5,4,3,2,1,0};
char ch[]="C++ Language";

for(int i=0;i<12;i++)
cout<<ch[n[i]];
}

ANSWERS:


#1: rroh


#2: 19411


#3: 01234


#4: egaugnaL ++C


Good-Bye!


Related Articles:


Increase your Programming Skills II

This is the continuation of the last article… Increase
your Programming Skills


Solve the problems listed here to gain programming skills.


Problem #5:


  // Problem or Question in C++
// --------------------------
// Problem related to pointers
// for increasing programming
// skills

#include<iostream.h>

void main()
{
char ch1[]="Programming";
char ch2[]="Skills";

char *s1=ch1;
char *s2=ch2;

while(*s1++=*s2++);
cout<<ch1;
}

Problem #6:


  // Problem or Question in C++
// --------------------------
// Problem related to pointers
// for increasing programming
// skills

#include<iostream.h>

void main()
{
int i, *j;

i=1;
j=&i;

cout<<i**j*i+*j;
}

Problem #7:


  // Problem related to pointers

#include<iostream.h>

void main()
{
int ar[]={1,2,3,4,5};
char *p;

p=(char *)ar;

cout<<*((int *)p+3);
}

Problem #8:


  // Problem related to pointers

#include<iostream.h>

void change(int *,int);

void main()
{
int ar[]={1,2,3,4,5};

change(ar,5);

for(int i=0;i<5;i++)
cout<<*(ar+i);
}

void change(int *p,int n)
{
for(int i=0;i<n;i++)
*(p+i)=*(p+i)+3;
}

ANSWERS:


#5: Skills


#6: 2


#7: 4


#8: 45678


Good-Bye!


Related Articles:


Increase your Programming Skills I

Here I have listed some selected problems or questions related to pointers
in C++. Solve them to increase your programming skills.


Problem #1:


  // Problem or Question in C++
// --------------------------
// Problem related to pointers
// for increasing programming
// skills

#include<iostream.h>

void main()
{
char ch[]="I_Like_C++";
cout<<*&*&ch;
}

Problem #2:


  // Problem or Question in C++
// --------------------------
// Problem related to pointers
// for increasing programming
// skills

#include<iostream.h>

void main()
{
int i,*p,**q,***r;

i=10;
p=&i;
q=&p;
r=&q;

cout<<i<<*p<<**q<<***r;
}

Problem #3:


  // problem related to pointers

#include<iostream.h>

void main()
{
char ch[]="Programming Skills";
char *s=ch;
cout<<s+++3;
}

Problem #4:


  // Problem related to pointers

#include<iostream.h>

void main()
{
int ar[2][2][2]={1,2,3,4,5,6,7,8};
cout<<***ar;
cout<<ar[1][1][1];
}

ANSWERS:


#1: I_Like_C++


#2: 10101010


#3: gramming Skills


#4: 18


Since this article has become a bit too long, I have broken it into one more
part.

Read Increase
your Programming Skills II



Hope this helps!


Good-Bye!


Related Articles:


Check out this stream