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