Tuesday, March 17, 2015

program to demonstrate the typecasting of class type to class type. (C plusplus or C++)

 Aim: Write a program to demonstrate the typecasting of class type to class type.
Ans:
#include<iostream.h>
#include<conio.h>
class SUM
{
int a,b,sum;
public:
int get()
{
cout<<"enter values of a&b:";
cin>>a>>b;
sum=a+b;
return(sum);
}
}obj1;

class SUME
{
int sum;
public:
SUME()
{
}
SUME(SUM obj)
{
sum=obj.get();
}
void s()
{
cout<<"sum of no.s is="<<sum;
}
}obj2;
void main()
{
obj2=obj1;
obj2.s();
}


program to demonstrate the typecasting of class type to basic type. (C plusplus or C++)

Aim: Write a program to demonstrate the typecasting  of class type to basic type.
Ans:

#include<iostream.h>
#include<conio.h>
class  A
{
int x,y;
public:
            A()
            {
                        cout<<"Enter the values which you want to add:\n";
                        cin>>x>>y;
            }
operator  int()
            {
                        int z;
                        z=x+y;
                        return(z);
            }
};
void  main()
{
clrscr();
A o;
int k=o;
cout<<"Sum is:"<<k;
getch();
}


program to demonstrate the typecasting of basic to class type . (C plusplus or C++)

 Aim: Write a program to demonstrate the typecasting  of basic to class type .
Ans:
#include<iostream.h>
#include<conio.h>
class  A
{
public:
int z;
A (int sum)
{
            z=sum;
}
void  show()
{
            cout<<"Sum is:"<<z;
}
};
void  main()
{
int  a,b,sum;
clrscr();
cout<<"Enter the values you want to add:\n";
cin>>a>>b;
sum=a+b;
A o1=sum;
o1.show();
getch();

}

program to demonstrate the overloading of binary memory management operator. (C plusplus or C++)

Aim: Write a program to demonstrate the overloading of binary memory management operator.
Ans:
#include<iostream.h>
#include<conio.h>
void main()
{
//Allocates using new operator eory space in meory for storing a integer datatype
clrscr();
int*a=new int;
*a=100;
cout<<"The Output is:a="<<*a;
//Memory Released using delete operator
delete a;
getch();
}



program to demonstrate the overloading of binary arithmetic operator. (C plusplus or C++)

Aim: Write a program to demonstrate the overloading of binary arithmetic operator.
Ans:

#include<iostream.h>
#include<conio.h>
class  A
{
int x,y;
public:
            void  in()
            {

                        cout<<"Enter real and imaginary parts\n";
                        cin>>x>>y;
            }
A operator  +(A obj)
{
            A T;
            T.x=x +obj.x;
            T.y=y +obj.y;
            return(T);
}
void  show()
{
            cout<<"RESULT IS  "<<x<<"+i"<<y;
}
};
void  main()
{
clrscr();
A o1,o2,o3;
o1.in();
o2.in();
o3=o1+o2;
o3.show();
getch();
}



program to demonstrate the overloading of increment and decrement operator. (C plusplus or C++)

Aim: Write a program to demonstrate the overloading of increment and decrement operator.
Ans:

#include<iostream.h>
#include<conio.h>
class  A
{
int x;
public:
            A()
            {
                        x=40;

            }
            void  operator ++()
            {
            x++;
            }
            void  output()
            {
            cout<<x<<"\n";
            }
            };
void  main()
{
clrscr();
cout<<"Initial value of x=40\n\n";
cout<<"New value of x after using operator overloading is:\n";
A o1,o2;
o1++;
o2++;
o2++;
o1.output();
o2.output();
getch();
}




program to demonstrate the use of initializer list? (C plusplus or C++)

Aim: Write a program to demonstrate the use of initializer list ?
Ans.

#include<iostream.h>
#include<conio.h>
class A
{
const int a,b;
public:
            A(int x,int y):a(x),b(y)
            {

            }
            void d()
            {
            cout<<"a="<<a<<"\n"<<"b="<<b;
            }
}obj(10,34);
void main()
{
            clrscr();
            obj.d();
            getch();
}