Object Oriented Programming Examples

C++ Version

# Constructor 


#include <bits/stdc++.h>
using namespace std;
class timer
{
    int n,i,j;
    clock_t start,end;
public:
    timer();

};
timer::timer()
{
    start=clock();
    for(i=1;i<=10000;i++)
    {
        cout<<i<<endl;
    }
    end=clock();
    cout<<endl;
    cout<<"Time="<<(double)(end-start)/CLOCKS_PER_SEC<<" Seconds"<<endl;
}
int main()
{
    timer ob;
    return 0;

}


#Constructor With Parameter


#include <bits/stdc++.h>
using namespace std;
class gcd
{
 int x,y,r,z;
public:
    gcd(int a,int b);
};
gcd::gcd(int a,int b)
{

    x=a;
    y=b;
    while(y!=0)
    {

        r=x%y;
        x=y;
        y=r;

    }
    cout<<x<<endl;
}
int main()
{
    int p,q;
    cin>>p>>q;
    gcd ob(p,q);
}


# Inheritence

#include <bits/stdc++.h>
using namespace std;
class gcd
{
 int x,y,r,z;
public:
    int gcdx(int a,int b);
};
int gcd::gcdx(int a,int b)
{

    x=a;
    y=b;
    while(y!=0)
    {

        r=x%y;
        x=y;
        y=r;

    }
  return x;
}
class myclass:public gcd
{
    int v,p,mul;
public:
    int lcm(int w,int u);


};
int myclass::lcm(int w,int u)
{
    v=w;
    p=u;
    mul=v*p;
    return (mul/gcdx(v,p));

}

int main()
{
    int m1,m2,i,j,k;
    cin>>m1>>m2;
    myclass ob;
    cout<<"lcm="<<ob.lcm(m1,m2)<<endl;



}


JAVA VERSION

Constructor

(1st class)

package constructor;
import java.util.*;
public class Constructor {

    
    public static void main(String[] args) {
        // TODO code application logic here
        int p,q;
        Scanner input=new Scanner(System.in);
        p=input.nextInt();
        q=input.nextInt();
        gcd ob=new gcd(p,q);
        
    }
    

}

(2nd class)

package constructor;
public class gcd {
    int x,y,r,z;
    gcd(int a, int b)
    {
        x=a;
        y=b;
        while(y!=0)
        {

            r=x%y;
            x=y;
            y=r;

        }
        System.out.println("GCD= "+x);
    }
    

}


Inheritence

1st class


package inheritence;

import java.util.*;
public class Inheritence {

 
    public static void main(String[] args) {
        // TODO code application logic here
        int m1,m2;
        Scanner input=new Scanner(System.in);
        m1=input.nextInt();
        m2=input.nextInt();
        myclass ob=new myclass();
        System.out.println("lcm="+ob.lcm(m1, m2));
    }
    
    
}

(Base Class)

package inheritence;

public class gcd {
    int x,y,r,z;
   int gcdx(int a, int b)
    {
        x=a;
        y=b;
        while(y!=0)
        {

            r=x%y;
            x=y;
            y=r;

        }
        return x;
    }
    

}


(Derived Class)

package inheritence;
public class myclass extends gcd {
    int v,p,mul;
    int lcm (int w,int u)
    {
        v=w;
        p=u;
        mul=v*p;
        
        return (mul/gcdx(v,p));
    }
}

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment