Thursday, December 8, 2011

Reference variable : Classical function "swap" demo

#include<iostream>
using namespace std;

void swap(int &a, int& b)//'&'indicates call by reference
{
   int temp = a;
   a = b; // a and b are reference variables
   b = temp;
}
main()
{
   int x =10 ; // initialization
   int y =20 ;
   cout << "\nx = " << x << "   and y = " << y;
   swap(x,y);//call
   cout << "\nx = " << x << "   and y = " << y;
}

Reference variable : Demo

//reference.cpp

#include<iostream>

//Syntax of reference variable
//data_type & reference_name = variable_name;

using namespace std;
main()
{
 int x ;
 x = 10;
 int &y = x; // reference MUST always be initialized at declaration
 cout << "\nx = " << x << "   and y = " << y;
 y = 5;
 cout << "\nx = " << x << "   and y = " << y;
 x = 15;
 cout << "\nx = " << x << "   and y = " << y;
}

//Description:
//Lvalue : Location (address) of an expression
//Rvalue : VAlue of expression
//e.g. -
//  int a,b;
//a=4;  // Rvalue 4 is stored in Lvalue of a
// b = a;//Ralue of a is stored in Lvalue o fb



Think of REFERENCE variable as a constant pointer that is deREFERENCEd each time it is used!

Wednesday, December 7, 2011

Basic encapsulation (Accessing data through functions) Demo

//person_class.cpp

#include<iostream>
#include<string.h>
using namespace std;

class person
{
   // data members
   char name[30];  // Default access specifier is private
  int age;
  public:
   void getdata(void); //method declaration
  void display(void)//method declaration and definition
  {
  cout << endl <<"\t" << name ;
  cout << endl <<"\t" << age ;
  cout << endl <<"\t" << pid ;
  }
  private:
   int pid;
   public: void setdata(char[],int,int);
};

void person::getdata(void)
{
  cout <<"Enter name : ";
  cin>> name;
  cout <<"Enter age : ";
  cin>> age;
  cout <<"Enter pid : ";
  cin>> pid;
}

void person::setdata(char n[],int a,int p)
{
   strcpy(name,n);
   age = a;
   pid = p;
}
main()
{
   person raj,yash;
   raj.setdata((char*)"raj",20,101);

   yash.getdata();

   raj.display();
   yash.display();

   return 0;
}

Scope Resolution Operator Demo

//scope_resolution.cpp

#include<iostream>
using namespace std;

int m =10 ; //global variable

main()
{
      int m =20;

 { //starting the block
  int k = m;
  int m = 30;
   cout << endl << " m = " << m;
   cout << endl << " k = " << k; 
   cout << endl << " ::m = " << ::m;
 }//ending the block

 cout << endl << " m = " << m;
 cout << endl << " ::m = " << ::m;
}

Not using namespace std! Demo

#include<iostream>

//using namespace std;


// :: is scope resolution operator

int main()
{
  int a,b;
  std::cout<<"Enter two numbers : ";
  std::cin >> a >> b;

  std::cout << endl <<"Sum of " << a <<" and "<< b <<" is "<< a+b;

  return 0;
}

Tuesday, December 6, 2011

Procedure Oriented Programming: Example

//cshapes.h

#define T_CIRCLE 1
#define T_RECTANGLE 2
#define T_TRIANGLE 3

typedef struct CIRCLE_SHAPE
{
   short type;
   double x,y;
   double radius;

} CIRCLE_SHAPE;

typedef struct RECTANGLE_SHAPE
{
   short type;
   double x1,y1;
   double x2,y2;

} RECTANGLE_SHAPE;

typedef struct TRIANGLE_SHAPE
{
   short type;
   double x1,y1;
   double x2,y2;
   double x3,y3;

} TRIANGLE_SHAPE;

typedef union SHAPE
{
  short type;
  CIRCLE_SHAPE circle;
  RECTANGLE_SHAPE rectangle;
  TRIANGLE_SHAPE triangle;
} SHAPE;

double compute_area(SHAPE *pShape);
void draw_shape(SHAPE *pShape);


//cshapes.c
#include<stdio.h>
#include"cshapes.h" 
#include<math.h>


double compute_area(SHAPE *pShape)
{
   double area;

   switch(pShape->type)
   {
      case T_CIRCLE:
      {
         area = M_PI *pShape->circle.radius * pShape->circle.radius;
         break;
      }

      case T_RECTANGLE:
      {
         area = fabs(
    (pShape->rectangle.x2 - pShape->rectangle.x1) *
    (pShape->rectangle.y2 - pShape->rectangle.y1) 
      );
  break;
      }

      case T_TRIANGLE:
      {
        //double d1,d2,d3;
 // Hero's formula
 //d1 = (pShape->triangle.x1)
         area = 0.5; //TODO:
         break;
      }

      default: printf("Unknown shape\n");
   }

   return area;
}

void draw_shape(SHAPE *pShape)
{
   printf("Draw: ");
   switch(pShape->type)
   {
      case T_CIRCLE:
      {
         printf("Circle of radius %f at (%f,%f)\n", pShape->circle.radius, pShape->circle.x,pShape->circle.y);
         break;
      }

      case T_RECTANGLE:
      {
         printf("Rectangle with corners:  (%f, %f)and (%f,%f)\n", pShape->rectangle.x1, pShape->rectangle.y1,pShape->rectangle.x2, pShape->rectangle.y2);
  break;
      }

      case T_TRIANGLE:
      {
 printf("Triangle with coordinate: (%f,%f)", pShape->triangle.x1, pShape->triangle.y1);
      break;
      }

      default:
      printf("Unknown shape in draw!\n");
   }
}


//main.c
#include<stdio.h>
#include "cshapes.h"


int main()
{
  int i;
  SHAPE s[3];

  s[0].type = T_RECTANGLE;
  s[0].rectangle.x1 = 80.0;
  s[0].rectangle.y1 = 30.0;
  s[0].rectangle.x2 = 120.0;
  s[0].rectangle.y2 = 50.0;

  s[1].type = T_CIRCLE;
  s[1].circle.x = 200.0;
  s[1].circle.y = 100.0;
  s[1].circle.radius = 50.0;

  s[2].type = T_TRIANGLE;
  s[2].triangle.x1 = 4.0;
  s[2].triangle.y1 = 4.0;
  s[2].triangle.x2 = 4.0;
  s[2].triangle.y2 = 4.0;
  s[2].triangle.x3 = 4.0;
  s[2].triangle.y3 = 4.0;


  for (i = 0; i < 3; ++i)
 printf("Areas of shape[%d] = %f\n", i, compute_area(&s[i]));

  for (i = 0; i < 3; ++i)
    draw_shape(&s[i]);

  return 0;
}



How to run the program?

$gcc -lm -c cshapes.c -o cshapes.o
$gcc cshapes.o main.c -o shapes
$./shapes
Foot Note:
-lm links math library.
-c compile only option to generate object file.

HelloWorld.cpp

#include <iostream>

using namespace std;

// This is C++ comment


int main()
{
  int a,b,c;
  // cout is object of ostream class
  cout << "Hello World from C++ ";
  return 0;
}
// Description :
//cout is PREDEFINED object that represents
//standard output stream in C++.
//Here standard output stream is SCREEN.

//The operator << is called "INSERTION" operator or
//"PUT TO" operator.
//It sends (or inserts) contents of variable
//on its right side to the object on its 
// left.