Object Oriented Programming Inheritance

Published on June 2017 | Categories: Documents | Downloads: 34 | Comments: 0 | Views: 220
of 34
Download PDF   Embed   Report

Comments

Content

Chapter 4
Example 1 : Program to print value and address of a variable
#include <iostream.h>
#include<conio.h>
void main()
{
int i=5;
clrscr();
cout<<"Value of i : "<<i<<"\n";
cout<<"Address of i : "<<&i;
getch();
}
Sample input and output
Value of i : 5
Address of i : 0x8fc5fff4

Example 2 : A copy() function for arrays
#include <iostream.h>
double* copy(double [], int);
void print(double [], int);
void main()
{
double a[8] = {22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9};
print(a, 8);
double* b = copy(a, 8);
a[2] = a[4] = 11.1;
print(a, 8);
print(b, 8);
}
double* copy(double a[], int n)
{
double* p = new double[n];
for (int i = 0; i < n; i++)
p[i] = a[i];
return p;
}
void print(double a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;

}
Example 3 :
#include <iostream.h>
struct employee
{
char name[30];
int code;
int salary;
}emp[30];
main()
{
int i;
for(i=0;i<30;i++)
{
cout<<"Enter the name of the employee\n";
cin>>emp[i].name;
cout<<"Entre the code of employee\n";
cin>>emp[i].code;
cout<<"Enter the salary\n";
cin>>emp[i].salary;
}
for(i=0;i<30;i++)
{
if(emp[i].salary>10000)
{
cout<< " Employee having salary > 10,000 : " <<emp[i].name ;
}
}
}
Example 4 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct player
{
char name[30],country[30];
int No_Of_Matches;
float avg;
}
p[50];

void main()
{
int i,j,temp;
char str[30];
clrscr();
for(i=0;i<50;i++)
{
cout<<"Enter the name of player: ";
cin>>p[i].name;
cout<<"Enter the name of country of player: ";
cin>>p[i].country;
cout<<"Enter the number of matches he had played: ";
cin>>p[i].No_Of_Matches;
cout<<"Enter the Batting avg : ";
cin>>p[i].avg;
}
for(i=0;i<50;i++)
{
for(j=1;j<50;j++)
{
if(p[i].avg<p[j].avg)
{
temp=p[i].No_Of_Matches;
p[i].No_Of_Matches=p[j].No_Of_Matches;
p[j].No_Of_Matches=temp;
temp=p[i].avg;
p[i].avg=p[j].avg;
p[j].avg=temp;
strcpy(str,p[i].name);
strcpy(p[i].name,p[j].name);
strcpy(p[j].name,str);
strcpy(str,p[i].country);
strcpy(p[i].country,p[j].country);
strcpy(p[j].country,str);
}
}
}
for(i=0;i<50;i++)
{
cout<<"\nThe name of player: "<<p[i].name;
cout<<"\nThe name of country of player: "<<p[i].country;
cout<<"\nThe number of matches he had played:
"<<p[i].No_Of_Matches;
cout<<"\nThe Batting avg : "<<p[i].avg;

}
}

Example 5 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int m;
} s[5];
void main()
{
int i,j,temp;
char temp1[20];
for(i=0;i<5;i++)
{
cout<<"\n Enter the name of student "<<i;
cin>>s[i].name;
cout<<"\n Enter the marks of student "<<(i+1);
cin>>s[i].m;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(s[i].m>s[j].m)
{
temp=s[j].m;
s[j].m=s[i].m;
s[i].m=temp;
strcpy(temp1,s[j].name);
strcpy(s[j].name,s[i].name);
strcpy(s[i].name,temp1);
}
}
for(i=0;i<5;i++)
{
cout<<"\n Name : "<<s[i].name<<"\t Marks : "<<s[i].m;
}
getch();

}

}

Example 6 : Program for Use of Union
#include <iostream.h>
#include <conio.h>
void main ()
{
union sample
/* union declaration */
{
int a ;
char b ;
float c ;
};
union sample s ; /* union variable declaration */
clrscr () ;
/* stored as integer and retrieved as integer */
s.a = 10 ;
cout<< "a = "<<s.a ;
/* stored as character and retrieved as character */
s.b = 'x' ;
cout<<"\nb = "<<s.b ;
/* stored as float and retrieved as float */
s.c = 100.34 ;
cout<<"\nc = "<< s.c;
/*stored as float and retrieved as integer-a wrong way*/
s.c = 1.567 ;
cout<<"\na = "<<s.a ;
getch () ;
}

Example 7 :
#include<iostream.h>
#include<string.h>
#include<conio.h>
struct student
{
char name[20],DOB[8];
int RollNumber,m;
}s[5];

void

main()
{
int i,j,temp;
char temp1[20];
for(i=0;i<5;i++)
{
cout<<"\n Enter the name of student : "<<i;
cin>>s[i].name;
cout<<"\n Enter the marks of student : "<<i;
cin>>s[i].m;
cout<<"\n Enter the DOB of student : "<<i;
cin>>s[i].DOB;
cout<<"\n Enter the RollNumber of student : "<<i;
cin>>s[i].RollNumber;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(s[i].m>s[j].m)
{
temp=s[j].m;
s[j].m=s[i].m;
s[i].m=temp;
temp=s[j].RollNumber;
s[j]. RollNumber =s[i]. RollNumber;
s[i]. RollNumber =temp;
strcpy(temp1,s[j].DOB);
strcpy(s[j].DOB,s[i].DOB);
strcpy(s[i].DOB,temp1);
strcpy(temp1,s[j].name);
strcpy(s[j].name,s[i].name);
strcpy(s[i].name,temp1);
}
}
for(i=0;i<5;i++)
{

cout<<"\n Name : "<<s[i].name<<" \t Marks : "<<s[i].m<<"\tDOB :
"<<s[i].RollNumber;
}
getch();
return(0);
}}

