Overloading Operator

Have a look at the following code fragment:


  myclass a(3);

cout<<a[0];

Doesn’t it look awkward!


Yes it does, because we have overloaded he [] operator and given it some special
meaning.


In C++, it is possible to overload the [] operator
and give it a different meaning
rather then the usual object indexing.


The general form for overloading [] operator is:


  ret-type operator[](int);

It is considered a binary operator hence when declared as a member, it accepts
one explicit argument (usually int).


Although you are free to accept any type of argument but sticking to the original
concept of indexing, it would always be an integer.


ob[i];


When the compiler encounters the above expression (with the [] operator overloaded)
the [] operator
function
is called as below:


ob.operator[] (1)


The argument ‘1’ is passed explicitly while ‘ob’ is
passed implicitly using the ‘this’
pointer.


Enough discussion, now lets get on to some action!


Below is the example program illustrating the overloading of [] operator:



// Example Program illustrating
// the overloading of [] operator
#include <iostream.h>

class myclass
{
// stores the number of element
int num;
// stores the elements
int a[10];

public:
myclass(int num);

int operator[](int);
};

// takes the number of element
// wished to be entered.(<=10)
myclass::myclass(int n)
{
num=n;
for(int i=0;i<num;i++)
{
cout<<"Enter value for element "<<i+1<<":";
cin>>a[i];
}
}

int myclass::operator[](int index)
{
// if not out of bound
if(index<num)
return a[index];

// else
return NULL;
}

void main()
{
myclass a(3);

cout<<a[0]<<endl;
cout<<a[2]<<endl;

// out of bound
cout<<a[3]<<endl;
}


Related Articles:


Read More..

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:


Read More..

switch case statement

In some programming situation, when there are number of choices and we want to choose only appropriate choice, in that situation C provided switch statement. Thus switch statement allows us to make a decision from the number of choices.
The switch statement also known as switch-case-default.
Since, the switch statement known for decision maker.
Syntax of switch statement:

switch(integer expression)
{
 case 1 :
   do this;
   break;
 case 2 :
   do this;
   break;
 case 3 :
   do this;
   break;
 case 4 :
   do this;
   break;
 default :
   and this;
   break;
}

Example of switch-case-default statement:

/*program to demonstration of switch statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {

  int r=4;
  switch(r)
  {
   case 1 :
     printf("
I am in case 1");

     break;
   case 2 :
     printf("
I am in case 2");

     break;
   case 3 :
     printf("
I am in case 3"
);

     break;
   case 4 :
     printf("
I am in case 4"
);

     break;
   case 5 :
     printf("
I am in case 5"
);

     break;
   default :
     printf("
I am in default
");

     break;
  }
  getch();
 return 0;
 }


Output: I am in case 4

Different types of switch statement

(a) In switch statement case arrangement may be ascending order, descending order, scrambled order. So the following program should be valid and run:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int r=44;
  switch(r)
  {
   case 210 :
     printf("
I am in case 210");
     break;
   case 10 :
     printf("
I am in case 10");
     break;
   case 1 :
     printf("
I am in case 1");
     break;
   case 44 :
     printf("
I am in case 44");
     break;
   default :
     printf("
I am in defalut");
     break;
  }
  getch();
 return 0;
 }
Output: I am in case 44

(b) C also allowed to use char values in case and switch statement as:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  char a=n;
  switch(a)
  {
    case m :
      printf("
I am in case m"
);
 
    case n :
      printf("
I am in case n"
);
    case o :
      printf("
I am in case o"
);
    case p :
      printf("
I am in case p"
);
    default :
      printf("
I am in default"
);
  }
  getch();
  return 0;
 }

Output: I am in case n

Note: When above program run, m, n, o, p are actually replaced by subsequent ASCII(American Standard Code for Information Interchange) code.

(c) C language allows us to check the value of any expression in a switch statement like as:
   switch(r+c*i)
   switch(10+20%2*r) 
   switch(r+10*c)
   switch(r>10 || c<20) 
Above all switch statement are valid.
And expression can also be used in cases provided they are constant expression.
Thus case 10+15 is correct,
and however case r+c is incorrect.

(d) The switch statement is very useful while writing menu driven program.

(e) The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch.

(f) The case keyword is followed by an integer or a character constant.
Read More..

Number Rectangle

Q. Write a C program to print the following number rectangle:

1333
2222
3331

Ans.

/*c program for print number rectangle pyramid*/
#include<stdio.h>
int main()
{

 int num,n,r,c,k;
 printf("Enter no. of rows: ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  for(c=1; c<=r; c++)
     printf("%d",r);
  for(k=1; k<=n; k++)
     printf("%d",n);
  printf("
"
);

 }
 return 0;
}

The output of above program would be:


Output of number rectangle pyramid C program
Figure: Screen shot for number rectangle pyramid C program
Read More..

Introduction to PHP How to Insert Dynamic Content on WebPages


In the last article Introduction
To PHP, The Web Programming Language
, we talked about what
PHP is and what it is used for. There we talked about the Dynamic insertion
of date & time on web pages. Well, in this post we’re going to ACTUALLY
create page that displays current date & time.


We’ll also briefly talk about how PHP pages are handled by the server
and the browser (client).


Let’s get started!


Suppose we have the following:



<html>
<body>
Date: 06:00 AM, 18th April 2008
</body>
</html>


