Your Ad Here

Wednesday, February 27, 2008

Shuttle Sort - Simple Insertion Sort

#include< stdio.h>
#include< conio.h>

void shutsort(int a[],int n)
{
int j,i=1,mid;

while(i< n)
{
j=i-1;
while(j>=0)
{
if(a[j]>a[j+1])
{
mid = a[j];
a[j] = a[j+1];
a[j+1]=mid;
j--;
}
else
break;
}
i++;
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shutsort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT
Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 98

Element 5 : 1

Array Befor Sorting : 21 36 54 98 1
Array After Sorting : 1 21 36 54 98
*/

Shell Sort

#include< stdio.h>
#include< conio.h>

void shellsort(int a[],int n)
{
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
{
for(j = m;j< n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
}
}
}
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shellsort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT

Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 2

Element 5 : 0

Array Befor Sorting : 21 36 54 2 0
Array After Sorting : 0 2 21 36 54
*/

Merge Sort

#include< stdio.h>
#include< conio.h>

void mergesort(int a[],int n)
{
int b[50],c,low1,high1,high2,low2;
int i,k,j;
c=1;
while(c< n)
{
low1=0;
k=0;
while(low1+c< n)
{
low2=low1+c ;
high1=low2-1;
if(low2+c-1< n)
high2=low2+c-1;
else
high2=n-1;
i=low1;
j=low2;
while(i< =high1 && j< =high2)
{
if(a[i]< =a[j])
b[k++] =a[i++];
else
b[k++] = a[j++];

}
while(i< =high1)
b[k++]=a[i++];
while(j< =high2)
b[k++] =a[j++];
low1=high2+1;
}
i=low1;
while(k< n)
b[k++] =a[i++];
for(i=0;i< n;i++)
a[i]=b[i];
c=c*2;
}
}

main()
{
int a[20],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
mergesort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT

Enter The number Of Elements : 10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 6566

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Befor Sorting : 12 54 98 6566 45 12 5 1 156 21
Array After Sorting : 1 5 12 12 21 45 54 98 156 6566


*/

Quicksort

#include< stdio.h>
#include< conio.h>

void quicksort(int [],int,int);
int partition(int [],int,int);


main()
{
int a[20],p,q,i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

p=0;
q=n-1;
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
quicksort(a,p,q);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}


void quicksort(int a[],int p,int q)
{
int j;
if(p< q)
{
j=partition(a,p,q+1);
quicksort(a,p,j-1);
quicksort(a,j+1,q);
}
}

int partition(int a[],int m,int p)
{
int v,i,j;
int temp;
v=a[m];
i=m;j=p;
do
{
do
{
i += 1;
}
while(a[i]< v);
do
{
j -= 1;
}
while(a[j]>v);

if(i< j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
while(i< j);
a[m] =a[j];
a[j] = v;
return j;
}

/* OUTPUT

Enter The number Of Elements : 10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 6566

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Befor Sorting : 12 54 98 6566 45 12 5 1 156 21
Array After Sorting : 1 5 12 12 21 45 54 98 156 6566


*/

Monday, February 25, 2008

Simple Selection Sort

#include< stdio.h>
#include< conio.h>

void simplesel(int a[],int b[],int n)
{
int j,i=0,k,H;
int L=32760;
k=0;
L=H;
while(i< n)
{
for(j=0;j< n;j++)
{
if(a[j]< L)
{
L = a[j];
k = j;
}
}
a[k] = H;
b[i] = L;
L = H;
i++;
}
}

main()
{
int a[10],b[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
simplesel(a,b,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",b[i]);
getch();
return 0;
}
/* OUTPUT
Enter The number Of Elements : 10

Element 1 : 21

Element 2 : 25

Element 3 : 63

Element 4 : 45

Element 5 : 1

Element 6 : 147

Element 7 : 10

Element 8 : 78

Element 9 : 96

Element 10 : 5
Array Befor Sorting : 21 25 63 45 1 147 10 78 96 5
Array After Sorting : 1 5 10 21 25 45 63 78 96 147
*/

Tuesday, February 12, 2008

Factorial

/* C++ program for calculating factorial of a number entered by the user */

# include < iostream.h>
# include < conio.h>
void fact(int);

void main()
{
clrscr();
int n;
cout < < "Enter limit : ";
cin>>n;
fact(n);
getch();
}
void fact(int x)
{
char a;
int i=1;
for(int j=1;j< =x;j++)
{
i*=j;
}
cout< < "Answer = "< < i< < endl;
cout< < "Want to continue ( y )/or press any key to continue ";
cin>>a;
if(a=='y')
{
main();
}
else
{
cout< < "Good Bye ";
}
getch();
}

Sunday, February 10, 2008

Huffman Coding

/* Huffman Coding in C . This program reads a text file named on the command line, then compresses it using Huffman coding. The file is read twice, once to determine the frequencies of the characters, and again to do the actual compression. */

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < time.h>

/* there are 256 possible characters */

#define NUM_CHARS 256

/* tree node, heap node */

typedef struct _treenode treenode;
struct _treenode {
int freq; /* frequency; is the priority for heap */
unsigned char ch; /* character, if any */
treenode *left, /* left child of Huffman tree (not heap!) */
*right; /* right child of Huffman tree */
};

/* this is a priority queue implemented as a binary heap */
typedef struct _pq {
int heap_size;
treenode *A[NUM_CHARS];
} PQ;

/* create an empty queue */

void create_pq (PQ *p) {
p->heap_size = 0;
}

/* this heap node's parent */

int parent (int i) {
return (i-1) / 2;
}

/* this heap node's left kid */

int left (int i) {
return i * 2 + 1;
}

/* this heap node's right kid */

int right (int i) {
return i * 2 + 2;
}

/* makes the subheap with root i into a heap , assuming left(i) and
* right(i) are heaps
*/
void heapify (PQ *p, int i) {
int l, r, smallest;
treenode *t;

l = left (i);
r = right (i);

/* find the smallest of parent, left, and right */

if (l < p->heap_size && p->A[l]->freq < p->A[i]->freq)
smallest = l;
else
smallest = i;
if (r < p->heap_size && p->A[r]->freq < p->A[smallest]->freq)
smallest = r;

/* swap the parent with the smallest, if needed. */

if (smallest != i) {
t = p->A[i];
p->A[i] = p->A[smallest];
p->A[smallest] = t;
heapify (p, smallest);
}
}

/* insert an element into the priority queue. r->freq is the priority */
void insert_pq (PQ *p, treenode *r) {
int i;

p->heap_size++;
i = p->heap_size - 1;

/* we would like to place r at the end of the array,
* but this might violate the heap property. we'll start
* at the end and work our way up
*/
while ((i > 0) && (p->A[parent(i)]->freq > r->freq)) {
p->A[i] = p->A[parent(i)];
i = parent (i);
}
p->A[i] = r;
}

/* remove the element at head of the queue (i.e., with minimum frequency) */
treenode *extract_min_pq (PQ *p) {
treenode *r;

if (p->heap_size == 0) {
printf ("heap underflow!\n");
exit (1);
}

/* get return value out of the root */

r = p->A[0];

/* take the last and stick it in the root (just like heapsort) */

p->A[0] = p->A[p->heap_size-1];

/* one less thing in queue */

p->heap_size--;

/* left and right are a heap, make the root a heap */

heapify (p, 0);
return r;
}

/* read the file, computing the frequencies for each character
* and placing them in v[]
*/
unsigned int get_frequencies (FILE *f, unsigned int v[]) {
int r, n;

/* n will count characters */

for (n=0;;n++) {

/* fgetc() gets an unsigned char, converts to int */

r = fgetc (f);

/* no more? get out of loop */

if (feof (f)) break;

/* one more of this character */

v[r]++;
}
return n;
}

/* make the huffman tree from frequencies in freq[] (Huffman's Algorithm) */

treenode *build_huffman (unsigned int freqs[]) {
int i, n;
treenode *x, *y, *z;
PQ p;

/* make an empty queue */

create_pq (&p);

/* for each character, make a heap/tree node with its value
* and frequency
*/
for (i=0; i< NUM_CHARS; i++) {
x = malloc (sizeof (treenode));

/* its a leaf of the Huffman tree */

x->left = NULL;
x->right = NULL;
x->freq = freqs[i];
x->ch = (char) i;

/* put this node into the heap */

insert_pq (&p, x);
}

/* at this point, the heap is a "forest" of singleton trees */

n = p.heap_size-1; /* heap_size isn't loop invariant! */

/* if we insert two things and remove one each time,
* at the end of heap_size-1 iterations, there will be
* one tree left in the heap
*/
for (i=0; i< n; i++) {

/* make a new node z from the two least frequent
* nodes x and y
*/
z = malloc (sizeof (treenode));
x = extract_min_pq (&p);
y = extract_min_pq (&p);
z->left = x;
z->right = y;

/* z's frequency is the sum of x and y */

z->freq = x->freq + y->freq;

/* put this back in the queue */

insert_pq (&p, z);
}

/* return the only thing left in the queue, the whole Huffman tree */

return extract_min_pq (&p);
}

/* traverse the Huffman tree, building up the codes in codes[] */

void traverse (treenode *r, /* root of this (sub)tree */
int level, /* current level in Huffman tree */
char code_so_far[], /* code string up to this point in tree */
char *codes[]) {/* array of codes */

/* if we're at a leaf node, */

if ((r->left == NULL) && (r->right == NULL)) {

/* put in a null terminator */

code_so_far[level] = 0;

/* make a copy of the code and put it in the array */

codes[r->ch] = strdup (code_so_far);
} else {

/* not at a leaf node. go left with bit 0 */

code_so_far[level] = '0';
traverse (r->left, level+1, code_so_far, codes);

/* go right with bit 1 */

code_so_far[level] = '1';
traverse (r->right, level+1, code_so_far, codes);
}
}

/* global variables, a necessary evil */

int nbits, current_byte, nbytes;

/* output a single bit to an open file */

void bitout (FILE *f, char b) {

/* shift current byte left one */

current_byte < < = 1;

/* put a one on the end of this byte if b is '1' */

if (b == '1') current_byte |= 1;

/* one more bit */

nbits++;

/* enough bits? write out the byte */

if (nbits == 8) {
fputc (current_byte, f);
nbytes++;
nbits = 0;
current_byte = 0;
}
}

/* using the codes in codes[], encode the file in infile, writing
* the result on outfile
*/
void encode_file (FILE *infile, FILE *outfile, char *codes[]) {
unsigned char ch;
char *s;

/* initialize globals for bitout() */

current_byte = 0;
nbits = 0;
nbytes = 0;

/* continue until end of file */

for (;;) {

/* get a char */

ch = fgetc (infile);
if (feof (infile)) break;

/* put the corresponding bitstring on outfile */

for (s=codes[ch]; *s; s++) bitout (outfile, *s);
}

/* finish off the last byte */

while (nbits) bitout (outfile, '0');
}

/* main program */

int main (int argc, char *argv[]) {
FILE *f, *g;
treenode *r; /* root of Huffman tree */
unsigned int n, /* number of bytes in file */
freqs[NUM_CHARS]; /* frequency of each char */
char *codes[NUM_CHARS], /* array of codes, 1 per char */
code[NUM_CHARS], /* a place to hold one code */
fname[100]; /* what to call output file */

/* hassle user */

if (argc != 2) {
fprintf (stderr, "Usage: %s < filename>\n", argv[0]);
exit (1);
}

/* set all frequencies to zero */

memset (freqs, 0, sizeof (freqs));

/* open command line argument file */

f = fopen (argv[1], "r");
if (!f) {
perror (argv[1]);
exit (1);
}

/* compute frequencies from this file */

n = get_frequencies (f, freqs);
fclose (f);

/* make the huffman tree */

r = build_huffman (freqs);

/* traverse the tree, filling codes[] with the codes */

traverse (r, 0, code, codes);

/* name the output file something.huf */

sprintf (fname, "%s.huf", argv[1]);
g = fopen (fname, "w");
if (!g) {
perror (fname);
exit (1);
}

/* write frequencies to file so they can be reproduced */

fwrite (freqs, NUM_CHARS, sizeof (int), g);

/* write number of characters to file as binary int */

fwrite (&n, 1, sizeof (int), g);

/* open input file again */

f = fopen (argv[1], "r");
if (!f) {
perror (argv[1]);
exit (1);
}

/* encode f to g with codes[] */

encode_file (f, g, codes);
fclose (f);
fclose (g);
/* brag */
printf ("%s is %0.2f%% of %s\n",
fname, (float) nbytes / (float) n, argv[1]);
exit (0);
}

Operator Overloading

/* C++ Program for the IMPLEMENTATION OF OPERATOR OVERLOADING(BINARY). The Given Program performs Basic Arithematic operation : Addition, Subtraction, Multiplication and Division for Two Complex Numbers. */

#include< iostream.h>
#include< conio.h>
#include< process.h>
class complex
{
float real;
float imag;
public:
complex()
{}
complex(float x,float y)
{
real=x;
imag=y;
}
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
void display(void)
{
cout< < real< < " +i" < < i mag< < endl;
}

};

complex complex :: operator +(complex c)
{
complex c2;
c2.real=real+c.real;
c2.imag=imag+c.imag;
return (c2);
}

complex complex :: operator -(complex c)
{
complex c2;
c2.real=real-c.real;
c2.imag=imag-c.imag;
return (c2);
}
complex complex :: operator *(complex c)
{
complex c2;
c2.real = ((real * c.real) - (imag * c.imag));
c2.imag = ((real * c.imag) + (imag * c.imag));
return (c2);
}
complex complex :: operator /(complex c)
{
complex c2;
c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
c2.imag=((imag * c.real) - (real * c.imag))/((real * c.real) + (imag * c.imag));
return (c2);
}

void main()
{
clrscr();
complex c1,c2,c3;
int op;
char ch,y,Y;

c1 = complex(5.6,2.7);
c2 = complex(3.5,5.6);
cout< < "Two Complex numbers Are :"< < endl;
c1.display();
c2.display();
do
{
cout< < endl< < "******** MENU *********"< < endl;
cout< < "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit"< < endl;
cout< < "Enter Your Choice : ";
cin>>op;
switch(op)
{
case 1:
c3 = c1 + c2;
cout< < "Addition of Two complex Nos. :";
c3.display();
break;

case 2:
c3 = c1 - c2;
cout< < "Subtraction of Two complex Nos. :";
c3.display();
break;

case 3:
c3 = c1 * c2;
cout< <" Multiplication of Two complex Nos. :";
c3.display();
break;

case 4:
c3 = c1 / c2;
cout< < "division of Two complex Nos. :";
c3.display();
break;

case 5:exit(0);

default: cout< < endl< < "Aborting!!!!!!!INVALID CHOICE"< < endl;
}
cout< < " Do you want to continue(Y/y)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
/******************* OUTPUT ********************
Two Complex numbers Are :
5.6 +i2.7
3.5 +i5.6

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 1
Addition of Two complex Nos. :9.1 +i8.3
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 2
Subtraction of Two complex Nos. :2.1 +i-2.9
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 3
Multiplication of Two complex Nos. :4.48 +i46.48
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 4
division of Two complex Nos. :1 +i-0.631048
Do you want to continue(Y/y)n
*/

Friend Function

/* C++ program for the Implementation Of Friend Function */

#include< iostream.h>
#include< conio.h>
class complex
{
float real;
float imag;
public:
void getdata(float x,float y)
{
real=x;
imag=y;
}
friend complex add (complex c1,complex c2);
void display()
{
cout< < "the complex no is"< < real< < "+i"< < imag< < endl;
}

};
complex add (complex c1,complex c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return (c3);

}
void main()
{
clrscr();
complex c1,c2,c3;

c1.getdata(4.2,5.5);
c2.getdata(3.5,5.6);
c3=add(c1,c2);
c3.display();
}

Implementation Of Destructors

/* IMPLEMENTATION OF DESTRUCTORS */

#include< iostream.h>
#include< conio.h>

int count = 0;

class data
{
public:
data(void)
{
count++;
cout< < endl< < "The Number of The Object Created Is:"< < count;
}

~data()
{
cout< < endl< < "The Number Of The Object Destroyed Is"< < count;
count--;
}
};

void main()
{
clrscr();
data d1;
data d2;
{
data d3;
data d4;
}
data d5;
data d6;
}


/**********OUTPUT**********
The Number of The Object Created Is: 1
The Number of The Object Created Is: 2
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number Of The Object Destroyed Is 2
The Number Of The Object Destroyed Is 1

*/

Monday, February 4, 2008

Implementation Of Constructors

A constructor is a member function with the same name as its class. For example:

class X
{
public:
X(); // constructor for class X
};

Constructors are used to create, and can initialize, objects of their class type.

You cannot declare a constructor as virtual or static, nor can you declare a constructor as const, volatile, or const volatile.

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.


Types?

Default Constructor:

Constructor() {
name = "";
size = 0;
text = "";
}

-takes 0 parameters, and initializes them as any other language could.


Parameter List Constructor:

Constructor(String n, int s, String t){
name = t;
size = s;
text = t;
}

-again similar to other languages, but this kind of work is not necessary, this is one of many places C++ shines.


Paramenter List Constructor 2:

Constructor(String n, int s, String t):name(n),size(s),text(t) {}


-this method utilizes a feature similar to the contructor, it is as if your primitive types now have there own constructors!! Simplifying your code.

*There is one more thing you can add to this last modification, it will combine the Default and List Parameter Constructors into one!
It will also allow for only partial constructors (eg: only enter a name or name and size only!)


List Parameter with Defaults Constructor:

Constructor(String n="", int s=0, String t=""):name(n),size(s),text(t) {}


-in this case when information is supplied, if only one field is given, the left most field is assumed to be the field supplied. If any fields are left out, or this constructor is called as the default, the values set equal will be used instead!!

Now that is all well and good for primatives, but what about objects?
C++ to the rescue, there is a copy constructor format wich you can use to initialize an object using another object of the same type!

Copy Constructor:

Constructor(const Constructor& c){
this->name = c.name;
this->size = c.size;
this->text = c.text;
}


/* IMPLEMENTATION OF DIFFERENT TYPES OF CONSTRUCTORS */

#include<>
#include<>

class data
{
private:
int x,y,z;

public:
data(void)
{
x = 0;
y = 0;
z = 0;
cout< <"This Is The First Type Of The Constructor"< < endl;
cout< < " The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a)
{
x = a;
y = 0;
z = 0;
cout< < "This Is The Second Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a,int b)
{
x = a;
y = b;
z = 0;
cout< < "This Is The Third Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl < < endl;
}

data(int a,int b,int c)
{
x = a;
y = b;
z = c;
cout< < "This Is The Fourth Type Of The Constructor"< < endl;
cout< <"The Three Values Are"< < x< <" ,"< < y< < ","< < z< < endl;
}
};

void main()
{
data d1();
data d2 = data(9);
data d3(1,2);
data d4(1,2,4);

getch();
}

/* OUTPUT *

This Is The First Type Of The Constructor
The Three Values Are0,0,0

Is The Second Type Of The Constructor
The Three Values Are9,0,0

This Is The Third Type Of The Constructor
The Three Values Are1,2,0

This Is The Fourth Type Of The Constructor
The Three Values Are1,2,4 */

FUNCTION OVERLOADING in C++

/* C++ Program For the IMPLEMENTATION OF FUNCTION OVERLOADING. The Program Finds the Area OF A Circle, Square and rectangle */

#include< iostream.h>
#include< conio.h>

int area(int);
int area(int,int);
float area(float);

void main()
{
clrscr();
cout< < " Area Of Square: "< < area(4);
cout< < " Area Of Rectangle: "< < area(4,4);
cout< < " Area Of Circle: "< < area(3.2);
getch();
}

int area(int a)
{
return (a*a);
}

int area(int a,int b)
{
return(a*b);
}

float area(float r)
{
return(3.14 * r * r);
}

/*****OUTPUT******
Area Of Square: 16 Area Of Rectangle: 16 Area Of Circle: 10.048 */

Pass Object As An Argument

/*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the two heights given in feet and inches. */

#include< iostream.h>
#include< conio.h>

class height
{
int feet,inches;
public:
void getht(int f,int i)
{
feet=f;
inches=i;
}
void putheight()
{
cout< < "\nHeight is:"< < feet< < "feet\t"< < inches< < "inches"< < endl;
}
void sum(height a,height b)
{
height n;
n.feet = a.feet + b.feet;
n.inches = a.inches + b.inches;
if(n.inches ==12)
{
n.feet++;
n.inches = n.inches -12;
}
cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl;
}
};
void main()
{
height h,d,a;
clrscr();
h.getht(6,5);
a.getht(2,7);
h.putheight();
a.putheight();
d.sum(h,a);
getch();
}

/**********OUTPUT***********

Height is:6feet 5inches

Height is:2feet 7inches

Height is 9 feet and 0

*/

Static Data Functions

/* C++ Program For the IMPLEMENTATION OF STATIC MEMBERS FUNCTIONS */

#include< iostream.h>
#include< conio.h>
class customer
{
private:
static int count;
int accountno;
public:
void setaccountno(void);
void displayaccountno(void);
void static displaycount(void);
};
int customer::count;

void customer::setaccountno(void)
{
count++;
accountno=count;
}

void customer :: displayaccountno(void)
{
cout< < "The Account Number Is:"< < accountno< < endl;
}
void customer :: displaycount(void)
{
cout< < "The Total Numer Of Account Are:"< < count< < endl;
}
void main()
{
customer c1,c2;
clrscr();
c1.setaccountno();
c2.setaccountno();
c1.displayaccountno();
c2.displayaccountno();
c1.displaycount();
getch();
}


/********* OUTPUT *************
The Account Number Is:1
The Account Number Is:2
The Total Numer Of Account Are:2
*/

Static Data members

/* C++ program For the IMPLEMENTATION OF STATIC DATA MEMBERS */

#include< iostream.h>
#include< conio.h>
class book
{
private:
static int count;
int bookid;
public:
addbook(void)
{
count ++;
bookid=count;
}
displaybook()
{
cout< < "The Number Of The Books Are:"< < count;
}
};
int book::count;
void main()
{
book b1,b2;
clrscr();
b1.addbook();
b2.addbook();
b1.displaybook();
cout< < endl;
b2.displaybook();
getch();
}

/* OUTPUT
The Number Of The Books Are:2
The Number Of The Books Are:2
*/

Student Marks

/* C++ program for the Implementation Of Class And Objects. The Programs Take inputs of n number of students, their marks and roll number and then displays the ranker. */

#include< iostream.h>
#include< conio.h>
class student
{
int count;
struct stud
{
int rollno;
char name[10];
float marks;
}s[10];

public:
void getcount(void);
void getdata(void);
void putdata(void);
void findranker(void);
};

void student::getcount(void)
{
cout< < "Enter no of Students:";
cin>>count;
}

void student::getdata(void)
{
int i;
for(i=0;i< count;i++)
{
cout< < "Name: ";
cin>>s[i].name;
cout< < "Roll no:";
cin>>s[i].rollno;
cout< < "marks:";
cin>>s[i].marks;
}
}

void student::putdata(void)
{
int i;
cout< < "_______________________________________"< < endl;
cout< < "Name\t"< < "Roll no.\t"< < "Marks"< < endl;
cout< < "---------------------------------------"< < endl;
for(i=0;i< count;i++)
cout< < s[i].name< < "\t"< < s[i].rollno< < "\t\t"< < s[i].marks< < endl;
cout< < "---------------------------------------";
}

void student::findranker(void)
{
int i,loc=0;
float top;
top=s[0].marks;
for(i=0;i< count;i++)
{
if(s[i].marks>top)
{
top=s[i].marks;
loc=i;
}

}
cout< < "\nThe Ranker Is:";
cout< < s[loc].name;
}
void main()
{
clrscr();
student s;
s.getcount();
s.getdata();
s.putdata();
s.findranker();
getch();
}

/***************** OUTPUT ***************
Enter no of Students:4
Name: Lionel
Roll no:1
marks:90
Name: Cyril
Roll no:45
marks:89
Name: Valerian
Roll no:56
marks:87
Name: Noronha
Roll no:56
marks:80
_______________________________________
Name Roll no. Marks
---------------------------------------
Lionel 1 90
Cyril 45 89
Valerian 78 87
Noronha 56 80
---------------------------------------

The Ranker Is : Lionel*/
Your Ad Here