Tuesday, 2 July 2013

FORMATTED CONSOLE INPUT/OUTPUT FUNCTONS

//formatted console input/output functions
// width() to specify the required field size
// for displaying an output value
// precision() to specify the number of digits to
// be displayed after the decimal point
// of the float value
// fill to specify a character that is used to
// fill the unused portion of the field
// setf() to specify format flags that can control the
// form of output display(such as left justi-
// fication and right justification)
//      unsetf() to clear the flags specified
//here is the basic program of defining the field width()
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int items[4]={10,8,12,15};
    int cost[4]={75,100,60,99};
    cout.width(5);
    cout<<"ITEMS";
    cout.width(8);
    cout<<"COST";
    cout.width(15);
    cout<<"TOTAL VALUE"<<endl;
    int sum=0;
    for(int i=0;i<4;i++)
    {
        cout.width(5);
        cout<<items[i];
        cout.width(8);
        cout<<cost[i];
        int value=items[i]*cost[i];
        cout.width(15);
        cout<<value<<"\n";
        sum=sum+value;
    }
    cout<<"\ngrand total = "<<sum;
    getch();
}

PROGRAM TO IMPLEMENT THE VIRTUAL BASE CLASS

//PROGRAM TO IMPLEMENT THE VIRTUAL BASE CLASS
/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
    protected:
    int roll_number;
    public:
    void getroll_number()
    {
        cout<<"please enter the roll no.\n";
        cin>>roll_number;
    }
    void putroll_number()
    {
        cout<<"\nthe roll_number is = "<<roll_number;
    }
};
class test:virtual public student
{
    protected:
    float part1, part2;
    public:
    void get_marks()
    {
        cout<<"\nenter the marks in part 1\n";
        cin>>part1;
        cout<<"\nenter the marks in part2\n";
        cin>>part2;
    }
    void put_marks()
    {
        cout<<"\nthe marks obtained are";
        cout<<"\nthe marks in part1 is = "<<part1;
        cout<<"\nthe marks in part2 is = "<<part2;
    }
};
class sports:public virtual student
{
    protected:
    float score;
    public:
    void get_score()
    {
        cout<<"\nenter the score\n";
        cin>>score;
    }
    void put_score()
    {
        cout<<"\nthe score is = "<<score;
    }
};
class result:public test, public sports
{
    float total;
    public:
    void display()
    {
        total=part1+part2+score;
        putroll_number();
        put_marks();
        put_score();
        cout<<"\ntotal result obtained is = "<<total;
    }
};
//------------------------------------
int main()
{
    result student1;
    student1.getroll_number();
    student1.get_marks();
    student1.get_score();
    student1.display();
    return 0;
}

PROGRAM TO IMPLEMENT HYBRID INHERITENCE

//PROGRAM TO IMPLEMENT HYBRID INHERITENCE
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class student
{
    protected:
    int roll_number;
    public:
    void get_number();
    void put_number();
};
void student::get_number()
{
    cout<<"enter the roll number\n";
    cin>>roll_number;
}
void student::put_number()
{
    cout<<"\nthe roll number is = "<<roll_number;
}
class test:public student
{
    protected:
    float part1, part2;
    public:
    void get_marks();
    void put_marks();
};
void test::get_marks()
{
    cout<<"\nenter the marks of part one and part two\n";
    cin>>part1>>part2;
}
void test::put_marks()
{
    cout<<"\nmarks in part 1 is = "<<part1;
    cout<<"\nmarks in part 2 is = "<<part2;
}
class sports
{
    protected:
    float scores;
    public:
    void get_score()
    {
        cout<<"\nenter the score\n";
        cin>>scores;
    }
    void put_score()
    {
        cout<<"\nthe score is = "<<scores;
    }
};
class result: public test, public sports
{
    float total;
    public:
    void display()
    {
        total=part1+part2+scores;
        put_number();
        put_score();
        put_marks();
        cout<<"\ntotal is = "<<total;
    }
};
void main()
{
    clrscr();
    result student_1;
    student_1.get_number();
    student_1.get_marks();
    student_1.get_score();
    student_1.display();
    getch();
}

PROGRAM TO IMPLEMENT MULTIPLE INHERITENCE

//PROGRAM TO IMPLEMENT MULTIPLE INHERITENCE
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class M
{
    protected:
    int m;
    public:
    void get_m();
};
void M::get_m()
{
    cout<<"enter the value of m\n";
    cin>>m;
}
class N
{
    protected:
    int n;
    public:
    void get_n();
};
void N::get_n()
{
    cout<<"enter the value of n\n";
    cin>>n;
}
class P:public M, public N
{
    public:
    void display();
};
void P::display(void)
{
    cout<<"\nthe value of m is = "<<m;
    cout<<"\nthe value of n is = "<<n;
    cout<<"\nthe value of product is = "<<m*n;
}
void main()
{
    clrscr();
    P p;
    p.get_m();
    p.get_n();
    p.display();
    getch();
}

