Welcome To Uday Satya Blog

71-80 PROGRAMS



/*****************************************************
            71.  Program for hierarchical inheritence
******************************************************/
#include
#include
class student
{
  int roll;
public:
  void get_roll(int);
  void put_roll(void);
};
class result1:public student
{
int marks1;
public:
void get_marks1(int);
void show_marks1(void);
};
class result2:public student
{
int marks2;
public:
void get_marks2(int);
void show_marks2(void);
};
void student::get_roll(int x)
{
roll=x;
}
void student::put_roll()
{
cout<
}
void result1::get_marks1(int y)
{
marks1=y;
}
void result1::show_marks1()
{
cout<<"roll no of 1st student is =";
put_roll();
cout<<"\nmarks of 1st student is = "<
}
void result2::get_marks2(int z)
{
marks2=z;
}
void result2::show_marks2()
{
cout<<"rollno of 2nd student is =";
put_roll();
cout<<"\n and the marks of 2nd student is ="<
}

void main()
{
result1 r1;
result2 r2;
clrscr();
r1.get_roll(48);
r1.get_marks1(100);
r1.show_marks1();
r2.get_roll(50);
r2.get_marks2(280);
r2.show_marks2();
getch();
}

OUTPUT:

Roll no of 1st student is =48
Marks of 1st student is  = 100

Roll no of 2nd student is  =50
marks of 2nd student is  =280


/*************************************************
            72.  Program for virtual base class
**************************************************/
#include
#include
class student
{
protected:
  int roll_num;
public:
void get_roll(int a)
{
roll_num=a;
}
void put_roll(void)
{
cout<<"roll number="<
}

};
class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
  cout<<"marks obtained:\n";
  cout<<"part1="<
  cout<<"part2="<
}
};



class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports wt:"<
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_roll();
put_marks();
put_score();
cout<<"total score :"<
}
void main()
{
result student1;
clrscr();
student1.get_roll(48);
student1.get_marks(77.7,83.0);
student1.get_score(67.0);
student1.display();
getch();
}

OUTPUT:

Roll number=48
Marks obtained:
Part1=77.7
Part2=83
Sports wt:67

Total score :227.7





























/*******************************************************************
       73. Using pre processor macro to find square of a number
******************************************************************/


#include
#include
#define square(x) (x*x)
void main()
            {
            int a;
            clrscr();
            cout<<"Enter a"<
            cin>>a;
            int s = square(a);
            cout<<"Square of "<
            getch();
            }


OUTPUT

Enter a
4
Square of 4 is 16
                                                                               













/***************************************************************                                                                               
    74. Using pre processor macro to find greater of 2 no’s
*****************************************************************/
                                                                   
#include
#include
#define great(a,b) a>b?cout<
void main()
            {
            int a,b;
            clrscr();
            cout<<"Enter a,b"<
            cin>>a>>b;
            great(a,b);
            cout<<" is great";
            getch();
            }


OUTPUT

Enter a,b
3
4

4 is great
                                                                               












/******************************************************************                                                                             
    75.Using function template to add 2 ints,2 long ints and 2 floats
*******************************************************************/                                                                             

#include
#include

template
            t add(t a,t b)
                        {
                        return(a+b);
                        }


void main()
            {
            int a,b;
            float c,d;
            long int e,f;
            clrscr();
            cout<<"Enter 2 INTS"<
            cin>>a>>b;
            cout<<"Enter 2 Floats"<
            cin>>c>>d;
            cout<<"Enter 2 LONG INTS"<
            cin>>e>>f;
            cout<<"Sum of 2 INTS "<
            cout<<"Sum of 2 FLOATS "<
            cout<<"Sum of 2 LONG INTS "<
            getch();
            }













OUTPUT

Enter 2 INTS
3
4

Enter 2 Floats
3.3
4.5

Enter 2 LONG INTS
22849503
2326764

Sum of 2 INTS 7
Sum of 2 FLOATS 7.8
Sum of 2 LONG INTS 25176267

















/**************************************************************
             76.Using template to swap 2 ints,2 chas and 2 floats    
***************************************************************/
                                                                           
#include
#include

