Tuesday, March 26, 2019

C++ with technical akash | Object Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++

passing object as a fun argument

#include<iostream>
using namespace std;
class time
{
    int hour;
    int minute;
public:
    void gettime(int h,int m)
    {
        hour=h;
        minute=m;
    }
    void puttime()
    {
        cout<<hour<<"hours";
        cout<<minute<<"minutes";
    }
    void sum(time,time);
};
void time:: sum(time t1,time t2)
{
    minute=t1.minute+t2.minute;
    hour=minute/60;
    minute=minute%60;
    hour=hour+t1.hour+t2.hour;
}
int main()
{
    time T1,T2,T3;
    T1.gettime(2,45);
    T2.gettime(3,30);
    T3.sum(T1,T2);
    cout<<endl<<"M = ";
    T1.puttime();
    cout<<endl<<"N = ";
    T2.puttime();
    cout<<endl<<"P = ";
    T3.puttime();
    return 0;
}

Friend function

#include<iostream>
using namespace std;
class sample
{
    int a;
    int b;
public:
    void setvalue()
    {
        a=25;
        b=40;
    }
    friend float mean(sample s); //here s object is decleared
};
    float mean(sample s)
    {
        return float(s.a+s.b)/2.0;
    }
    int main()
    {
        sample x;
        x.setvalue(); //here private data is accessed through member function setvalue
        cout<<"mean value=" <<mean(x) <<endl;
        return 0;
    }


//using two class
#include<iostream>
using namespace std;
class A; //forward declearation
class B
{
    int a;
public:
    void setdata (int x)
    {
        a=x;
    }
    friend void fun(A,B);
};
class A
{
    int b;
public:
    void setdata(int y)
    {
        b=y;
    }
    friend void fun(A,B);
};
void fun(A o1,B o2)
{
    cout<<"sum="<<o1.b+o2.a;
}
int main()
{
    A obj1;
    B obj2;
    obj1.setdata(2);
    obj2.setdata(3);
    fun(obj1,obj2);
    return 0;
}

Static Data Member Function :-

#include<iostream>
using namespace std;
class A
{
    static int count;
    int num;
public:
    void getdata(int a1)
    {
        num=a1;
        count ++;
    }
    void getcount ()
    {
        cout<<"count:"<<count<<endl;
    }
};
int A:: count;  //definition of static data
int main()
{
    A a,b,c;
    a.getcount();
    b.getcount();
    c.getcount();
    a.getdata(100);
    b.getdata(200);
    c.getdata(300);
    cout<<"after calling " <<endl;
    a.getcount();
    b.getcount();
    c.getcount();
    return 0;
}

Constructor :-


#include<iostream>
using namespace std;
class a
{
    int m,n;
public:
    a() //constructor
    {
        m=1;
        n=2;
    }
void display()
{
    cout<<"m="<<m<<endl;
    cout<<"n="<<n<<endl;
}
};
int main()
{
    a o;        //object o is created of class a and constructor is called automatically
    o.display();
    return 0;
}



#include<iostream>
using namespace std;
class gce
{
    int m,n;
public:
    gce(int x, int y);  //decleration of constructor
    void display()
    {
        cout<<m<<n<<endl;
    }
    gce()
    {

    }
};
gce :: gce(int x=1,int y=2) //definition of constructor outside class first gce is class name and other is function name
{
    m=x;
    n=y;
}
int main()
{
    //gce g;
    gce g=gce(10,20);
    //gce g(10,20);
    g.display();
    return 0;
}