HOW TO USE RAND( ) IN C ?

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
 1202  View(s)
Ratings:
Rate this:
Hi Expert,

How to use rand( ) in c++?

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to use rand( ) in c ?

    Hi,

    for using rand() you can see following example

    class base{
    public:
    int i;
    base(int x){int x;}
    virtual void func()
    {
    cout<<"Using base version of func():";
    cout<<i<<'\n';
    }
    };
    class derived1: public base{
    public:
    derived1(int x);base(x) { }
    void func()
    {
    cout<<" Using derived1's version of func():";
    cout<<i*i<<'\n';
    }
    };
    class derived2:public base{
    derived2(int x):base(x){ }
    void func()
    {
     cout<<"Using derived2's version of func():";
    cout<<i+i<<'\n';
    }
    };
    int main()
    {
    base *p;
    derived1 d_ob1(10);
    derived2 d_ob2(10);
    int i,j;
    for(i=0;i<10;i++)
    {
    j=rand( );
    if((j%2)) p = &d_ob2;
    p->func( ); 
    }
    return 0;
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!