Pages

Jun 5, 2011

COUT Class code


#include <stdio.h>
#include<iostream>
using namespace std;
#define endl '\n'
class syp{
  ostream* ost;   // output stream  pointer
public:
  syp(ostream* is){     // accepting 'cout' as constructor argument
     ost = is;                 // assigning 'cout' to output stream ost
  }
 friend syp operator<<(const syp&, const char*); //(lft operand -> class obj,rht operand -> char*/int /double)
 friend syp operator<<(const syp&, const int);      // e.g.   syp<<8;
 friend syp operator<<(const syp& s, const double x);
 friend syp operator<<(const syp& s, const char c);
 };

syp operator<<(const syp& s, const char c){
s.ost->put(c);     // put (const char) is a function of ostream class
return s;              // return class syp object  's'
}
 syp operator<<(const syp& s, const char* os){
s.ost->write(os,strlen(os));    // write (const char*, length) is a function of ostream class
return s;
 }
 syp operator<<(const syp& s, const int x){
s.ost->operator<<(x);           // operator<<(const int) is an operator of ostream class
return s;
 }
 syp operator<<(const syp& s, const double x){
s.ost->operator<<(x);           //operator<<(const double) is an operator of ostream class
return s;
 }

May 7, 2011

Linked List in C++

A good Doubly linked list functionality class with template.
Enjoy people !! 

template <class T>
class node {
   private:
      T data;
      node *prev, *next;
   public:
      node(T);
      void setnext(node<T> *);
      void setprev(node<T> *);
      node<T>* getnext( );
      node<T>* getprev( );
      T showdata( );
};

May 6, 2011

Swap Objects with Reference

SWAP EXAMPLE


In the following example we are swapping objects. Java only allows the exchange or swapping values between two objects with reference.


There are two functions created demonstrating proper and improper exchange of data.


public class swap{
 private int num;
 private String name;
 public swap(){
  num = 0;
  name = null;
 } 
 
 public swap(int n, String name){
  num = n;
  this.name = name; 
 }
 public  void improperSwap(swap a, swap b){
  swap t;
  t = a;
  a = b;
  b = t;
  System.out.println("InSide ImproperSwap");
  System.out.println(a);
  System.out.println(b); 
  System.out.println("*** Bye Swap ****\n"); 
 }
 public String toString(){
  String t;
  t = "The number is :" + num + "\n" + 
      "The name is: " + name;
     return t;
 }
 public static void main(String args[]){
  swap one , two, swapper;
  one = new swap(10,"Ashu");
  two = new swap(45,"Dennis");
  swapper = new swap();
  swapper.improperSwap(one, two);
  System.out.println(one);
  System.out.println(two);
 }
}