Object Oriented Programming Inheritance
Object Oriented Programming Inheritance
Object Oriented Programming Inheritance
}
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 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++)
{
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;
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";
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*/
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 ;
}
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++)
{
#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();
}
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();
}
{
double (*a)(); /* Pointer to Function declaration */
double function () ;
double result ;
clrscr () ;
a = function ; /* Pointer to Function assignment */
result = (*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 ;
{
a=a+b;
return ( a ) ;
}
#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;
}
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 ) ;
}
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 ) ;
}
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 ( ) ;
}
cout<<"/"<<s.sample_date_2.year ;
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 ;
}
}
}