Thursday, 21 August 2014

Pyramid And Structure Using C

#include 
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}
 

o/p
*
* *
* * *
* * * *
* * * * *

Program Of Template Partial Specialization

template 
class sortedVector
{
    public:
    void insert (T val)
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && val > vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }
    // other functions...
    private:
    T *vec_data;
    int length;
    int size;
};

To declare a partially specialized template that handles any pointer types, we'd add this class declaration


template
class sortedVector
{
    public:
    // same functions as before.  Now the insert function looks like this:
    insert( T *val )
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && *val > *vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }

    private:
    T** vec_data;
    int length;
    int size;
};

typeid, polymorphic class Using C++ Programing


#include
#include
#include
using namespace std;
class CBase 
virtual void f()
{} 
};
class CDerived : public CBase {};
int main () 
{
try 
{
CBase* a = new CBase;
CBase* b = new CDerived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
catch (exception& e) 
{ cout << "Exception: " << e.what() << endl; 
}
return 0;
}

Type Casting In Class Using C++ Programing


#include
using namespace std;

class CDummy
{
    float i,j;
};

class CAddition
{
int x,y;
  public:
CAddition (int a, int b)
{ x=a; y=b; }
int result()
{ return x+y;}
};

int main ()
{
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}

C++ Example Pointer Program


#include 
#include 
void main()
{
int i=10;
int *Ptr;
clrscr();
Ptr=&i;
cout<<"\nValue Of i :"<cout<<"\nAddress Of i :"<cout<<"\nValue Of Ptr :"<cout<<"\nAddress Of Ptr :"<<&Ptr;
cout<<"\nPtr's Pointer Value:"<<*Ptr;
cout<<"\nPtr Equal to &i :"<<*(&i));

Pointer To Functions In C++


#include
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <  return 0;
}