Saturday 14 June 2014

C++ program to print Hello World


Here are  C++ programs to print "Hello World".Here are different programs to print "Hello World " like with semicolon, without using semicolon, using pointers only, by generating interrupt and by operator overloading. Actually here I have given different methods through which you can print the string output .



C++ Program to simply print Hello world with semicolon


 #include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;


C++ Program to print Hello world without using semicolon


#include<iostream>
using namespace std;
int main()
{
    while(!(bool)(cout<<"Hello World\n")){}
    if(cout<<"Hello World\n"){}
    return 0;
}


C++ Program to print Hello world using pointers only

Below programs are made in Turbo C and will only work in Turbo C. Actually in this program we directly access VDU memory .Thus it is faster as compared to any other functions .We can directly access this memory using pointers. Thus I have typecast the address of VDU memory and store it in vid and then I have called write() function .
Now using r and c we can put our characters at any place on the screen and than through while loop I have print “HELLO WORLD”. Notice that through attr I can select different background and text colors.
#include<stdio.h>
#include<conio.h>
char far *vid;
void write(r,c,a,attr)
int r,c,attr;
char *a;
{
  char far *v;
  v=vid+r*160+c*2;
  while(*a)
  {
    *v=*a;
    v++,a++;
    *v=attr;
    v++;
  }
}
void main()
{
  clrscr();
  vid=(char far*)0xb8000000L;
  write(5,25,"HELLO WORLD",2);
  getch();
}

OUTPUT



C++ Program to print Hello world by generating interrupt

In this program we are storing our string with '$' character at last this is just like null character used to represent end of the string .
Now in _AH register we will store integer 9 and in _DX register store the address of the message we have typecast the address to int .Now we will use geninterrupt() function which is used to generate interrupts and pass the value 0x21. There are many other interrupts also like 0x10 etc. 
#include<dos.h>
void main()
{
    char *message="Hello World$";
    clrscr();
    _AH=9;
    _DX=(int)message;
    geninterrupt(0x21);
    getch();
}

Output



C++ Program to print Hello world using operator overloading

Here we have used operator overloading to print "Hello World". We have used the concept of the program in which we have print hello world using pointers. It is just like making our own object to print output like cout. This program will only work with Turboc++ 3.0.
#include<stdio.h>
#include<conio.h>
class prtout
{
public:
    int r,c,attr;
    prtout()
    {
      r=0,c=0,attr=15;
    }
};
void operator <<(prtout &print,char *a)
{
   char far *v;
   v=(char far*)0xb8000000L+print.r*160+print.c*2;
   while(*a)
   {
      *v=*a;
      v++,a++;
      *v=print.attr;
      v++;
   }
}
void main()
{
  clrscr();
  prtout print;
  print<<"Hello World";
  getch();
}




1 comment: