HOME

Thursday, October 15, 2015

EXAMPLE FOR PARAMETERIZED CONSTRUCTOR

#include<iostream>
using namespace std;
class sample
{
int p,t,r;
public:
    sample(int x, int y, int z)
    {
        p=x;
        t=y;
        r=z;
    }
    void calculate()
    {
    int result=(p*t*r)/100;
    cout<<"The simple interest is "<<result<<endl;
    }
};
int main()
{
sample s1(500,5,2); //parameterized constructor
s1.calculate();
sample s2(300,6,4);
s2.calculate();
sample s3(s1); //copy of constructor is done
s3.calculate(); //it will print same value as object of s1
return 0;
}

Wednesday, October 7, 2015

SAMPLE PROGRAM FOR PARAMETERIZED CONSTRUCTOR IN C++

#include<iostream>
using namespace std;
class sample
{
    int a,c;
public:
    sample(int l, int b)
    {
        a=l;
        c=b;
    }
void area()
{
    int result=a*c;
    cout<<"area of rectangle is "<<result<<endl;
}
};
int main()
{
    sample s(3,5);
    s.area();
    return 0;
}

SAMPLE PROGRAM FOR CONCEPT OF CONSTRUCTOR IN C++

#include<iostream>
#include<iomanip>
using namespace std;
class sample
{
    int l,b;
public:
    sample()
    {
        l=5;
        b=3;
    }
    void area()
    {
        int result=l*b;
        cout<<"Area of rectangle is"<<setw(15)<<result<<endl;
    }
};
int main()
{
    sample s;
    s.area();
    return 0;
}