Example 8 : Program for dynamic implementation of linked list
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
};
struct node *list=0;
struct node * getnode(void)
{
/*function to allocate memory for new node*/
return((struct node*)new(struct node));
}/*end getnode*/
void freenode(struct node *p)
{
/*free dynamically allocated memory*/
delete(p);
}/*end freenode*/
void display()
{
/*display all nodes*/
int i;
struct node *t;
t=list;
if(t==NULL)
cout<<"\nThe linked list is empty";
else
{
cout<<"\n";
while(t!=NULL)
{
cout<<"-->|"<<t->info<<"|";
t=t->next;
}
}
}/*end display*/
void insertbeg(int x)
{
/*insert new element at the beginning of linked list*/
struct node *q;
q=getnode();
q->info=x;

q->next=list;
list=q;
display();
}/*end insertbeg*/
void insertend(int x)
{
/*insert new element at the end of linked list*/
struct node *q,*temp;
q=getnode();
q->info=x;
q->next=NULL;
temp=list;
if(temp==NULL)
list=q;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=q;
}
display();
}/*void insertend*/
void insafter(int p,int x)
{
/*insert new element after a specific node*/
struct node *q,*t;
int i;
if(list==NULL)
cout<<"\nInvalid insertion";
else
{
t=list;
for(i=0;i<p-1;i++)
t=t->next;
q=getnode();
q->info=x;
q->next=t->next;
t->next=q;
display();
}
}/*end insafter*/
void delafter(int p)
{
/*delete new element after a specific node*/
int x,i;

struct node *t,*q;
t=list;
for(i=0;i<p-1;i++)
t=t->next;
if((list==NULL)||(t->next==NULL))
cout<<"\nInvalid deletion(list is empty/end of list)";
else
{
q=t->next;
x=q->info;
t->next=q->next;
freenode(q);
cout<<"\nThe deleted element is "<<x;
}
display();
}/*end delafter*/
void deletebeg(void)
{
/*delete a node from the beginning of the linked list*/
int x;
struct node *q;
q=list;
if(list==NULL)
cout<<"\nThe linked list is empty";
else
{
x=q->info;
list=q->next;
cout<<"\nThe deleted element is "<<x;
freenode(q);
display();
}
}/*end deletebeg*/
void deleteend(void)
{
/*delete a node from the end of the linked list*/
struct node *q,*temp;
int x;
q=list;
if(q==NULL)
cout<<"\nThe linked list is empty";
else
{
if(q->next==NULL)
{
x=q->info;

list=NULL;
}
else
{
while(q->next!=NULL)
{
temp=q;
q=q->next;
}
x=q->info;
temp->next=NULL;
}
cout<<"\nThe deleted element is "<<x;
freenode(q);
}
display();
}/*end deleteend*/
void insloc(int p,int x)
{
/*insert new node at a specific location*/
int t,i;
struct node *q,*temp;
temp=list;
for(i=0;i<(p-2);i++)
{
temp=temp->next;
if(temp==NULL)
{
cout<<"\nThere are less than "<<p<<"elements in list "<<p;
break;
}
}
if(temp!=NULL)
{
q=getnode();
q->info=x;
q->next=temp->next;
temp->next=q;
}
display();
}/*end insloc*/
void delloc(int p)
{
/*delete a node from a specific location*/
int i;
struct node *temp,*t;

temp=list;
if(p==1)
list=list->next;
for(i=0;i<p-1;i++)
{
if(temp->next==NULL)
{
cout<<"\nThere are less than "<<p<<"elements in list "<<p;
break;
}
t=temp;
temp=temp->next;
}
if(temp->next!=NULL)
{
cout<<"\nThe deleted element is "<<temp->info;
t->next=temp->next;
freenode(temp);
}
display();
}/*end delloc*/
void search(int x)
{
/*search a node in linked list*/
struct node *t;
int i;
for(t=list,i=0;t!=NULL;t=t->next,i++)
{
if(t->info==x)
{
cout<<"\nPosition of element "<<x<<"is "<<i+1<<endl;
break;
}
}
if(t==NULL)
cout<<"\nElement not found";
display();
}/*end search*/
void main()
{
struct node * getnode(void);
int ch,p,info,x;
char ans='y';
clrscr();
cout<<"\n\tDynamic Linked List";

cout<<"\n1.Insert after a node";
cout<<"\n2.Delete after a node";
cout<<"\n3.Insert at begining ";
cout<<"\n4.Delete at begining ";
cout<<"\n5.Insert at end ";
cout<<"\n6.Delete at end ";
cout<<"\n7.Insert at location";
cout<<"\n8.Delete at location";
cout<<"\n9.Search an element";
back:cout<<"\nEnter ur choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the node no : ";
cin>>p;
cout<<"\nEnter the info : ";
cin>>x;
insafter(p,x);
break;
case 2: cout<<"\nEnter the node no : ";
cin>>p;
delafter(p);
break;
case 3: cout<<"\nEnter the info : ";
cin>>x;
insertbeg(x);
break;
case 4: deletebeg();
break;
case 5: cout<<"\nEnter the info : ";
cin>>x;
insertend(x);
break;
case 6: deleteend();
break;
case 7: cout<<"\nEnter the node no : ";
cin>>p;
cout<<"\nEnter the info : ";
cin>>x;
insloc(p,x);
break;
case 8: cout<<"\nEnter the node no : ";
cin>>p;
delloc(p);
break;
case 9 :cout<<"\nEnter the element : ";

cin>>x;
search(x);
break;
case 10: display();
break;
default:cout<<"\nWrong choice ";
}
cout<<"\nDo u want to continue(y/n)?";
cin>>ans;
if(ans=='y')
goto back;
}/*end main*/

Example 9 : Program for Searching for an address
#include <iostream.h>
int* location(int[],int,int);
void main()
{ int a[8] = {22, 33, 44, 55, 66, 77, 88, 99}, * p=0, n;
do
{ cin >> n;
if (p == location(a, 8, n)) cout << p << ", " << *p << endl;
else cout << n << " was not found.\n";
} while (n > 0);
}
int* location(int a[], int n, int target)
{ for (int i = 0; i < n; i++)
if (a[i] == target) return &a[i];
return NULL;
}
Example 10 :
#include <iostream.h>
#include <conio.h>
struct COMPUTER
{
char CPUtype[15],CDROM_type[10],mouse_type[15];
char key_board_type[15],SVGA_monitor_make[15];
int SDRAM_size,HDD_size;
float clock_speed,CDROM_speed;
}
comp[50];

void main()
{
int i;
clrscr();
for(i=0;i<50;i++)
{
cout<<"\nEnter CPU tpye, CDROM_type, mouse_type";
cin>>comp[i].CPUtype>>comp[i].CDROM_type>>comp[i].mouse_type;
cout<<"Enter key_board_type, SVGA_monitor_make";
cin>>comp[i].key_board_type>>comp[i].SVGA_monitor_make;
cout<<"Enter SDRAM_size, HDD_size";
cin>>comp[i].SDRAM_size>>comp[i].HDD_size;
cout<<"Enter clock_speed, CDROM_speed";
cin>>comp[i].clock_speed>>comp[i].CDROM_speed;
}
for(i=0;i<50;i++)
{
if(comp[i].HDD_size> 8)
{
cout<<"CPU type is "<<comp[i].CPUtype;
}
}
}

