Pages

Jul 11, 2013

Static Variable in Class

This is a very simple example differentiating static  member variables and  regular member variables of the class.

I will be updating this post later with more examples on static variables and  functions

#include <iostream>
using namespace std;
class staticExample{
      int mydata;
      static int count; // Declaration
 public:

      staticExample( ){
      mydata = 50;   // static variable cannot be initialized
      count +=1;     // inside the class becoz d variable is SAME
      mydata++;      // for each class object. However, mydata
}                    // is unique for each class object.
                     // As we know, obj1->mydata is different   from obj2->mydata
                     // but, obj1->count is equal to obj2->count ... all the time
                  void getData(){
               cout<<"mydata : "<<mydata<<" count : "<<count<<endl;
          }
};
int staticExample :: count = 50; // Initialization, out side the class

int main(){
     staticExample john;
     john.getData();

     staticExample alex;
     alex.getData();
}

Jul 10, 2013

Defining Macros

Example 1
#include <stdio.h>

#define SUM a+b    // Macros used just to replace values .
                                        // Whenever compiler see 'SUM' keyword in the program      
                                       //  it will replaced with a+b .

                                       //  It works on the principal of function, so you don't have to
                                       //  write same long particular statement every where in the program 
                             

int main (){

   int a = 5,
       b = 3,
       c;             

   c = SUM - 1;     // this statement is same as c = a + b - 1;
   printf("%d", c);

   return 0;

}

 

Jul 9, 2013

Dynamic Array Example




#include <iostream.h>

class IntArray{
  int *p, junk;
  int size;

  public:

  IntArray(int size){       
    p = new int[size];
    this->size = size;
  }

  int length(void)const{
    return size;
  }

  void resize(int newsize){
    int *temp = new int[newsize];
    for(int i=0; i<newsize && i < size;i++){
      temp[i]=p[i];
    }

    delete[] p;
    p = temp;
    size = newsize;
  }

Jul 2, 2013

Some addressing fundamentals for C beginners


The following program explains how properly addressed variable don't loose their correct values

#include<iostream>
using namespace std;

void func_widout_address(int a, int b){
a = 10;
b = 30;
}

void func_wid_address(int &x, int &y){
x = 100;
y = 200;
}
int main(){
int a = 20;
int b = 40;

func_widout_address(a,b);
cout<<"a: "<<a<<", b: "<<b<<endl;

func_wid_address(a,b);
cout<<"a: "<<a<<", b: "<<b<<endl;
return 0;
}
// ** Output **
// a: 20, b: 40
// a: 100, b: 200

Passing number of Vairable Arguments in function

Passing number of Variable Arguments in function

#include <stdio.h>
#include <stdarg.h>

int sum(int num, ...); /* Function which takes n number of arguments */

int main(void){
  int s1;
  s1 = sum(4, 100,200,300,400);/* Lets put some numbers to add up       */
  s2 = sum(6,6,6,6,6);        /* which can be changed on each function */
                               /* call, but calling the same function   */
  printf("The sum S1 is: %d\n",s1);
  printf("The sum S2 is: %d\n",s2);
  return 0;
}

int sum(int num,...){
  int S = 0, i;
  va_list L; /* 'va_list' - structure defined in 'stdarg.h' header file */

  va_start(L,num); /* 'va_start' -function taking object 'L'(of Structure 
                       va_list) and 'num' -total number of arguments
                       passed when 'sum' function is called */
                   /*  Object 'L' will hold all the values when sum is   
                       called */

  for(i=0;i<num;i++){
   S += va_arg(L, int); /* va_arg will convert current value is L into
                          'int' (function argument) and return an 'int' */
  }

  va_end(L);        /* destructing Object 'L'  */
return S;           /* returning 'S' to command line */
}

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( );
};