PROGRAM TO IMPLEMENT MULTILEVEL INHERITENCE

//PROGRAM TO IMPLEMENT MULTILEVEL INHERITENCE
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class student
{
    protected:
    int roll_number;
    public:
    void get_number();
    void put_number();
};
void student::get_number()
{
    int k;
    cout<<"enter the roll number\n";
    cin>>k;
    roll_number=k;
}
void student::put_number()
{
    cout<<"\nroll number = "<<roll_number<<"\n";
}
class test:public student
{
    protected:
    float sub1;
    float sub2;
    public:
    void get_marks();
    void put_marks();
};
void test::get_marks()
{
    float p,q;
    cout<<"enter the marks in two subjects\n";
    cin>>p>>q;
    sub1=p;
    sub2=q;
}
void test::put_marks()
{
    cout<<"\nmarks in sub1 is = "<<sub1<<endl;
    cout<<"marks in sub2 is = "<<sub2<<endl;
}
class result: public test
{
    float total;
    public:
    void display(void);
};
void result::display()
{
    total=sub1 + sub2;
    put_number();
    put_marks();
    cout<<"\ntotal = "<<total<<"\n";
}
//-----------------------------------------
void main()
{
    clrscr();
    result student1;
    student1.get_number();
    student1.get_marks();
    student1.display();
    getch();
}


SINGLE LEVEL INHERITENCE(PRIVATE)

                    //SINGLE LEVEL INHERITENCE(PRIVATE)
//A BASIC PROGRAM OF SINGLE LEVEL INHERITENCE HERE WE HAVE INHERITED
//THE BASE CLASS PRIVATELY . SO THAT THE PUBLIC MEMBERS OF THE BASE CLASS BECOME
//THE PRIVATE MEMBERS OF THE DERIVED CLASS
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class B
{
    int a;
    public:
    int b;
    void get_ab();
    int get_a(void);
    void show_a(void);
};
class D:private B
{
    int c;
    public:
    void mul(void);
    void display(void);
};
//---------------------------------------------
void B::get_ab(void)
{
    cout<<"\nenter the values of a and b\n";
    cin>>a>>b;
}
int B::get_a()
{
    return a;
}
void B::show_a()
{
    cout<<"a = "<<a<<"\n";
}
void D::mul()
{
    get_ab();
    c=b*get_a();
}
void D::display()
{
    show_a();
    cout<<" b = "<<b<<"\n";
    cout<<" c = "<<c<<"\n";
}
//----------------------------------------------
void main()
{
    D d;
    //d.get_ab(); won't work
    d.mul();
    //d.show_a(); won't work
    d.display();
    // d.b=20;    won't work
    d.mul();
    d.display();
    getch();
}

SINGLE LEVEL INHERITENCE(PUBLIC)

  //SINGLE LEVEL INHERITENCE(PUBLIC)
//A BASIC PROGRAM OF SINGLE LEVEL INHERITENCE HERE WE HAVE INHERITED
//THE BASE CLASS PUBLICALY . SO THAT THE PUBLIC MEMBERS OF THE BASE CLASS BECOME
//THE PUBLIC MEMBERS OF THE DERIVED CLASS
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class B
{
    int a; //private; not inheritable
    public:
    int b; //public; ready for inheritence
    void set_ab();
    int get_a(void);
    void show_a(void);
};
class D:public B   //public declaration
{
    int c;
    public:
    void mul(void);
    void display(void);
};
//HERE THE CLASS B ENDS AND THE CLASS B BEGINS
void B::set_ab(void)
{
    a=5;
    b=10;
}
int B::get_a()
{
    return a;
}
void B::show_a()
{
    cout<<"a =  "<<a<<"\n";
}
void D::mul()
{
    c=b*get_a();
}
void D::display()
{
    cout<<"a = "<<get_a()<<"\n";
    cout<<"b = "<<b<<"\n";
    cout<<"c = "<<c<<"\n";
}
//-------------------------------------------------
//HERE THE MAIN BEGINS
void main()
{
    clrscr();
    D d;
    d.set_ab();
    d.mul();
    d.show_a();
    d.display();
    d.b=20;
    d.mul();
    d.display();
    getch();
}

PROGRAM USING STATIC DATA MEMBER

//PROGRAM USING STATIC DATA MEMBER
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class test
{
    int code;
    static int count;
    public:
    void setcode(void)
    {
        code=++count;
    }
    void showcode(void)
    {
        cout<<"object number : "<<code<<"\n";
    }
    static void showcount(void)
    {
        cout<<" count : "<<count<<"\n";
    }
};
int test::count;
void main()
{
    clrscr();
    test t1,t2;
    t1.setcode();
    t2.setcode();
    test::showcount();
    test t3;
    t3.setcode();
    test::showcount();
    t1.showcode();
    t2.showcode();
    t3.showcode();
    getch();
}