Example 11 :
#include<iostream.h>
#include<string.h>
#include<conio.h>
void swap (char p, char q)
{
char c ;
c=p;
p=q;
q=c;
}
void swap1 (char *p, char *q)
{
char c ;
c = *p ;
*p = *q ;
*q = c ;
}

void perm (char *a, int m, int n)
{
if (m == n)
{
for (int i = 0 ; i < n ; i ++)
cout<<a [ i ] ;
}
else
{
for (int j = n ; j<=m;j++)
{
swap(a[j],a[n]) ;
perm(a,m,n+1);
swap1(&a[j],&a[n]);
}
}
}
void main( )
{
char *a = "India";
perm(a, strlen(a)-1, 0);
getch( );
}

Example 12 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20],DOB[8];
int Marks,SeatNo;
}
s[5];
main()
{
int i,j,temp;
char temp1[20];
for(i=0;i<5;i++)
{

cout<<"\n Enter the name of student : " <<i;
cin>>s[i].name;
cout<<"\n Enter the SeatNo of student : "<<i;
cin>>s[i].SeatNo;
cout<<"\n Enter the DOB of student : "<<i;
cin>>s[i].DOB;
cout<<"\n Enter the Marks of student : "<<i;
cin>>s[i].Marks;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(s[i].SeatNo>s[j].SeatNo)
{
temp=s[j].SeatNo;
s[j].SeatNo=s[i].SeatNo;
s[i].SeatNo=temp;
temp=s[j].Marks;
s[j]. Marks =s[i]. Marks;
s[i]. Marks =temp;
strcpy(temp1,s[j].DOB);
strcpy(s[j].DOB,s[i].DOB);
strcpy(s[i].DOB,temp1);
strcpy(temp1,s[j].name);
strcpy(s[j].name,s[i].name);
strcpy(s[i].name,temp1);
}
}
for(i=0;i<5;i++)
{
cout<<"\n Name : "<< s[i].name<<"\t SeatNo :
"<<s[i].SeatNo<<"\tDOB : "
<<s[i].DOB <<"\t Marks : "<<s[i].Marks;
}
}
getch();
return(0);
}
Example 13 : Function returning a Pointer

#include <iostream.h>
#include <conio.h>
void main ()
{
int *square ( int *a ) ;
int i, *result ;
clrscr () ;
cout<<"i = " ;
cin>>i;
result = square ( &i ) ;
cout<<"square ( i ) : "<<*result ;
}
int *square ( int *a )
{
*a = *a * *a ;
return ( a );
}
Example 14 : Program for integer pointer type
#include <iostream.h>
#include<conio.h>
void main()
{
int i=5;
int *p;
clrscr();
p=&i;
cout<<"Value of i : "<<i;
cout<<"\nValue of i : "<<*p;
cout<<"\nValue of i : "<<*(&i)<<"\n";
cout<<"\nAddress of i : "<<&i;
cout<<"\nAddress of i : "<<p<<"\n";
cout<<"\nValue of p: "<<p;
cout<<"\nValue of p: "<<*(&p);
cout<<"\nAddress of p: "<<&p;
getch();
}

Example 15 : Program for Other Pointer types
#include <iostream.h>
#include <conio.h>
void main ( )
{
char *a, b ;
float *c, d ;
double *e, f ;
clrscr ( ) ;
b = 'x' ;
d = 1.2 ;
f = 100.009 ;
cout<< "\nValue of b : "<< b ;
a = &b ;
cout<< "\nValue of location pointed by a : "<< *a ;
cout<<"\nValue of d : "<< d ;
c = &d ;
cout<<"\nValue of location pointed by c : "<< *c ;
cout<<"\nValue of f v : "<< f ;
e = &f ;
cout<<"\nValue of location pointed by : "<< *e ;
}
Example 16 :
#include <iostream.h>
#include<conio.h>
void main()
{
int i=5,*p,**pp;
clrscr();
p=&i;
pp=&p;
cout<<"Value of i : "<<i;
cout<<"\nValue of i : "<<*p;
cout<<"\nValue of i : "<<*(&i);
cout<<"\nValue of i : "<<**pp;
cout<<"\nValue of i : "<<i<<"\n";

cout<<"\nAddress of i : "<<&i;
cout<<"\nAddress of i : "<<p<<"\n";
cout<<"\nValue of p : "<<p;
cout<<"\nValue of p : "<<*(&p);
cout<<"\nValue of p : "<<*pp<<"\n";
cout<<"\nAddress of p : "<<&p;
cout<<"\nAddress of p : "<<pp<<"\n";
cout<<"\nValue of pp : "<<pp<<"\n";
cout<<"\nAddress of pp : "<<&pp;
getch();
}

