Tuesday, March 10, 2009

C Program Dynamic Memory Allocation

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

// Dynamic Memory Allocation

//--------------------------------------------------------------------------

class student
{

private:

int rno ;
char name [25] ;


public:

student ()
{
rno = 0 ;
strcmp ( name , "N.A" ) ;
}

void getinfo ()
{
gotoxy ( 10 , 5 ) ;
cout << "Enter Roll No .................... : " ;
cin >> rno ;

gotoxy ( 10 , 7 ) ;
cout << "Enter Name ....................... : " ;
gets ( name ) ;
}

void showinfo ( int row )
{
gotoxy ( 5 , row ) ;
cout << rno ;

gotoxy ( 25 , row ) ;
cout << name ;
}

};

//#define max 500

//-----------------------------------------------------------------------

void main ()
{

clrscr () ;

student *ptr ;

char ch = 0 ;
int counter = 0 ;

while ( ch !='n' && ch !='N' )
{
clrscr () ;

gotoxy ( 10 , 3 ) ;
cout << "Record No : " << ( counter + 1 ) ;

ptr = new student ;
ptr -> getinfo () ;

counter ++ ;
ptr=ptr+1;
gotoxy ( 10 , 23 ) ; cout << "Do you want to enter [Y/N] : " ;
ch = getch () ;
}

clrscr () ;

for ( int i = 0 ; i < counter ; i++ )
{
ptr -> showinfo ( 5 + i ) ;
ptr =ptr-1 ;
}

getch () ;

}

No comments:

Post a Comment