template
            void swap(t a,t b)
                        {
                        t temp;
                        temp = a;
                        a = b;
                        b = temp;
                        cout<
                        }


void main()
            {
            int a,b;
            float c,d;
            long int e,f;
            clrscr();
            cout<<"Enter 2 INTS"<
            cin>>a>>b;
            cout<<"Enter 2 Floats"<
            cin>>c>>d;
            cout<<"Enter 2 LONG INTS"<
            cin>>e>>f;
            cout<<"Swap of 2 INTS ";
            swap(a,b);
            cout<<"Swap of 2 FLOATS ";
            swap(c,d);
            cout<<"Swap of 2 LONG INTS ";
            swap(e,f);
            getch();
            }




OUTPUT

Enter 2 INTS
3
6

Enter 2 Floats
45.6
21.6

Enter 2 LONG INTS
8772357
9865238


Swap of 2 INTS 6 3
Swap of 2 FLOATS 21.6 45.599998
Swap of 2 LONG INTS 9865238 8772357


















/***********************************************************
77  function template for sorting n ints and n floats
************************************************************/
#include
#include
template
void bubble(T a[],int n)
{
for(int i=0;i
            for(int j=n-1;i
            if(a[j]
            {
            swap(a[j],a[j-1]);   //calling template function
            }
}
template
void swap(X &a,X &b)
{
  X temp=a;
  a=b;
  b=temp;
  }
void main()
{
int x[5]={33,22,77,99,11};
float y[5]={1.2,2.3,7.9,1.8,4.2};
bubble(x,5);//calling template func for ints
bubble(y,5);//calling bubble for floats
cout<<" sorted ints are :\n" ;
for(int i=0;i<5;i++)
cout<
cout<
cout<<" sorted floats are :\n" ;
for(int j=0;j<5;j++)
cout<
cout<
getch();
}



OUTPUT:

sorted ints are :
11 22 33 77 99

 sorted floats are :
1.2  1.8  2.3  4.2  7.9






























/************************************************************
        78.  Program for function template for binary search
**************************************************************/
#include
#include
template
class search
{
T a[10],n,i,mid key,low,high,flag=1;
public:
void get();
void search();
};
template
void search::get()
{
cout<<"enter the number of ele\n";
cin>>n;
cout<<"enter the  elements in sorted order ";
for(i=0;i
{
cin>>a[i];
}
}
template

void search::search()
{
cout<<"Enter elements to be found ";
cin>>key;
low=0;
high=n-1;
mid=(low+high)/2;

if(key
high =mid-1;
else
if(key>a[mid])
key=mid+1;
else if(key==a[mid])
{
cout<<"elements found at"<
flag=0;
}
if(flag)
cout<<"Element not found";
else
cout<<"search successful";
}

void main()
{
search obj1;
search obj2;

cout<<"enter ints";
obj1.get();
obj1.search();

cout<<"enter float"<
obj2.get();
obj2.search();
getch();
}

Output:
Enter the number of ele
4
Enter the elements in sorted order
12
32
93
74
Enter element to be found
74
Element found at 4 place


/***************************************************************
   79. Program for class template by taking single parameter
****************************************************************/
#include
#include

template

class add
{
T a,b,c;

public:
void get()
{
cout<<"enter a and b value\n ";
cin>>a>>b;
}
void sum()
{
c=a+b;
cout<<"\nsum="<
}
};
void main()
{
add obj1;
add obj2;
cout<<"enter  ints"<
obj1.get();
cout<<"enter floats"<
obj2.get();
obj1.sum();
obj2.sum();
getch();
}







OUTPUT:

Enter ints

Enter a and b value
4
2

Enter floats

Enter a and b value
5.3
6.2


Sum=6
Sum=11.5


















/***************************************************************
80.//program for class templates taking multiple parameters
****************************************************************/
#include
#include
template
class test
{
T1 a;
T2 b;
public:
test(T1 x,T2 y)
{
a=x;
b=y;
}
void show()
{
cout<
}};
void main()
{
test obj1(12,0.20);
test obj2('x',"templates");
test obj3('a',3.15);
obj1.show();
obj2.show();
obj3.show();
getch();
}

OUTPUT:
12
0.20
X

Templates
A
3

0 comments:

Post a Comment