Tuesday, 5 November 2013

PHONEBOOK DIRECTORY

//MADE BY KRISHNA AGARWAL
#include<iostream>
#include<string.h>
using namespace std;
int i=0,j=0;
class phone
{
string num[50];
string name[50];
public:
void addcontact();
void searchcontactbynumber(string a);
void searchcontactbyname(string b);
void showall();
void totalcontacts();
void removecontactbyname(string a);
void removecontactbynumber(string b);
};
void phone::addcontact()
{
cout<<"\nENTER THE NAME : ";
cin>>name[i];
do
    {
        cout<<"ENTER THE NUMBER : ";
        cin>>num[i];
        if(num[i].length()!=10)
            cout<<"\n                  INVALID MOBILE NO. \n     PLEASE ENTER THE VALID NUMBER\n";
    }while(num[i].length()!=10);
i++;
cout<<"\n\tCONTACT ADDED\n ";
}
void phone::searchcontactbynumber(string a)
{
for(j=0;j<i;j++)
{
if(num[j]==a)
{
cout<<"\n"<<name[j];
cout<<"\n"<<num[j];
            break;
}
}
if(j==i)
    {
            cout<<"\n\t\tCONTACT NOT FOUND\n";
    }
}
void phone::searchcontactbyname(string b)
{
for(j=0;j<i;j++)
{
if(name[j]==b)
{
cout<<"\n"<<name[j];
cout<<"\n"<<num[j];
break;
}
}
    if(j==i)
    {
            cout<<"\n\t\tCONTACT NOT FOUND\n";
    }
}
void phone::showall()
{
for(j=0;j<i;j++)
{
cout<<"\n"<<name[j];
cout<<"\n"<<num[j]<<"\n";
}
}
void phone::totalcontacts()
{
cout<<"\nTOTAL CONTACTS IN PHONEBOOK IS  "<<i;
}
void phone::removecontactbyname(string a)
{
for(j=0;j<i;j++)
{
if(name[j]==a)
{
name[j]="CONTACT REMOVED";
num[j]="NUMBER REMOVED";
}
}
}
void phone::removecontactbynumber(string b)
{
for(j=0;j<i;j++)
{
if(num[j]==b)
{
name[j]="CONTACT REMOVED";
num[j]="NUMBER REMOVED";
}
}
}
int main()
{
phone obj;
string nam,num;
int choice;
do
{
cout<<"\n\n\t\tPHONEBOOK";
cout<<"\nPRESS 1 TO ADD CONTACT";
cout<<"\nPRESS 2 TO SEARCH CONTACT BY NAME";
cout<<"\nPRESS 3 TO SEARCH CONTACT BY NUMBER";
cout<<"\nPRESS 4 TO DISPLAY ALL CONTACTS";
cout<<"\nPRESS 5 TO PRINT TOTAL NUMBER OF CONTACTS";
cout<<"\nPRESS 6 TO REMOVE A CONTACT BY NUMBER";
cout<<"\nPRESS 7 TO REMOVE A CONTACT BY NAME";
cout<<"\nPRESS 8 TO EXIT THE PHONEBOOK";
cout<<"\nenter your choice\n";
cin>>choice;
switch(choice)
{
case 1:
obj.addcontact();
break;
case 2:
cout<<"\nENTER THE NAME TO BE SEARCHED : ";
cin>>nam;
obj.searchcontactbyname(nam);
break;
case 3:
cout<<"\nENTER THE NUMBER TO BE SEARCHED : ";
cin>>num;
obj.searchcontactbynumber(num);
break;
case 4:
obj.showall();
break;
case 5:
obj.totalcontacts();
break;
case 6:
cout<<"\nENTER THE NUMBER TO BE REMOVED : ";
cin>>num;
obj.removecontactbynumber(num);
break;
case 7:
cout<<"\nENTER THE NAME TO BE REMOVED : ";
cin>>nam;
obj.removecontactbyname(nam);
break;
case 8:
break;
default :
cout<<"\nPLEASE ENTER THE VALID CHOICE\n";
}
}while(choice!=8);
cout<<"\n\n\n\t\t\tTHANK YOU\n\n";
return 0;
}

Sunday, 15 September 2013

PROGRAM TO USE CONSTRUCTORS

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
using namespace std;
class integer
{
    int m,n;
public:
    integer(int,int);   //constructor declared
    void display()
    {
        cout<<endl;
        cout<<"m = "<<m<<"\n";
        cout<<"n = "<<n<<"\n";
    }
};
integer::integer(int a,int b)       //constructor defined
{
    m=a;
    n=b;
}
int main()
{
    integer int1(0,100);    //constuctor called implicitly
    integer int2=integer(25,75);    //constructor called explicitly
    cout<<"\nobject1\n";
    int1.display();
    cout<<"\nobject2\n";
    int2.display();
    return 0;
}

PROGRAM OF FRIEND FUNCTION

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
    int a;
    int b;
    int c;
public:
    void getdata()
    {
        cout<<"enter the value of a \n";
        cin>>a;
        cout<<"enter the value of b \n";
        cin>>b;
        cout<<"enter the value of c \n";
        cin>>c;
    }
    void putdata()
    {
        cout<<"\nthe value of a is  "<<a;
        cout<<"\nthe value of b is  "<<b;
        cout<<"\nthe value of c is  "<<c;
    }
    friend int product(A obj);
};
int product(A obj)
{
    int c;
    c=obj.a*obj.b*obj.c;
    return c;
}
main()
{
    A obj1;
    obj1.getdata();
    obj1.putdata();
    cout<<"\nthe product is "<<product(obj1);2
    return 0;
}

PROGRAM TO IMPLEMENT OPERATOR OVERLOADING

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
using namespace std;
class A
{
    int a;
public:
    A()
    {
        a=0;
    }
    A(int c)
    {
        a=c;
    }
    A operator-()
    {
        A temp;
        temp.a=-a;
        return temp;
    }
    A operator-(A a1)
    {
        A temp;
        temp.a=a-a1.a;
        return temp;
    }
    A operator+(A a1)
    {
        A temp;
        temp.a=a+a1.a;
        return temp;
    }
    A operator*(A a1)
    {
        A temp;
        temp.a=a*a1.a;
        return temp;
    }
    void display()
    {
        cout<<"\nthe value of the object is "<<a;
    }
};
int main()
{
    A a1,a2(2),a3(3),a4(4),a5(5),a6,a7(7);
    a1.display();
    a2.display();
    a3.display();
    a4.display();
    a5.display();
    a6.display();
    cout<<"\n\n";
    a1=a2+a3;
    a6=a4+a5;
    a1.display();
    a6.display();
    return 0;
}

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();
}