Sunday, 15 September 2013

PROGRAM TO USE CONSTRUCTORS

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
using namespace std;
class integer
{
    int m,n;
public:
    integer(int,int);   //constructor declared
    void display()
    {
        cout<<endl;
        cout<<"m = "<<m<<"\n";
        cout<<"n = "<<n<<"\n";
    }
};
integer::integer(int a,int b)       //constructor defined
{
    m=a;
    n=b;
}
int main()
{
    integer int1(0,100);    //constuctor called implicitly
    integer int2=integer(25,75);    //constructor called explicitly
    cout<<"\nobject1\n";
    int1.display();
    cout<<"\nobject2\n";
    int2.display();
    return 0;
}

PROGRAM OF FRIEND FUNCTION

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
    int a;
    int b;
    int c;
public:
    void getdata()
    {
        cout<<"enter the value of a \n";
        cin>>a;
        cout<<"enter the value of b \n";
        cin>>b;
        cout<<"enter the value of c \n";
        cin>>c;
    }
    void putdata()
    {
        cout<<"\nthe value of a is  "<<a;
        cout<<"\nthe value of b is  "<<b;
        cout<<"\nthe value of c is  "<<c;
    }
    friend int product(A obj);
};
int product(A obj)
{
    int c;
    c=obj.a*obj.b*obj.c;
    return c;
}
main()
{
    A obj1;
    obj1.getdata();
    obj1.putdata();
    cout<<"\nthe product is "<<product(obj1);2
    return 0;
}

PROGRAM TO IMPLEMENT OPERATOR OVERLOADING

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
using namespace std;
class A
{
    int a;
public:
    A()
    {
        a=0;
    }
    A(int c)
    {
        a=c;
    }
    A operator-()
    {
        A temp;
        temp.a=-a;
        return temp;
    }
    A operator-(A a1)
    {
        A temp;
        temp.a=a-a1.a;
        return temp;
    }
    A operator+(A a1)
    {
        A temp;
        temp.a=a+a1.a;
        return temp;
    }
    A operator*(A a1)
    {
        A temp;
        temp.a=a*a1.a;
        return temp;
    }
    void display()
    {
        cout<<"\nthe value of the object is "<<a;
    }
};
int main()
{
    A a1,a2(2),a3(3),a4(4),a5(5),a6,a7(7);
    a1.display();
    a2.display();
    a3.display();
    a4.display();
    a5.display();
    a6.display();
    cout<<"\n\n";
    a1=a2+a3;
    a6=a4+a5;
    a1.display();
    a6.display();
    return 0;
}