C++4BTech

Embed Size (px)

Citation preview

  • 8/6/2019 C++4BTech

    1/28

    Write a program that uses a structure called Point to model a point. Define three pointsand have the user input value of two of them. Then set third point equal to sum of the

    other two and display the value of the new point.

    #include #include

    struct point{

    int x,y;} p,p1,p2;

    void main(){

    clrscr();cout

  • 8/6/2019 C++4BTech

    2/28

    Create the equivalent of a four function calculator. The program should request the userto enter a number, an operator and another number. It should then carry out the specified

    arithmetical operation adding, subtracting, multiplicating or dividing the number finally.It should display the result. When it finishes the calculation, the program should ask of

    the user wants to do another calculation

    #include

    #include

    void main()

    {clrscr();

    int a, b, result;char ch, op;

    do{

    cout

  • 8/6/2019 C++4BTech

    3/28

    case /:result = a/b;

    cout

  • 8/6/2019 C++4BTech

    4/28

    A Phone number such as 212-767-8900 can be thought of as having 3 parts such as areacode (212), exchange code (767) and the number (8900). WAP that uses a structure to

    store these 3 parts separately. Call the structure variable and type phone int and haveinput a number for the other one there. Display both numbers.

    #include #include

    struct phone

    {int area;

    int ex;int number;

    }ph1, ph2={111,455,3678};

    void main(){

    clrscr();cout

  • 8/6/2019 C++4BTech

    5/28

    Create two classes DM and DB which store the value of distance. DM stores distances inmeters and centimeters and DB in feet and inches. Write a program that can read values

    for the class objects and add one object of DM with another object of DB. Use a friendfunction to carry out the addition operation. The object that stores the results may be a

    DM object or DB object, depending on the units in which the results are required. The

    display should be in the format of feet and inches or meters and centimeters depending onthe object on display.

    #include

    #include

    class DB;class DM

    {float m,cm;

    public :

    void setValue(float a, float b);friend void convert (DM, DB);

    };

    class DB

    {float feet, inch;

    public :

    void setValue(float c, float d);friend void convert (DM, DB);

    };

    void DM :: setValue(float a, float b)

    {m=a;

    cm=b;}

    void DB :: setValue(float c, float d)

    {feet=c;

    inch=d;}

  • 8/6/2019 C++4BTech

    6/28

    void convert(DM p, DB q){

    int ch;float x,y;

    do

    { cout

  • 8/6/2019 C++4BTech

    7/28

    int main(){

    clrscr();

    DM p;

    p.setValue(2.0, 4.5);

    DB q;

    q.setValue(6.0, 8.5);

    convert(p,q);return(0);

    }

  • 8/6/2019 C++4BTech

    8/28

    Consider the following class definitionclass father{

    protected: int age;public:

    father(int x){age=x;}

    virtual void iam(){cout

  • 8/6/2019 C++4BTech

    9/28

    class son : public father{

    protected :int s_age;

    public :son(int x){

    s_age=x;}

    virtual void iam()

    {cout

  • 8/6/2019 C++4BTech

    10/28

    fptr = &f;fptr->iam();

    fptr = &s;

    fptr->iam();

    fptr = &d;fptr->iam();

    getch();

    }

  • 8/6/2019 C++4BTech

    11/28

    Imagine a tollbooth with a class called tollbooth. The two data items are a type unsignedint to hold the total number of cars and a type double to hold the total amount of money

    collected. A constructor initializes both these to 0. A member function callednopayCar() increments the car total but adds nothing to the cash total. Finally a member

    function called display(), displays to the two totals. Include a program to test this class.

    This program should allow the user to push one key to count a paying car and another tocount a nonpaying car. Pushing the Esc Key should cause the program to print out thetotal cars and total ash and then exit.

    #include #include

    classs tollbooth

    {unsigned int total_cars;

    double money;

    public :tollbooth()

    {total_cars = 0;

    money=0;}

    void payingcar(void)

    {total_cars++;

    money = money + 0.50;}

    void nopaycar(void)

    {total_cars++;

    }

    void display(void){

    cout

  • 8/6/2019 C++4BTech

    12/28

    void main(){

    clrscr();tollbooth t;

    int ch;

    cout

  • 8/6/2019 C++4BTech

    13/28

    Create a class rational which represents a numerical value by two double values,

    NUMERATOR and DENOMINATOR. Include the follwing public member functions constructor with no arguments(default) constructor with two arguments

    void reduce() that reduces the rational number by eliminating the highestcommon factor between the numerator and denominator overload + operator to add two rational numbers overload >> operator to enable input through cin overload > (istream &, rational &);friend ostream &operator >> (ostream &, rational &);

    };

    rational rational :: operator +(rational r2){

  • 8/6/2019 C++4BTech

    14/28

    rational r3;r3.numerator = numerator * r2.denominator + denominator * r2.numerator;

    r3.denominator = denominator * r2.denominator;r3.reduce();

    return r3;

    }

    void rational :: reduce(){

    int i,s,fac=1;if(numerator (istream & s, rational r2){

    coutr2.numerator;

    coutr2.denominator;

    return s;}

    ostream & operator >> (ostream & o, rational r2){

    o

  • 8/6/2019 C++4BTech

    15/28

    o

  • 8/6/2019 C++4BTech

    16/28

    Raising a number n to a power p is the same as multiplying n by itself p times. Write aprogram, function called power() that takes a double value. Use a default argument of 2

    for p, so that if this argument is omitted, the number will be squared. Write a main()function that gets values from the user to test the function.

    #include #include

    #include

    double power(double n, int p=2){

    int i;double k=1;

    for(i=1; i

  • 8/6/2019 C++4BTech

    17/28

    Write a function called reverseit() that reverses a string (an array of characters). Use afor loop that swaps the first and last characters, then second and next to last characters

    and so on. The string should be passed to reverseit() as an argument.

    Write a program to exercise reverseit(). The program should get a string from the user,

    call reverseit(), and print out the results. Use an input method that allows embeddedblanks. Test the program with Napoleons famos phase, Able was I ere I Elba

    #include #include

    #include #include

    void reverseit(char s[200])

    { int i, n, k;

    char a;i=strlen(s);

    k=0;for(n=i-1; n>=i/2; n--)

    {a=s[n];

    s[n]=s[k];s[k]=a;

    k++;}

    cout

  • 8/6/2019 C++4BTech

    18/28

    Make a class employee with a name and salary. Make a class manager inherit fromemployee. Add an instance variable named department of type string. Supply a method

    toString that prints the managers name, department and salary. Mame a executiveinherit from manager. Supply a method toString that prints the string Executive

    followed by the information stored in the manager superclass object. Supply a test

    program that tests classes and methods.

    #include #include

    #include

    class employee{

    protected :char name[20];

    int salary;

    public :void getdata()

    {cout

  • 8/6/2019 C++4BTech

    19/28

    cout

  • 8/6/2019 C++4BTech

    20/28

    WAP that creates a binary file by reading the data for the each student from the terminal.The data of each student consists of rollno, name(a string of 60 or lesser number of

    characters) and marks.

    #include #include #include

    #include

    class student

    {char name[60];

    int rollNo;int marks;

    public :

    void getdata(){

    cout

  • 8/6/2019 C++4BTech

    21/28

    //for the number of records to be entered, read the record from console//and write into the file

    for(int i=0; i

  • 8/6/2019 C++4BTech

    22/28

    Create some objects of the string class and put them in Deque, some at the head of theDeque and some at the tail. Display the contents of the Deque using the forEach()

    function and a user written display function. Then search the Deque for a particularstring using the first That() function and display any string that match. Finally remove all

    the items from the Deque using the getLeft() function and display each item. Notice the

    order in which the items are displayed using getLeft(), those interested on the left (head)of the Deque are removed in first in first out order. The opposite would be true ifgetRight() were used.

    A hospital wants to create a database regarding its indoor patients. The information to

    store includes(a) Name of Patient

    (b) Date of Admission(c) Disease

    (d) Date of DischargeCreate a structure to store the data (year, month, and date as its members). Create a base

    class to store the above information. The member function should include function toenter information and display a list of all the patients in the database. Create a derived

    class to store the age of the patients. List the information about all the patients to storethe age of the patients. List the information about all the pediatric patients (less than 12

    years in age)

  • 8/6/2019 C++4BTech

    23/28

    Oracle File

    Create a database and perform following operation:-

    Add records into it Modify a record Generate queries Data operations List all the records

    Create table student (stud_id varchar2(20), stud_name varchar2(20), stud_dept

    varchar2(15));

    Alter table student add (stud_fees varchar2(20));

    Create table Teacher (teach_id varchar2(20), teach_name varchar2(20), teach_subjectvarchar2(20), dept varchar2(15), DOJ date);

    Insert into student values (MTCSE-01, Mandeep, MTech, 58808);

    Insert into student values (MTCSE-02, Sukhdeep, MTech, 58809);

    Insert into student values (BTCSE-03, Nipun, BTCSE, 12000);

    Insert into student values (BTCSE-04, Nirupam, BTCSE, 12000);

    Insert into student values (BTCSE-05, Nirmala, BTCSE, 34111);

    Insert into student values (BTCSE-06, Sujata, BTCSE, 34113);

    Insert into student values (MCA-05, Nirmala, MCA, 1234111);

    Insert into student values (MCA-06, Sujata, MCA, 1234113);

    Insert into teacher values (MCA-01, Sukhvinder, Data Structures, MCA,

    10/10/1998);

    Insert into teacher values (MCA-02, Sandeep, Database Mgt, MCA, 1/1/2000);

    Insert into teacher values (MCA-03, Navdeep, DMS, MCA, 1/4/1999);

    Insert into teacher values (BT-01, Sukhdeep, C, BTCSE, 1/1/2004);

    Insert into teacher values (BT-02, Kuldeep, C++, BTCSE, 5/7/2008);

  • 8/6/2019 C++4BTech

    24/28

    Insert into teacher values (BT-03, Surender, Web Engineering, BTCSE,6/9/2007);

    Select * from student;

    Select stud_id, stud_name from student;

    Select from student where stud_stream = MTech;

    Select stud_name, stud_fees from student where stud_fees = 58808;

    Update student set stud_name = Sukhvinder Singh where stud_name = Mandeep;

    Delete from student where stud_id=MTCSE-01;

    Rename student to Mandeep;

    Drop table Mandeep;

    Arithmetic Operators

    Select stud_fees+500 from student;

    Select stud_fees-300 from student;

    Select stud_fees*2 from student;

    Select stud_fees/2 from student;

    Select stud_name from student where stud_fees = 58808;

    Select stud_name, stud_fees from student where stud_fees > 50000;

    Select stud_name, stud_fees from student where stud_fees 50000;

    Select stud_name, stud_fees from student where stud_name LIKE M%;

    Select stud_name || stud_fees from student;

    Logical Operators

    Select stud_id, stud_name, stud_fees from student where stud_fees > 50000 AND

    stud_name LIKE M%;

    Select stud_id, stud_name, stud_fees from student where stud_fees > 50000 ORstud_name LIKE M%;

  • 8/6/2019 C++4BTech

    25/28

    Select stud_id, stud_name, stud_fees from student where NOT stud_fees < 50000;

    Set Operations

    Intersection

    Select stud_id, stud_name from student INTERSECT select teach_id, teach_name,teach_subject from teacher;

    Union

    Select stud_id, stud_name from student UNION select teach_id, teach_name,

    teach_subject from teacher;

    Minus

    Select stud_name from student MINUS select stud_name from student where stud_fee >50000;

    Other Operators

    INSelect stud_name from student where stud_fee IN (58808, 12000);

    BETWEEN

    Select stud_name from student where stud_fee BETWEEN 58808 AND 12000;

    Functions

    Aggregate Functions

    COUNTSelect count(stud_name) from student;

    SUM

    Select SUM(stud_fees) from student;

    AVERAGESelect AVG(stud_fees) from student;

    MAXIMUM

  • 8/6/2019 C++4BTech

    26/28

    Select MAX(stud_fees) from student;

    MINIMUMSelect MIN(stud_fees) from student;

    Date Functions

    Select add_month(DOJ,1) from teacher;

    Select months_between(SYSDATE, DOJ) from teacher;

    Arithmetic Functions

    Select CEIL(3.7) from tab;

    Select FLOOR(5.2) from tab;

    Select Mod(10,3) from teacher;

    Select Exp(2) from student where stud_fees > 50000;

    Select power(2,3) from student;

    Select sqrt(9) from student;

    Character Functions

    Select chr(65) from student;

    Select concatenate (stud_name, stud_id) from student;

    Select initcap(stud_name) from student;

    Select lower (stud_name) from student;

    Select upper(stud_name) from student;

    Select Lpad(stud_name, 10, #) from student;

    Select Rpad(stud_name, 10, ?) from student;

    Ordering the Records of Database

  • 8/6/2019 C++4BTech

    27/28

    Ascending OrderSELECT * from teacher order by teach_name;

    Descending Order

    SELECT * from teacher order by teach_name desc;

    ViewCREATE view students_details as select * from student where stud_dept IN (select dept

    from teacher where teach_name = Sukhdeep);

    Create a database with data constraints ensuring that tables with the similar names do notexist within the database. Also Add records in it.

    CREATE table Voter(Name char(15) UNIQUE, ID Number(3) PRIMARY KEY, City

    char(8) NOT NULL, Age Number(3) CHECK(Age >= 18), Salary Number(5)DEFAULT 1000);

    Insert into Voter (Mandeep, 1, Panipat, 20, 20000);

    Insert into Voter (Sukhdeep, 2, Karnal, 25, 17000);

    Insert into Voter (Mandeep, 3, Gharaunda, 27, 42000);

    Create table Ward (Ward_Name char(20) Unique, NoOfVoters Number(5) NOT NULL, )

    Generate Queries on table data using:- Arithmetic Operators Logical Operators Aggregate Functions Date Manipulation functions

    Write a program to implement Having and Group By clause

    Write a program to implement Joins and Correlation

    JoinSelect stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJ

    from student, teacher where stud_dept = dept group by dept desc;

    Left Outer Join

  • 8/6/2019 C++4BTech

    28/28

    Select stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJfrom student, teacher where stud_dept = dept(+) group by dept desc;

    Right Outer Join

    Select stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJ

    from student, teacher where stud_dept(+) = dept group by dept desc;

    Generate subqueries to Update and list the records with specific conditions.

    Execute queries using Union, Intersect and Minus clauses.

    Create View on OrderNo, OrderDate, OrderDate, OrderStatus of the Sales_Order and

    ProductNo, ProductRate and QtyOrdered of Sales_Order_Details table.