Example 17 : Program for Pointers and Array
#include <iostream.h>
#include <conio.h>
void main ()
{
int array[5] = { 11, -2, 30, 41, 15 } ;
int *pointer ;
clrscr () ;
pointer = &array[0] ;
cout<<"The array elements are.....";
while ( pointer < pointer + 5 )
{
cout<<*pointer<<"\n";
pointer++ ;
}
getch () ;
}

Example 18 : Program for Array name as a pointer
#include <iostream.h>
#include <conio.h>
void main ()
{

int array[5] = { 11, -2, 30, 41, 15 } ;
int *pointer ;
clrscr () ;
pointer = array ; /* Array name is used as a pointer */
cout<<"The array elements are...." ;
while ( pointer < pointer + 5 )
{
cout<<*pointer<<endl;
pointer++ ;
}
getch () ;
}

Example 19 : Program for Array name as a pointer - second example
#include <iostream.h>
#include <conio.h>
void main ()
{
void multiply_by_2 ( int a[] ) ;
int array[5] = { 11, -2, 30, 41, 15 } ;
int *start, *end ;
clrscr () ;
multiply_by_2 ( array ) ;/* name of the array is passed to function */
start = array ;
end = array + 5 ;
cout<<"The array elements are....." ;
for ( ; start < end ; start++ )
cout<<*start<<endl;
getch () ;
}
void multiply_by_2 ( int a[] )
{
int i ;
for ( i = 0; i < 5; i++ )
a[i] = a[i] * 2 ;
}
Example 20 :
#include <iostream.h>
#include <conio.h>
void main ()

{
double (*a)(); /* Pointer to Function declaration */
double function () ;
double result ;
clrscr () ;
a = function ; /* Pointer to Function assignment */
result = (*a)() ;

/* Function call using pointer 'a' */

cout<<"Result = "<<result ;
getch () ;
}
double function ()
{
double a, b ;
a = 10.5 ;
b = 20.3 ;
a=a+b;
return ( a ) ;
}
Example 21 : Program for Pointer to Function ( with arguments )
#include <iostream.h>
#include <conio.h>
void main ()
{
double (*a)( double, int ); /* Pointer to Function declaration */
double function ( double a, int b ) ;
double result ;
clrscr () ;
a = function ;

/* Pointer to Function assignment */

result = (*a)( 12.7, 23 ) ; /* Function call using pointer 'a' */
cout<<"Result = "<<result ;
getch () ;
}
double function ( double a, int b )

{
a=a+b;
return ( a ) ;
}

Example 22 : Using a reference as a synonym
#include <iostream.h>
void main()
{
int n=44;
int& rn=n; // r is a synonym for n
cout << "n = " << n << ", rn = " << rn << endl;
--n;
cout << "n = " << n << ", rn = " << rn << endl;
rn *= 2;
cout << "n = " << n << ", rn = " << rn << endl;
}

Example 23 : Program to illustrate use of structure
#include<iostream.h>
#include<conio.h>
void main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1,b2,b3;
cout<<"\nEnter names,prices & no of pages of 3 books";
cin>>b1.name>>b1.price>>b1.pages;
cin>>b2.name>>b2.price>>b2.pages;
cin>>b3.name>>b3.price>>b3.pages;
cout<<"\nAnd this is what you entered";
cout<<"\n"<<b1.name<<"\t"<<b1.price<<"\t"<<b1.pages;
cout<<"\n"<<b2.name<<"\t"<<b2.price<<"\t"<<b2.pages;
cout<<"\n"<<b3.name<<"\t"<<b3.price<<"\t"<<b3.pages;
}

