Welcome To Uday Satya Blog

61-70 PROGRAMS



/****************************************************************
      61.To show a friend function being friend to both Classes
*****************************************************************/
#include
#include
class two;
class one
{
              int x;
public:
              void setvalue(int i)
              {
              x=i;
              }
              friend void max(one o,two t);
};
class two
{
              int y;
public:
              void setvalue(int i)
              {
              y=i;
              }
              friend void max(one o,two t);
};
void max(one o,two t)
{
if(o.x==t.y)
cout<<"both the values are same"<
else
if(o.x>t.y)
cout<<"max ="<
else
cout<<"max ="<
}




void main()
{
one obj1;
two obj2;
int n1,n2;
clrscr();
cout<<"enter the two numbers"<
cin>>n1>>n2;
obj1.setvalue(n1);
obj2.setvalue(n2);
max(obj1,obj2);
getch();
}



output:


enter the two numbers
6
34
max =34















/***************************************************************
      62. To overload unary minus using member function
****************************************************************/

#include
#include
class sample
{
int a,b;
public:
void get()
{
cout<<"enter the a,b values"<
cin>>a>>b;
}
void operator -() //unary minus implementation as a member function
{
a=-a;
b=-b;
}
void display()
{
cout<<"a="<
cout<<"b="<
}
};
void main()
{
sample obj;
clrscr();
obj.get();
obj.display();
- obj;//invoking the function
cout<<"after invoking"<
obj.display();
getch();
}



Output:

enter the a,b values

56
32

a=56
b=32

after invoking

a=-56
b=-32























/***************************************************************
     63.  To overload unary minus using friend function
***************************************************************/
#include
#include
class sample
{
int a,b;
public:
void get()
{
cout<<"enter the a,b values"<
cin>>a>>b;
}
friend void operator -(sample &s)  //implementing unary minus using friend
                                   //function
{
s.a= -s.a;
s.b= -s.b;
}
void display()
{
cout<<"a="<
cout<<"b="<
}
};
void main()
{
sample obj;
clrscr();
obj.get();
obj.display();
- obj;//invoking the function
cout<<"after invoking"<
obj.display();
getch();
}



Output:

enter the a,b values

65
63

a=65
b=63

after invoking

a=-65
b=-63
















