MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Friday, September 27, 2013

dinamic initialization of constructor

#include <iostream>

using namespace std;
class student
{
    private:
    int year;
    int amount;
    float total;
    public:
    student(int a,int y,float r=.12);
    student(int a,int y,int d);
    void display();
};
 student::student(int a,int y,float r)
{
    amount=a;
    year=y;
    total=a;
    for(int i=1;i<=year;i++)
    {
        total=total+total*r;
    }
}
 student::student(int a,int y,int d)
{
    amount=a;
    year=y;
    total=a;
    for(int i=1;i<=year;i++)
    {
        total=total+amount*((float)(d)/100);
        amount=total;
    }
}
void student::display()
{
    cout<<"The resultant value is "<<total<<endl;
}
int main()
{
    int a,b,c;
    float f;
    while(cin>>a>>b>>c)
    {
        student ob(a,b,c);
        ob.display();
        cin>>a>>b>>f;
        student ob1(a,b,f);
        ob1.display();
        cin>>a>>b;
        student ob2(a,b);
        ob2.display();
    }
    return 0;
}

using multple constructor of c++ program

#include <iostream>
using namespace std;
class student
{
    private:
    float a;
    float b;
    public:
    student()
    {

    }
    student(float x,float y)
    {
        a=x;
        b=y;
    }
    friend student sum(student,student);
    void display(student);
};
student sum(student c1,student c2)
{
    student c3;
    c3.a=c1.a+c2.a;
    c3.b=c1.b+c2.b;
    return (c3);
}
void student::display(student k)
{
    cout<<"The resultant is="<<k.a<<" + j"<<k.b<<endl;
}
int main()
{
    student ob2[1000],t;
    float x,y,d,z;
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        student ob(x,y);
        cin>>d>>z;
        student ob1(d,z);
        ob2[i]=sum(ob,ob1);
        }
        for(int i=0;i<n;i++)
        {
          t=ob2[i];
          ob2[i].display(t);
        }
    }
    return 0;
}

#include <iostream>
using namespace std;
class student
{
    private:
    float a;
    float b;
    public:
    student()
    {

    }
    student(float x,float y)
    {
        a=x;
        b=y;
    }
    friend student sum(student,student);
    void display(student);
};
student sum(student c1,student c2)
{
    student c3;
    c3.a=c1.a+c2.a;
    c3.b=c1.b+c2.b;
    return (c3);
}
void student::display(student k)
{
    cout<<"The resultant is="<<k.a<<" + j"<<k.b<<endl;
}
int main()
{
    student ob2[1000],t;
    float x,y,d,z;
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        student ob(x,y);
        cin>>d>>z;
        student ob1(d,z);
        ob2[i]=sum(ob,ob1);
        }
        for(int i=0;i<n;i++)
        {
          t=ob2[i];
          ob2[i].display(t);
        }
    }
    return 0;
}