Example 24 : Structure within Structure
#include <iostream.h>
#include <conio.h>
void main()
{
struct date
{
int day ;
int month ;
int year ;
};
struct item
{
char *item_name ;
int price ;
struct date manufacture_date ; /* structure within structure
Declaration */
};
struct item a ;
clrscr () ;
a.item_name = "Soap" ;
a.price = 10 ;
a.manufacture_date.day = 2; /*structure within structure*/
a.manufacture_date.month = 11 ; /* initialisation */
a.manufacture_date.year = 1999 ;
cout<<"Item Details :\n";
cout<<"\nName : "<<a.item_name;
cout<<"\nPrice : "<<a.price;
cout<<"\nDate : "<<a.manufacture_date.day;
cout<<"/"<<a.manufacture_date.month;
cout<<"/"<<a.manufacture_date.year;
getch () ;
}

Example 25 : Program for Structure local to a function

#include <iostream.h>
#include <conio.h>
void main ( )
{
void function ( ) ;
clrscr ( ) ;
function ( ) ;
getch ( ) ;
}
void function ( )
{
struct date
{
int day ;
int month ;
int year ;
};
struct date todays_date = { 22, 6, 2007 } ;
cout<<"Today's Date : ";
cout<<todays_date.day;
cout<<" "<<todays_date.month;
cout<<" "<<todays_date.year;
}

Example 26 : Program for Structure passed to a function ( by Value )
#include <iostream.h>
#include <conio.h>
struct student
{
int student_roll_no ;
int marks_in_subject1 ;
int marks_in_subject2 ;
};
void main ( )
{
void print_structure ( struct student s1 ) ;
float find_average ( struct student s1 ) ;
struct student s1 = { 3032, 89, 78 } ;
clrscr ( );
/* Structure variable passed by value */
print_structure ( s1 ) ;
cout<<"Average Marks = "<<find_average ( s1 ) ;

getch ( ) ;
}
void print_structure ( struct student s1 )
{
cout<<"Student Record : \n";
cout<<"Roll Number : "<<s1.student_roll_no<<endl;
cout<<"Subject 1 marks : "<<s1.marks_in_subject1<<endl;
cout<<"Subject 2 marks : "<<s1.marks_in_subject2<<endl;
}
float find_average ( struct student s1 )
{
float average ;
average = s1.marks_in_subject1 + s1.marks_in_subject2 ;
average = average / 2 ;
return ( average ) ;
}

Example 27 : Function returning a Structure
#include <iostream.h>
#include <conio.h>
struct student
{
int student_roll_no ;
int marks_in_subject1 ;
int marks_in_subject2 ;
};
void main ()
{
struct student function () ;
struct student t ;
float result ;
clrscr () ;
t = function () ;
cout<<"Student Record : \n";
cout<<"Roll Number : "<<t.student_roll_no;
cout<<"Subject 1 marks : "<< t.marks_in_subject1 ;
cout<<"Subject 2 marks : "<< t.marks_in_subject2 ;
getch () ;
}
struct student function ( )
{
struct student s1 = { 3032, 89, 78 } ;
return ( s1 ) ;

}

Example 28 : Program : Array as a member of a Structure
#include <iostream.h>
#include <conio.h>
struct employee
{
char name[10] ;
char address[15] ;
long int telephone_numbers[5] ;
};
void main ( )
{
struct employee input ( ) ;
struct employee e ;
int i ;
clrscr ( ) ;
e = input ( ) ;
cout<<"\n\nEmployee Personal Information displayed... \n";
cout<<"\nName
: "<<e.name ;
cout<<"\nAddress : "<<e.address ;
cout<<"\nTelephone Numbers : ";
for ( i = 0; e.telephone_numbers[i] > 0 && i < 5; i++ )
cout<<"\n"<<e.telephone_numbers[i] ;
getch ( ) ;
}
struct employee input ( )
{
struct employee temp ;
long int no;
int i ;
cout<<"Input the following -\n";
cout<<"Name : ";
cin>>temp.name ;
cout<<"Address : ";
cin>>temp.address;
cout<<"Telephone Numbers :\n";
cout<<"(terminate with -ve number,\n" ;
cout<<"if want to enter less than 5 telephone numbers)\n";
i = 0;

cin>>no ;
/*
User can enter either 5 differrent telephone numbers or less.
If he enters less than 5 telephone numbers then he may terminate by entering
negative number */
while ( no > 0 && i < 4 )
{
temp.telephone_numbers[i] = no ;
i++ ;
//scanf("%ld",&no) ;
cin>>no;
}
temp.telephone_numbers[i] = no ;
return ( temp ) ;
}
Example 29 : Program for Array of Structures
#include <iostream.h>
#include <conio.h>
struct employee
{
char name[10] ;
char address[15] ;
long int telephone_numbers[5] ;
};
void main ( )
{
struct employee input ( ) ;
struct employee e[5] ;
int i, j, n ;
clrscr ( ) ;
cout<<"Enter Number of Employees ( less than 5 ) : ";
cin>>n ;
for ( i = 0; i < n; i++ )
e[i] = input ( ) ;
clrscr ( ) ;
cout<<"\n\nEmployee Personal Information displayed... \n";
for ( i = 0; i < n; i++ )
{
cout<<"\nName
: "<<e[i].name<<endl;
cout<<"Address : "<<e[i].address<<endl;
cout<<"\nTelephone Numbers :\n";
for ( j = 0; e[i].telephone_numbers[j] > 0 && j < 5; j++ )

cout<<e[i].telephone_numbers[j];
}
getch ( ) ;
}
struct employee input ( )
{
struct employee temp ;
long int no;
int i ;
cout<<"Input the following -\n" ;
cout<<"Name : " ;
cin>>temp.name ;
cout<<"Address : ";
cin>>temp.address ;
cout<<"Telephone Numbers : ";
i = 0;
cin>>no;
while ( no > 0 && i < 4 )
{
temp.telephone_numbers[i] = no ;
i++ ;
cin>>no;
}
temp.telephone_numbers[i] = no ;
return ( temp ) ;
}

Example 30 : Array of Students as a function argument
#include <iostream.h>
#include <conio.h>
struct student
{
int roll_no ;
int physics_marks ;
int chemistry_marks ;
float average_marks ;
};
void main ()
{
void find_average ( struct student a[] ) ;
/* array of structure initialisation --

( average_marks are initially set to 0 ) */
struct student s[5] = { { 1, 50, 70, 0 },
{ 2, 67, 89, 0 },
{ 3, 78, 87, 0 },
{ 4, 56, 77, 0 },
{ 5, 49, 45, 0 } } ;
int i ;
clrscr ();
find_average ( s ) ;
cout<<"Student Information --\n\n" ;
for ( i = 0; i < 5; i++ )
{
cout<< "Student Roll No. : "<<s[i].roll_no<<endl ;
cout<< "Physics marks : "<<s[i].physics_marks<<endl;
cout<<"Chemistry marks : "<<s[i].chemistry_marks<<endl;
cout<<"Average marks : "<<s[i].average_marks<<endl;
}
getch ();
}
void find_average ( struct student a[] )
{
int i ;
for ( i = 0; i < 5; i++ )
{
a[i].average_marks = a[i].physics_marks + a[i].chemistry_marks ;
a[i].average_marks /= 2 ;
}
}
Example 31 : Program for Pointer to Structure
#include <iostream.h>
#include <conio.h>
void main ()
{
struct samplr
{
int x ;
int y ;
} a, *b ;
clrscr ( ) ;
a.x = 10 ;
a.y = 20 ;
b = &a;

cout<<"x : " <<(*b).x<<endl;
cout<<"y : "<<(*b).y<<endl ;
cout<<"x : " <<b->x<<endl;
cout<<"y : "<<b->y<<endl ;
getch ( ) ;
}

Example 32 : Array of pointers to structure
#include <iostream.h>
#include <conio.h>
struct sample
{
int a ;
char b ;
};
void main ()
{
/* Array of pointers to structure declaration */
struct sample *array[5] ;
/* structure declaration and initialisation */
struct sample s1 = { 1, 'A' } ;
struct sample s2 = { 2, 'B' } ;
struct sample s3 = { 3, 'C' } ;
struct sample s4 = { 4, 'D' } ;
struct sample s5 = { 5, 'E' } ;
int i ;
clrscr ();
array[0] = &s1;
/* pointer assignment */
array[1] = &s2;
array[2] = &s3;
array[3] = &s4;
array[4] = &s5;
for ( i = 0; i < 5; i++ ) /* accessing through pointers */
{
cout<<"Contents of structure ... "<<i+1 ;
cout<<"a : "<<(*array[i]).a<<endl ;
cout<<"b : "<<(*array[i]).b<<endl ;
}
getch ();
}