/*****************************************************************
   
    64. To overload greater than operator using member function
*******************************************************************/
#include
#include
class gr
{
int a;
public:
void input()
{
cin>>a;
}
int operator>(gr g)
{
return(a>g.a);
}};
void main()
{
clrscr();
gr ob1,ob2;
cout<<"enter ob1 value"<
ob1.input();
cout<<"enter ob2 value"<
ob2.input();
int n=ob1>ob2;
if (n==1)
cout<<"ob1 is greater";
else
cout<<"ob2 is greater";
getch();
output:- 
enter ob1 value
20
enter ob2 value
13

ob1 is greater








/***************************************************************
     65.To overload assignment operator using member function
*****************************************************************/
#include
#include
class sample
{
int a,b;
public:
void get()
{
cout<<"enter the a,b values"<
cin>>a>>b;
}
void operator =(sample s)  //implementing as member function
{
a=s.a;
b=s.b;
}
void display()
{
cout<<"a="<
cout<<"b="<
}
};
void main()
{
sample obj1,obj2;
clrscr();
obj1.get();
obj1.display();
obj2.get();
obj2.display();
obj2=obj1;//invoking the function
cout<<"after invoking"<
obj1.display();
obj2.display();
getch();
}



Output:

enter the a,b values

45
65

a=45
b=65

enter the a,b values

68
65

a=68
b=65

after invoking

a=45
b=65

a=45
b=65











/******************************************************************               66.   To overload all arithmetic operator for 2 complex numbers
********************************************************************
#include
#include
class complex
{
int a,b;
public:
void input()
{
cin>>a>>b;
}
complex operator+(complex c)
{
complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}
complex operator-(complex c)
{
complex temp;
temp.a=a-c.a;
temp.b=b-c.b;
return(temp);
}
complex operator*(complex c)
{
complex temp;
temp.a=(a*c.a)-(b*c.b);
temp.b=(b*c.a)+(a*c.b);
return(temp);
}
complex operator/(complex c)
{
complex temp;
float d=(c.a*c.a)+(c.b*c.b);
temp.a=((a*c.a)+(b*c.b))/d;
temp.b=((b*c.a)-(a*c.b))/d;
return(temp);
}
void display()
{
cout<
}};
void main()
{
complex o1,o2,o3;
o1.input();
o2.input();
o3=o1+o2;
o3.display();
o3=o1-o2;
o3.display();
o3=o1*o2;
o3.display();
o3=o1/o2;
o3.display();
getch();
}






 
output:-
enter real and imaginary part
8
12
enter real and imaginary part
2
3

10+i15
 6+i9
-20+i48

  4+i0















/**************************************************************
        67.       For single inheritence by public derivation
**************************************************************/
#include
#include
class base
{
  int a;
public:
  int b;
  void getab();
  int geta(void);
  void show_a(void);
};

class der:public base
{
  int c;
public:
  void mul(void);
  void display(void);
};

void base::getab()
{
cout<<"enter values of a and b\n";
cin>>a>>b;
}
int base::geta(void)
{
return a;
}
void base::show_a(void)
{cout<<"a="<
}
void der::mul(void)
{
c=b*geta();
}
void der::display()
{
cout<<"a="<
cout<<"b="<
cout<<"c="<
}
void main()
{
der d;
d.getab();
d.mul();
d.show_a();
d.display();
cout<<"after changing b value\n";
d.b=20;
d.mul();
d.display();
getch();
}

OUTPUT:

Enter values of a and b
2
6
a=2
a=2
b=6
c=12

after changing b value
a=2
b=20
c=40





/****************************************************
        68. Program for multiple inheritence
*****************************************************/
#include
#include

class M
{
protected:
  int m;
public:
  void get_m(int);
};
class N
{
protected:
  int n;
public:
  void get_n(int);
};

class P:public M,public N
{
public:
  void display(void);
};

void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}

void P::display(void)
{
  cout<<"M ="<
  cout<<"N ="<
  cout<<"M*N ="<
}

void main()
{
P p;
clrscr();
p.get_m(23);
p.get_n(2);
p.display();

getch();
}



OUTPUT:

M =20
N =2
M*N =40

















/**********************************************************
               69.  Program for multilevel inheritence
***********************************************************/
#include
#include

class student
{
protected:
  int roll_number;
public:
  void get_roll(int);
  void put_num(void);
};

void student::get_roll(int a)
{
roll_number=a;
}
void student::put_num()
{
cout<<"roll number :"<
}
class test:public student //first level inheritence

{
protected:
  int marks1,marks2;
public:
  void get_marks(int,int);
  void put_marks(void);
};


void test::get_marks(int x,int y)
{
 marks1=x;
 marks2=y;
 }
void test::put_marks()
{
cout<<"marks in subject1="<
cout<<"marks in subject2="<
}
class result:public test    //second level derivation
{
  float total;
public:
  void display(void);
};
void result::display(void)
{
total=marks1+marks2;
put_num();
put_marks();
cout<<"total="<
}

void main()
{
  result student1;    //object of class result is created
  clrscr();
  student1.get_roll(01);
  student1.get_marks(78,98);
  student1.display();

  getch();
}


OUTPUT:

Roll number:48
Marks in subject1=98
Marks in subject2=91
Total=189


/********************************************************
       70.  Program to implement hybrid inheritence
**********************************************************/
#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: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
{
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(17.5,53.0);
student1.get_score(9.0);
student1.display();
getch();
}



OUTPUT:


Roll number=48
Marks obtained:
Part1=17.5
Part2=53
Sports wt:9

Total score :79.5



0 comments:

Post a Comment