Above is a simple HTML code (some tags are intentionally left-out)


No matter when and how many times you run it, you’d see the same output
(date and time), since it is a static HTML page.


Now have a look at the following



<html>
<body>
Date: <?php echo date("H:i A, jS F Y"); ?>
</body>
</html>


This is a HTML document having embedded PHP code.


As I said PHP can make documents dynamic. So you’ll get different outputs
(date and time) depending on the condition (the date and time you run it) programmed.
The date function returns the current date and time.


If you request this page today you’ll see

06:00 AM, 18th April 2008

in your browser.


You’d see 12:00 AM, 26th May 2008, should you request it on
26th May 2008 at 12:00 AM, on the other hand the static page would always show
the same date and time statically coded into it.


Since, as I said PHP codes are interpreted server-side so if you look at the
source code of the requested page (from browser) in both the cases (static and
dynamic pages) you’d see



<html>
<body>
Date: 06:00 AM, 18th April 2008
</body>
</html>


in case of the static page, and



<html>
<body>
Date: 06:00 AM, 18th April 2008
</body>
</html>


in the case of the dynamic page requested on the same date and time as coded
into the static page.


As you can notice, the end result is a static HTML document in both cases.
The difference being that a part (date and time) of the PHP page was created
dynamically at run time.


Now it’s pretty obvious that Internet Browser don’t/don’t
need to understand PHP, its server’s job. PHP on the other hand need to
output data that a browser does understand (HTML, Image, files etc.). In this
case it’s plain text which can be considered HTML.


Related Articles:


Read More..

Learn to Define and Use Functions in C

Functions can be thought of as separate programs that are designed to perform
specific tasks. For example, in a program, separate functions can be so designed
to perform specific tasks such as taking input, doing calculation, giving output
etc.

In this article we will be discussing about what functions are and how they
are designed in C++.

Generally, programs have many logically different parts (input, output etc.)
which can be coded separately as function. This makes programs easily manageable
and understandable.

Suppose we have to design an invoice program that takes input, calculates and
at last displays the output. We have two options to do so, we can either program
the whole thing linearly or we can make separate functions for each of the tasks
(input, output etc.), this would make the program far more manageable and upgradeable.
(In the future, if you need to change certain things, you only need to alter
the functions)

In this way if you divide various activities of the program into different
function, then it would be easy for you to code and check each of the function
besides making the program easier to understand and manage.

Have a look at this program:


   //C++ program
#include<iostream.h>

   int sqr(int x);//declaration of the function
   void main(void)
{
int a=9;
cout<<"square of "<<a<<" is ";
cout<<sqr(a)<<endl;//function is being called here
}

int sqr(int x)//no ;(semicolon) here
{
x=x*x;
return (x);//return keyword is used
//to return values, in this case
//the value of x is returned
}

Few points about the program:


  • In C++, it is necessary to declare a function before defining it as we have
    done in the line:

    int sqr(int x);//declaration
    of the function


  • One function cannot be defined inside another. Therefore sqr(int x) function
    cannot be defined inside main () which itself is a function.


  • A function can return any type of values except arrays.
    The value is returned in the line:

    return (x);

    and the value got accepted and printed in the line:
    cout<<sqr(a);


Now that you understand how to declare and use functions, let me give you a
slightly more complex program:


   //C++ program
#include<iostream.h>

   //declaration of functions
int take_input(void);
void play(void);//it is optional to use the void keyword
void help(void);
void quit(void);

   //main program starts from here
void main(void)
{
int choice;
choice=take_input();//take input

   //perform action as per the value returned
if(choice==1) play();
else if(choice==2) help();
else if(choice==3) quit();
}

   //functions are defined below
int take_input()
{
int ch=0;
cout<<" MAIN MENU";
cout<<endl<<endl;
cout<<"1> PLAY"<<endl;
cout<<"2> HELP"<<endl;
cout<<"3> QUIT"<<endl;
cout<<endl<<endl;
cout<<"enter choice and press enter"<<endl;

while (ch!=1 && ch!=2 && ch!=3)
{
cin>>ch;
}

return (ch);
}

   void play(void)
{
cout<<"write code for PLAY"<<endl;
}

   void help(void)
{
cout<<"write code for HELP"<<endl;
}

   void quit(void)
{
cout<<"write code for QUIT"<<endl;
}

This is a simple program to illustrate how the various activities of a program
can be divided into separate functions. Notice how easy it is to understand
the program. Even if the functions play (), help () etc. had 100’s of
lines of code, the program would have been easily understandable.


Hope this helps…


Related Articles:


Read More..

Naive pattern matching algorithm


//Naive pattern matching algorithm implementation.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 int n,m,s;
 clrscr();
 char t[200],p[200],count=0;
 printf("Enter the data for Naive Pattern Match Algorithm
");

 printf("String: ");scanf("%s",t);
 printf("Pattern: ");scanf("%s",p);
 n=strlen(t);
 m=strlen(p);
 printf("Pattern Matched at:");
 for(s=0;s<n-m;s++)
 {
  for(int i=0;i<m;i++)
  {
   if(t[s+i]==p[i]) count++;
  }
  if(count==m)
  printf("%d ",s);
  count=0;
 }
 getch();
}
//program written by Mars.
Read More..

Blog Archive

Powered by Blogger.