Example 33 : Summing an indirect array
#include <iostream.h>
float sum(float*[],int);
void print(float* [], int);
void main()
{
float a[8] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8};
float* p[8];
for (int i = 0; i < 8; i++)
p[i] = &a[i]; // p[i] points to a[i]
print(p, 8);
for (int n=0; n<8; n++)
cout << "sum(p," << n << ") = " << sum(p,n) << endl;
}
void print(float* a[], int n)
{
for (int i = 0; i < n; i++)
cout << *a[i] << " ";
cout << endl;
}
float sum(float* p[], int n)
{
float sum=0.0;
for (int i=0; i<n; i++)
sum += *p[i];
return sum;
}

Example 34 : Program for Use of Union
#include <iostream.h>
#include <conio.h>
void main ( )
{
union sample
/* union declaration */
{
int a ;
char b ;
float c ;
};
union sample s ; /* union variable declaration */
clrscr ( ) ;
/* stored as integer and retrieved as integer */

s.a = 10 ;
cout<<"a : "<<s.a ;
/* stored as character and retrieved as character */
s.b = 'x' ;
cout<<"b : "<<s.b<<endl ;
/* stored as float and retrieved as float */
s.c = 100.34 ;
cout<<"c : "<<s.c<<endl ;
/*stored as float and retrieved as integer-a wrong way*/
s.c = 1.567 ;
cout<<"a : "<<s.a <<endl;
getch ( ) ;
}

Example 35 : Program for Structure in an Union
#include <iostream.h>
#include <conio.h>
void main ( )
{
struct date
{
int day ;
int month ;
int year ;
};
union sample
{
char *sample_date_1 ;
struct date sample_date_2 ; /* structure within union */
};
union sample s ; /* union variable declaration */
clrscr ( ) ;
/* stored as string and retrieved as string */
s.sample_date_1 = "2 Nov 1999" ;
cout<<"Date : "<<s.sample_date_1;
/* stored as structure and retrieved as structure */
s.sample_date_2.day = 2 ;
s.sample_date_2.month = 11 ;
s.sample_date_2.year = 1999 ;
cout<<"Date : "<<s.sample_date_2.day;
cout<<" / "<<s.sample_date_2.month ;

cout<<"/"<<s.sample_date_2.year ;
getch () ;
}

Example 36 : Program for Structure containing Union
#include <iostream.h>
#include <conio.h>
union sample
{
int r1 ;
float r2 ;
};
struct triplet
{
int a ;
int b ;
int flag ;
/* 'flag' is used to indicate the type */
union sample c ; /* of value stored in union 'c' */
};
void main ( )
{
void find_divison ( struct triplet X[ ] ) ;
struct triplet X[5] ;
int i ;
clrscr () ;
cout<<"Input numbers :\n";
for ( i = 0; i < 5; i++ )
{
cin>>X[i].a;
cin>>X[i].b;
}
find_divison ( X ) ;
/* printing the result in correct format 'int' or 'float' */
for ( i = 0; i < 5; i++ )
{
cout<<"\nThe result of divison of "<<X[i].a<<"&"<<X[i].b<<" :
";
if ( X[i].flag == 0 )
cout<<X[i].c.r1;
else
cout<<X[i].c.r2;
}
getch ( ) ;

}
void find_divison ( struct triplet X[] )
{
int i ;
for ( i = 0; i < 5; i++ )
{
int temp ;
if ( X[i].a < X[i].b ) /* Swap if 'a' is less than 'b' */
{
temp = X[i].a ;
X[i].a = X[i].b ;
X[i].b = temp ;
}
if ( ( X[i].a % X[i].b ) != 0 )
{
/* if the result of divison is 'float' */
X[i].flag = 1 ; /* flag = 1, indicates 'float' result */
X[i].c.r2 = (float) X[i].a / (float) X[i].b ;
}
else
{
/* if the result of divison is 'int' */
X[i].flag = 0 ; /* flag = 0, indicates 'int' result */
X[i].c.r1 = X[i].a / X[i].b ;
}
}
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close