c++ programs for class 12

Embed Size (px)

Citation preview

  • 7/29/2019 c++ programs for class 12

    1/59

    PPRROOGGRRAAMMSS IINN CC++++2012-2013

    NAME: ALOK KUMAR

    ROLL NO: 1603879

  • 7/29/2019 c++ programs for class 12

    2/59

  • 7/29/2019 c++ programs for class 12

    3/59

    INDEX

    1. Classes and objects

    2. Polymorphism

    3. Constructor destructor

    4. Inheritance

    5. Stacks and queue

    6. Data file management7. Array

    8. SQL queries

  • 7/29/2019 c++ programs for class 12

    4/59

    CLASSES AND OBJECTS

    1. Program to Define a Class Student and accessing

    member function using its object.Define a class student with the following specification:

    Private members:

    Admno integer

    Sname 20 character

    Eng,Math,Science, float

    total float

    ctotal() a function to calculate eng + math + science with

    float return type.

    Public member:

    Takedata() Function to accept values for admnosnameeng

    science and invoke ctotal() to calculate total.

    Showdata() Function to display all the data members onthe

    screen.

    #include

    #include

    #include

    class student

    {

    private:

    intAdmno;

    charSname[20];

  • 7/29/2019 c++ programs for class 12

    5/59

    floatEngMathScience;

    float total;

    floatctotal()

    {

    returnEng+Math+Science;}

    public:

    voidTakedata()

    {

    coutAdmno;

    coutMath>>Science;

    total=ctotal();

    }

    voidShowdata()

    {

    cout

  • 7/29/2019 c++ programs for class 12

    6/59

    2. Program to Define a Class Batsman and accessing

    member function using its object.

    Define a class batsman with the following specifications:

    Private members:

    bcode 4 digits code number

    bname 20 characters

    inningsnotoutruns integer type

    batavg it is calculated according to the formula

    batavg =runs/(innings-notout)

    calcavg() Function to compute batavg

    Public members:

    readdata() Function toaccept value forbcodebname

    inningsnotout and invoke the function calcavg()

    displaydata() Function to display the data members on the

    screen.

    #include

    #include

    #include

    class batsman{

    intbcode;

    charbname[20];

    int inningsnotoutruns;

    intbatavg;

  • 7/29/2019 c++ programs for class 12

    7/59

    voidcalcavg()

    {

    batavg=runs/(innings-notout);

    }

    public :

    voidreaddata ();

    voiddisplaydata();

    };

    void batsman::readdata ()

    {

    coutbcode;

    coutnotout>>runs;

    calcavg();

    }

    void batsman::displaydata()

    {

    cout

  • 7/29/2019 c++ programs for class 12

    8/59

    3. Program to Define a Class Test and accessing member

    function using its object.

    Define a class TEST in C++ with following description:

    Private Members:

    TestCode integer

    Description string

    NoCandidate integer

    CenterReqd integer(number of centers required)

    CALCNTR() function to calculate and return the numberof

    centers as (NoCandidates/100+1)Public Members:

    SCHEDULE()- Function toaccept value from TestCode,

    Description, NoCandidate and invoke the function

    CALCNTR()

    DISPTEST()- Function to display the data members on the

    screen.

    #include

    #include

    #include

    class TEST

    {

    intTestCode;

    char Description[30];

    intNoCandidate;intCenterReqd;

    int CALCNTR()

    {

    returnNoCandidate/100+1;

    }

  • 7/29/2019 c++ programs for class 12

    9/59

    public:

    void SCHDULE();

    void DISPTEST();

    };

    void TEST::SCHDULE()

    {

    coutTestCode;

    cout

  • 7/29/2019 c++ programs for class 12

    10/59

    4. Program to Define a Class BOOK and accessing

    member function using its object.

    Define a class BOOK with the following specifications :

    Private members:

    BOOK NO integer type

    BOOKTITLE string

    PRICE float

    TOTAL_COST() A function to calculate the total cost for Nnumber

    argument.

    Public members:

    INPUT() function to read BOOK_NO. BOOKTITLE,

    PRICE

    PURCHASE() function to ask the user to input the numberof

    copies to be purchased. It invokes

    TOTAL_COST() and prints the total cost tobe

    paid by the user.

    #include

    #include

    #include

    class BOOK

    {

    int BOOKNO;

    char BOOKTITLE[20];

    float PRICE;

    void TOTAL_COST(int N)

  • 7/29/2019 c++ programs for class 12

    11/59

    {

    floattcost;

    tcost=PRICE*N;

    cout

  • 7/29/2019 c++ programs for class 12

    12/59

    5. Define a class Travel in C++ with the description

    given below

    Private Members:

    T_Code string

    No_ of_ Adults integer

    No _of _Children integer

    Distance integer

    TotalFare float

    Public Members:

    constructor to assign initial values as follows:

    TCodeas NULLNo _of_ Adults as 0

    No_ of_Children as 0

    Distance as 0

    TotalFare as 0

    AssignFare() function which calculates and assigns the value of t

    he data member Totalfare as follows For each

    Adult

    Fare(Rs)

    ForKilometers

    500 >=1000

    300=500

    200

  • 7/29/2019 c++ programs for class 12

    13/59

    EnterTour() function to input the values of the data members

    T_Code, No_of_Adults, No_of_Children and

    Distance ; and invoke the AssignFare() function.

    ShowTravel() function which displays the content of all the data

    members for a Travel.

    #include

    #include

    #include

    #include

    class Travel

    {

    charT_Code[21];

    intNo_of_Adults,No_of_Children,Distance;

    float TotalFare;

    public:

    Travel( )

    {strcpy(T_Code,"NULL");

    No_of_Adults=No_of_Children=Distance=

    TotalFare=0;

    }

    voidAssignFare( )

    {

    if(Distance>=1000)

    TotalFare=No_of_Adults*500+No_of_Children* 250;

    else if(Distance>=500)

    TotalFare=No_of_Adults*300+No_of_Children* 150;

    else

    TotalFare=No_of_Adults*200+No_of_Children* 100;

    }

  • 7/29/2019 c++ programs for class 12

    14/59

    voidEnterTravel( )

    {

    cout

  • 7/29/2019 c++ programs for class 12

    15/59

    POLYMORPHISM

    #include

    #include

    float area(float a, float b)

    { return a*b;

    }

    float area(float a)

    { return a*a;

    }

    void main()

    { clrscr();

    int choice;float a,b, ar;

    cout

  • 7/29/2019 c++ programs for class 12

    16/59

    CONSTRUCTOR DISTRUCTURE

    1.Program to calculate factorial of a given numberusing copy constructor.

    #include

    #include

    class copy

    { intvar,fact;

    public:

    copy(int temp)

    {

    var = temp;

    }double calculate()

    {

    fact=1;

    for(int i=1;i

  • 7/29/2019 c++ programs for class 12

    17/59

    2. Program to Calculate Prime Number Using Constructor

    #include

    #include

    class prime

    {

    inta,k,i;

    public:

    prime(int x)

    {

    a=x;}

    void calculate()

    {

    k=1;

    {

    for(i=2;i

  • 7/29/2019 c++ programs for class 12

    18/59

    else

    cout

  • 7/29/2019 c++ programs for class 12

    19/59

    3. Program to print student details using constructor and

    destructor

    #include#include

    classstu

    {

    private:

    char name[20],add[20];

    introll,zip;

    public:stu ( );//Constructor

    ~stu( );//Destructor

    void read( );

    voiddisp( );

    };

    stu :: stu( )

    {

    cout

  • 7/29/2019 c++ programs for class 12

    20/59

    voidstu :: disp( )

    {

    cout

  • 7/29/2019 c++ programs for class 12

    21/59

    4. Program to show constructor overloading#include

    #include

    class temp

    { int a,b;

    public:

    temp(int x, int y);

    temp(int x);

    temp();

    void put();

    };

    temp::temp(int x, int y)

    { a=x;b=y;

    }

    temp::temp(int x)

    { a=x;

    b=0;

    }

    temp::temp()

    { a=0;b=0;

    }

    void temp::put()

    { cout

  • 7/29/2019 c++ programs for class 12

    22/59

    INHERITANCE

    1.Program to find out the student details using multipleinheritance.#include

    #include

    class student

    {

    protected:

    int rno,m1,m2;

    public:

    void get()

    {

    coutrno;

    coutm1>>m2;

    }

    };

    class sports

    {

    protected:

    intsm;// sm = Sports mark

    public:

    voidgetsm()

    {

    coutsm;

    }

    };

  • 7/29/2019 c++ programs for class 12

    23/59

    classstatement:publicstudent,public sports

    {

    inttot,avg;

    public:

    void display(){

    tot=(m1+m2+sm);

    avg=tot/3;

    cout

  • 7/29/2019 c++ programs for class 12

    24/59

    2. Program to find the mean value of a given number

    using friend function.#include

    #includeclassbase

    {

    int val1,val2;

    public:

    void get()

    {

    coutval1>>val2;}

    friend float mean(base ob);

    };

    float mean(base ob)

    {

    return float(ob.val1+ob.val2)/2;

    }

    void main()

    {

    clrscr();

    baseobj;

    obj.get();

    cout

  • 7/29/2019 c++ programs for class 12

    25/59

    STACKS AND QUEUE

    1.Program: Static queue

    #include

    #include

    #include

    class queue

    {

    int data[10];

    int front,rear;

    public:

    queue(){

    front=rear=-1;

    }

    void add();

    void remove();

    void display();

    };

    void queue :: add(){

    if((front==0&&rear==9)||(rear==(front-1)))

    {

    cout

  • 7/29/2019 c++ programs for class 12

    26/59

    else

    rear++;

    data[rear]=x;

    }

    void queue::remove(){

    if(front==-1)

    {

    cout

  • 7/29/2019 c++ programs for class 12

    27/59

    cout

  • 7/29/2019 c++ programs for class 12

    28/59

    2. Program: Queue with class#include

    #include

    #include

    #define NULL 0

    struct queue

    {

    int info;

    queue * next;

    };

    class que{

    private:

    queue * front;

    queue * rear;

    public:

    queue();

    void insert();

    void del();void display();

    };

    que:: que()

    {

    front= rear= NULL;

    }

    void que:: insert()

    {

    queue * temp;

    temp= new queue;

    couttemp->info;

    temp->next=NULL;

  • 7/29/2019 c++ programs for class 12

    29/59

    if(rear== NULL)

    front= rear =temp;

    else

    {

    rear->next=temp;rear=temp;

    }

    }

    void que :: del()

    {

    queue *temp;

    if(front == NULL)

    cout

  • 7/29/2019 c++ programs for class 12

    30/59

    clrscr();

    que q1;

    int ch;

    while (1)

    sert()";cout

  • 7/29/2019 c++ programs for class 12

    31/59

    3. Program: stack-lifo

    #include

    #Include

    #include

    Structemp

    {

    Char name[20];

    Intsal;

    Emp *next;

    };

    Emp * top=0;

    Void push()

    {

    emp *temp;

    temp= new emp;

    couttemp->name>>temp->sal;

    temp->next=0;if(top= =0)

    top=temp;

    else

    {

    temp ->next=top;

    top=temp;

    }

    }

    Void pop()

    {

    emp *temp;

    if(top= =0)

    cout

  • 7/29/2019 c++ programs for class 12

    32/59

    else

    {

    temp=top;

    top=top->next;

    delete temp;}

    }

    Void print()

    {

    if(top!=0)

    for(em *t=top;t;t->next)

    {

    cout

  • 7/29/2019 c++ programs for class 12

    33/59

    4. Program: Based on QUEUE [FIFO]

    #include

    #include

    #define NULL 0

    #include

    structque

    {

    int info;

    que *next;

    };

    que * front=NULL,*rear=NULL;

    void insert(){

    que *temp;

    temp=new que;

    couttemp->info;

    temp->next=NULL;

    if(rear=NULL)

    rear=front=temp;else

    {

    rear->next=temp;

    rear=temp;

    }

    }

    void del()

    { que * temp;

    if(front==NULL)

    cout

  • 7/29/2019 c++ programs for class 12

    34/59

    delete temp;

    }

    else

    { temp=front;

    front=front->next;delete temp;

    }

    }

    void print()

    { if(front==NULL)

    cout

  • 7/29/2019 c++ programs for class 12

    35/59

    5. Program: Stack with class

    #include

    #include

    #include

    # define NULL 0

    struct stack

    {

    int info;

    stack * next;

    };

    classst

    {

    private:

    stack * top;

    public:

    st();

    void pop();

    void push();void display();

    };

    st:: st()

    {

    top= NULL;

    }

    voidst:: push()

    {

    stack * temp;

    temp= new stack;

    couttemp->info;

  • 7/29/2019 c++ programs for class 12

    36/59

    temp->next = NULL;

    if(top== NULL)

    top= temp;

    else

    {temp->next= top;

    top=temp;

    }

    }

    voidst:: pop()

    {

    stack* temp;if(top== NULL)

    cout

  • 7/29/2019 c++ programs for class 12

    37/59

    cout

  • 7/29/2019 c++ programs for class 12

    38/59

    DATA FILE MANAGEMENT

    1.Program to count and display the number ofalphabets present in a text file

    #include

    #include

    #include

    #include

    void display()

    { ifstream afile;

    afile.open("STORY.TXT");

    char ch;

    int c=0;

    while(afile)

    {

    afile.get(ch);

    if (isalpha(ch))

    c++;}

    cout

  • 7/29/2019 c++ programs for class 12

    39/59

    2.Program to count and display the number of blankspaces present in a text file

    #include

    #include

    #include

    #include

    void display()

    {

    ifstream afile;

    afile.open("NOTES.TXT");

    char ch;

    int c = 0;

    while(afile)

    {

    afile.get(ch);

    if (ch == ' ' )

    c++;

    }cout

  • 7/29/2019 c++ programs for class 12

    40/59

    3.Program to calculate the average word size in a textfile.

    #include#include

    void calculate()

    {

    fstream tfile;

    clrscr();

    tfile.open("Report.txt",ios::in);

    char arr[20];

    char ch;

    int i=0,sum=0,n=0;

    while(tfile)

    {

    tfile.get(ch);

    arr[i] = ch;

    i++;

    if (( ch == ' ') || (ch == '.'))

    {I - -;

    sum = sum + i;

    i = 0;

    n++;

    }

    }

    cout

  • 7/29/2019 c++ programs for class 12

    41/59

    4.Program to create the file of employees

    # include

    # include

    # include

    class EMPLOYEE

    {

    int Empno, hra, da, net_sal;

    char Ename[20];

    char add[20];

    float basic;

    public :

    void GETIT()

    {

    coutEmpno;

    coutEname;coutadd;

    coutbasic;

    }

    void CALC()

    {

    hra = (basic * 20) /100;da = (basic * 10) / 100;

    net_sal = basic + da + hra;

    }

    void SHOWIT()

    {

  • 7/29/2019 c++ programs for class 12

    42/59

    CALC();

    cout

  • 7/29/2019 c++ programs for class 12

    43/59

    void main()

    {

    clrscr();

    int n;

    coutn;

    EMPLOYEE emp;

    for (int i = 0;i

  • 7/29/2019 c++ programs for class 12

    44/59

    5.Program to print the frequency of the alphabets and thenumeric digits after reading from the text file.

    # include

    # include

    # include

    void main()

    { char ch, fname[20];

    int num, fre[26], i;

    cout

  • 7/29/2019 c++ programs for class 12

    45/59

    ARRAY

    1.Write a program in C++ to find the maximum sum in a row& a column and the sum of elements in a row & a column.

    #include

    #include

    void main()

    {

    clrscr();

    void read( int [][10], int, int);

    int maxr( int [][10], int, int);

    int maxc( int [][10], int, int);

    void rowsm( int [][10], int, int);

    void colsm( int [][10], int, int);

    void disp( int, int);

    int a[10][10], b, d, r, c;

    coutr>>c;

    read( a, r, c);

    b = maxr(a ,r ,c);d = maxc(a, r ,c);

    rowsm (a ,r, c);

    colsm (a, r, c);

    disp (b, d);

    getch();

    }

    void read(int x[][10], int r, int c)

    {cout

  • 7/29/2019 c++ programs for class 12

    46/59

    int maxr(int x[][10] ,int r, int c)

    {

    int max=0, sum, rn;

    for(int i=0; i

  • 7/29/2019 c++ programs for class 12

    47/59

    for(int i=0; i

  • 7/29/2019 c++ programs for class 12

    48/59

    2. Program related to binary search in simple notation

    assuming the array to be ascending order

    #include#include

    void main()

    {

    int a[10];

    int n,val;

    void readsz(int &);

    void read(int *,int);

    void sort(int *,int);

    void val(int &);

    int bsearch(int * , int, int);

    readsz( n );

    read( a, n );

    sort( a, n );

    val( val );

    cout

  • 7/29/2019 c++ programs for class 12

    49/59

    void sort( int *, int n );

    {

    for(int i=0; i

  • 7/29/2019 c++ programs for class 12

    50/59

    3. Write a C++ program to find the difference of the sum

    of primary upper & lower diagonal.

    #include

    #include

    void main()

    {

    clrscr();

    int a[10][10], ud=0, ld=0, r, i, j;

    coutr;

    cout

  • 7/29/2019 c++ programs for class 12

    51/59

    4. To merge two arrays, one being in ascending, second in

    descending order, and resultant array should be In

    ascending

    #include

    #include

    void main()

    {

    clrscr();

    int a[10], b[10], c[20];

    int m, n;

    coutm;

    cout

  • 7/29/2019 c++ programs for class 12

    52/59

    void sort_asc( int a[], int m)

    {

    for(int i=0; i

  • 7/29/2019 c++ programs for class 12

    53/59

    void merge(int a[], int b[], int c[], int m, int n)

    {

    int i=0, j=n-1, k=0;

    while(i=0){

    if(a[i]=0; --l)

    c[k++]= b[l];

    else

    for(int l=i; i

  • 7/29/2019 c++ programs for class 12

    54/59

    SQL QUERIES

    1.TABLE:- StudentNo. Name Age Department Dateofadm Fee Gender

    1. Pankaj 24 Computer 10/01/97 120 M

    2. Shalini 21 History 24/03/98 200 F

    3. Sanjay 22 Hindi 12/12/96 300 M

    4. Sudha 25 History 01/07/99 400 F

    5. Rakesh 22 Hindi 05/09/97 250 M6. Shakeel 30 History 27/06/97 300 M

    7. Surya 34 Computer 25/02/97 210 M

    8. Shikha 23 Hindi 31/07/97 200 F

    1.Select * FROM Student WHERE Department = History;Output :-

    2. Shalini 21 History 24/03/98 200 F

    4. Sudha 25 History 01/07/99 400 F

    6. Shakeel 30 History 27/06/97 300 M

    2.Select Name FROM Student WHERE Gender = F;Output:-

    2. Shalini 21 History 24/03/98 200 F

    4. Sudha 25 History 01/07/99 400 F

    8. Shikha 23 Hindi 31/07/97 200 F

  • 7/29/2019 c++ programs for class 12

    55/59

    3.Select name FROM Student ORDER BY Dateofadm;Output:-

    4. Select COUNT (*) FROM Student WHERE age > 23;Output :- 4

    5.Select MAX (Age) FROM Student WHERE Gender = F;

    Output :- 25

    Sanjay

    PankajSurya

    Shakeel

    Shikha

    Rakesh

    Shalini

    Sudha

  • 7/29/2019 c++ programs for class 12

    56/59

    2.Table:- HospitalNo. Name Age Department Dateofadm Charg

    es

    Gender

    1. Sandeep 65 Surgery 23/02/98 300 M

    2. Ravina 24 Orthopedic 20/01/98 200 F

    3. Karan 45 Orthopedic 19/02/98 200 M

    4. Tarun 12 Surgery 01/01/98 300 M

    5. Zubin 36 ENT 12/01/98 250 M

    6. Ketaki 16 ENT 24/02/98 300 F

    7. Ankita 29 Cardiology 20/02/98 800 F

    8. Zareen 45 Gynecology 22/02/98 300 F

    9. Kush 19 Cardiology 13/01/98 800 M

    10. Shailya 31 Nuclear

    Medicine

    19/02/98 400 F

    1.Select * FROM Hospital WHERE Department = Cardiology;

    Output :-

    7. Ankita 29 Cardiology 20/02/98 800 F

    9. Kush 19 Cardiology 13/01/98 800 M

    2. Select Name, Charges, Age From Hospital WHERE Gender =

    M;

    Output :-

    Name Charges Age

    Sandeep 300 65

    Karan 200 45

    Tarun 300 12

    Zubin 250 36

  • 7/29/2019 c++ programs for class 12

    57/59

    Kush 800 19

    3. Select COUNT (*) FROM Hospital WHERE Age

  • 7/29/2019 c++ programs for class 12

    58/59

    3.Table :- BooksBook_Id Book_Name Author_Name Publishers Price Type Qty

    C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5

    F0001 The Teasers William

    Hopkins

    First Publ. 650 Fiction 20

    T0001 My First C++ Brian &Brooke

    EPB 350 Text 10

    T0002 C++

    Brainworks

    A. W.

    Rossaine

    TDH 350 Text 15

    F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50

    Table: - Issued

    Book_Id Quantity_Issued

    T0001 4

    C0001 5

    F0001 2

    1. Select * FROM Books WHERE Qty BETWEEN 10 and 50;

    Output :-

    Book_Id Book_Name Author_Name Publishers Price Type Qty

    T0002 C++

    Brainworks

    A. W.

    Rossaine

    TDH 350 Text 15

    F0001 The Teasers WilliamHopkins

    First Publ. 650 Fiction 20

    F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50

  • 7/29/2019 c++ programs for class 12

    59/59

    2.Select * FROM Books WHERE (Publishers = EPB OR

    Publishers = TDH;

    Output:-

    Book_Id Book_Name Author_Name Publishers Price Type Qty

    C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5

    T0001 My First C++ Brian & Brooke EPB 350 Text 10

    T0002 C++

    Brainworks

    A. W. Rossaine TDH 350 Text 15

    3. UPDATE Books SET Price = Price + 50 WHERE Publishers =

    EPB;

    Select * FROM Books WHERE Publishers = EPB;

    Output :-

    Book_Id Book_Name Author_Name Publishers Price Type Qty

    C0001 Fast Cook Lata Kapoor EPB 405 Cookery 5T0001 My First

    C++

    Brian &

    Brooke

    EPB 400 Text 10

    4. Select SUM (Quantity Issued) FROM Issued;

    Output :- 11

    5. Select COUNT (DISTINCT Publishers) FROM Books;