PROGRAM OF STATIC CLASS MEMBER FUNCTION

//STUDY OF STATIC CLASS MEMBER FUNCTION
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class item
{
    static int count;
    int number;
    public:
    void getdata(int a)
    {
        number=a;
        count++;
    }
    void getcount(void)
    {
        cout<<" count\t"<<count<<"\n";
    }
};
int item::count;
void main()
{
    clrscr();
    item a,b,c;
    a.getcount();
    b.getcount();
    c.getcount();
    a.getdata(100);
    b.getdata(200);
    c.getdata(300);
    cout<<"after reading data\n";
    a.getcount();
    b.getcount();
    c.getcount();
    getch();
}

PROGRAM FOR THE SHOOPING LIST USING OOPS

//PROGRAM FOR THE SHOOPING LIST
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
const m=50;                                    //HERE THE CLASS BEGINS
class items
{
    int itemcode[m];
    float itemprice[m];
    int count;
    public:
    void cnt(void)
    {
        count=0;
    }
    void getitem(void);
    void displaysum(void);
    void remove(void);
    void displayitems(void);
};                                          //here the class ends
void items::getitem(void)
{
    cout<<"enter the item code\n";
    cin>>itemcode[count];
    cout<<"\nenter the item price\n";
    cin>>itemprice[count];
    count++;
}
void items::displaysum(void)
{
    float sum=0;
    for(int i=0;i<count;i++)
    sum+=itemprice[i];
    cout<<"\ntotal value\t"<<sum<<"\n";
}
void items::remove(void)
{
    int a;
    cout<<"\nenter the item code \n";
    cin>>a;
    for(int i=0;i<count;i++)
    if(itemcode[i]==a)
    itemprice[i]=0;
}
void items::displayitems(void)
{
    cout<<"\ncode\tprice\n";
    for(int i=0;i<count;i++)
    cout<<itemcode[i]<<"\t"<<itemprice[i]<<"\n";
}
//---------------------------------------------
void main()                    //HETE THE MAIN BEGINS
{
    clrscr();
    items order;
    order.cnt();
    int x;
    do       //do.while loop
    {
        cout<<"\nyou can do the following\n";
        cout<<"enter the appropriate number\n";
        cout<<"1: add an item\n";
        cout<<"2: display total value\n";
        cout<<"3: delete an item\n";
        cout<<"4: display all items\n";
        cout<<"5: quit\n";
        cout<<"what us your option \n";
        cin>>x;
        switch(x)                                       //switch begins
        {
            case 1: order.getitem(); break;
            case 2: order.displaysum(); break;
            case 3: order.remove(); break;
            case 4: order.displayitems(); break;
            case 5: break;
            default : cout<<"\nerror in input; try again\n";
        }                               //SWITCH ENDS
    }while(x!=5); //DO WHILE ENDS
} //MAIN ENDS HERE

PROGRAM TO FIND THE 1s COMPLEMENT OF THE ENTERED BINARY NUMBER

//PROGRAM TO FIND THE 1s COMPLEMENT OF THE ENTERED BINARY NUMBER
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class binary
{
char s[30];
public:
void read(void)
{
    cout<<"enter a binary number\n";
    cin>>s;
}
void ckh_bin(void)
{
    for(int i=0;s[i]!=0;i++)
    {
        if(s[i]!=0&&s[i]!=0)
        {
            cout<<"\nincorrect binary format....program is quit\n";
            getch();
            exit(0);
}
}
}
void ones(void)
{
    void chk_bin(); //calling member function
    for(int i=0;s[i]!=0;i++)
    {
        if(s[i]=='0')
        s[i]='1';
        else
        s[i]='0';
    }
}
void displayones(void)
{
    ones(); //calling member function
    cout<<"\nthe 1s complement of the above binary number is \n"<<s;
}
}; //class end bracket
void main()
{
    clrscr();
    binary b;
    b.read();
    b.displayones();
    getch();
}

SIMPLE CLASS PROGRAM

//SIMPLE CLASS PROGRAM
/*MADE BY KRISHNA AGARWAL*/
#include<iostream.h>
#include<conio.h>
class item
{
    int number;
    float cost;
    public:
    void getdata(int a, float b);
    void putdata(void)
    {
        cout<<"mumber\t"<<number<<"\n";
        cout<<"cost\t"<<cost;
    }
};
void item::getdata(int a , float b)
{
    number=a;
    cost=b;
}
void main()
{
    clrscr();
    item x;
    cout<<"\nobject x "<<"\n";
    x.getdata(100,299.95);
    x.putdata();
    item y ;
    cout<<"\nobject y "<<"\n";
    y.getdata(200,175.50);
    y.putdata();
    getch();
}