Top Banner

of 88

C++ Practical File Complete

Apr 06, 2018

Download

Documents

Kevin Alluvalia
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/3/2019 C++ Practical File Complete

    1/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

  • 8/3/2019 C++ Practical File Complete

    2/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM -1: WAP in C++ to find the factorial of a number using functions.

    INPUT: Enter any number.

    EXPLAINATION: Here in this program we declare two variables n and fact. To find the factorial of

    a number we use data type i.e, unsigned int .

    SOURCE CODE:

    #include

    #include

    int main() //beginning of main f()

    {

    unsigned int n,fact; //variable declaration

    unsigned int factorial(unsigned int); //function declaration

    cout

  • 8/3/2019 C++ Practical File Complete

    3/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    4/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-2: WAP in C++ to find the volume of cube using functions.

    INPUT : Enter any number.

    EXPLAINATION: Here in this program we declare two variables a,b and one function i.e,cube . To find

    the cube of a number we use formula i.e,cube=side*side*side .

    SOURCE CODE:

    #include

    #include

    int main() //begining of main f()

    {

    int a,b; //declaration of variables

    int cube(int); //declaration of function

    cout

  • 8/3/2019 C++ Practical File Complete

    5/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    6/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-3: WAP in C++ to swap two numbers using call by value.

    INPUT : Enter the value of a and b.

    EXPLAINATION: Swap means to interchange the values In this program we declare two variables a and

    b. To swap two numbers we take another temperory variable i.e, z in user defined function.

    SOURCE CODE:

    #include

    #include

    int main() //beginning of main f()

    {

    int a,b; //variable declaration

    void swap(int,int); //function declaration

    cout

  • 8/3/2019 C++ Practical File Complete

    7/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    y=z;

    cout

  • 8/3/2019 C++ Practical File Complete

    8/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-4: WAP in C++ to swap two numbers using call by reference.

    INPUT : Enter the value of a and b.

    EXPLAINATION: Refenence means address(&) .In this program we declare two variables a and b. To

    swap two numbers by call by reference we take another temperory variable i.e, z in user defined f().

    SOURCE CODE:

    #include

    #include

    int main() //begining of main f()

    {

    int a,b; //variable declaration

    void swap(int&,int&); //function declaration

    cout

  • 8/3/2019 C++ Practical File Complete

    9/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    y=z;

    cout

  • 8/3/2019 C++ Practical File Complete

    10/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-5: WAP in C++ to find the simple interest using the concept of default arguments.

    EXPLAINATION: In this program we declare variable nand function siand apply the formula of simple

    interest i.e,S.I=(p*r*t)/100 in user defined f().

    SOURCE CODE:

    #include

    #include

    int main() //beginning of main f()

    {

    float n; //variable declaration

    float si(float, float, float t=5); //function declaration

    cout

  • 8/3/2019 C++ Practical File Complete

    11/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    12/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-6: Wap to enter the name ,age and percentage.

    INPUT : Enter any name, age and percentage.

    EXPLAINATION:Structure is a collection of dissimiliar type of data elements.In this program the

    structure name is student .We declare three variable i.e, name, age and per .We have to enter

    the name ,age and per of two students.

    SOURCE CODE:

    #include

    #include

    struct student //name of the structure

    {

    char name[20];

    int age;

    float per;

    }s1,s2;

    int main() //beginning of main f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    13/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cin>>s2.name;

    couts2.age;

    couts2.per;

    cout

  • 8/3/2019 C++ Practical File Complete

    14/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-7: Wap to demonstrate the concept of data abstraction.

    INPUT : Enter the value of a and b.

    EXPLAINATION: Data abstraction means to know about the essential features by by hiding the

    background details. In this program the class name is ABC .We declare three variables a,b,c and

    member functions read, process and show.We have to add two numbers. In main f() we create

    one object i.e,o1

    SOURCE CODE:

    #include

    #include

    class ABC //name of class

    {

    int a,b,c; //data member

    public:

    void read() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    15/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cout

  • 8/3/2019 C++ Practical File Complete

    16/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-8: Wap to demonstrate the concept of dataencapsulation.

    INPUT : Enter the value of a and b.

    EXPLAINATION: Data encapsulation means the wrapping up of data and functions in a single unit(called

    class). In this program the class name is ABC .We declare three data members a,b,c and member

    functions read, process and show.We have to add two numbers. In main f() we create one object i.e,o1

    SOURCE CODE:

    #include

    #include

    class ABC //name of class

    {

    int a,b,c; //data member

    public:

    void read() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    17/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cout

  • 8/3/2019 C++ Practical File Complete

    18/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-9: Wap to demonstrate the concept of classes and objects.

    INPUT : Enter any number.

    EXPLAINATION: In this program the class name is ABC .We declare two data members a,b and member

    functions read and process.We have to show greater between two numbers. In main f() we create one

    object i.e,o1

    SOURCE CODE:

    #include

    #include

    class ABC //name of class

    {

    int a,b; //data member

    public:

    void read() //member function

    {

    couta; //standard input

    coutb; //standard input

    }

    void show() //member f()

    {

    If(a>b) //if loop

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    19/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    20/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    21/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-10: WAP in C++ to demonstrate the concept of creating objects.

    INPUT : Enter any number.

    EXPLAINATION: In this program the class name is ABC .We declare data member i.e, n and member

    functions read and process.We have to show whether the number is even or odd. In main f() we create

    two objects i.e,o1 and o2

    SOURCE CODE:

    #include

    #include

    class ABC //name of class

    {

    int n; //data member

    public: //access level

    void read() //member function

    {

    coutn; //standard input

    }

    void show() //member f()

    {

    if(n%2==0) //if loop

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    22/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    }

    };

    int main() //beginning of main f()

    {

    ABC o1,o2; //creation of object

    cout

  • 8/3/2019 C++ Practical File Complete

    23/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-11: WAP in C++ to access the data member and member function.

    INPUT : Enter any value of a.

    EXPLAINATION:Here class name is ABC,data members are a&b, member functions are read, process and

    show. We have to find the square of a number.

    SOURCE CODE:

    #include

    #include

    class ABC //name of class

    {

    int a,b; //data member

    public:

    void read() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    24/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    };

    int main() //begining of main f()

    {

    ABC o1; //creation of object

    o1.read(); //invoke member f()

    o1.process();

    o1.show();

    return 0; //return type

    } //end of program

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    25/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-12: WAP in C++ to demonstrate the concept of array of object.

    INPUT : Enter any value of five item nos and price.

    EXPLAINATION:Array is a collection of similar type of data elements.In this program the class name is

    item.We declare data member i.e,itemno &price and member function read&show .We have to show

    item no.and price of five items. In main f() we take array of object o1[5].

    SOURCE CODE:

    #include

    #include

    class item //name of class

    {

    int itemno; //data members

    float price;

    public:

    void read(int i,float p) //member f()

    {

    itemno=i;

    price=p;

    }

    void show() //member f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    26/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    int itno;

    float pri;

    item o1[5]; //array of object

    cout

  • 8/3/2019 C++ Practical File Complete

    27/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    28/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-13: WAP in C++ to add two diff. minutes and hours using object as f() argument.

    INPUT : Enter any value of hours and minutes.

    EXPLAINATION: Here the class name is time, data member are hours and minutes, member functions

    are read &show. In this program we have to pass object as argument in place of values.

    SOURCE CODE:

    #include

    #include

    class time //name of the class

    {

    int hours; //variable declaration

    int minutes;

    public: //member function

    void read(int h,int m) //temporary variable/argument

    {

    hours=h;

    minutes=m;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    29/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    hours=t1.hours+t2.hours;

    minutes=t1.minutes+t2.minutes;

    h1=minutes/60;

    m1=minutes%60;

    hours=hours+h1;

    minutes=m1;

    }

    int main() //begining of main f()

    {

    time t1,t2,t3; //creation of object

    cout

  • 8/3/2019 C++ Practical File Complete

    30/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    31/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-14: WAP in C++ to add two english distances using object as f() argument.

    INPUT : Enter any value of hours and minutes.

    EXPLAINATION: Here the class name is distance, data members are feet and inches, member functions

    are read &show. In this program we have to pass object as argument in place of values.

    SOURCE CODE:

    #include

    #include

    class distance //name of the class

    {

    int feet; //variable declaration

    int inches;

    public: //member function

    void read(int i,int j) //temporary variable/argument

    {

    feet=i;

    inches=j;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    32/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    feet=d1.feet+d2.feet;

    inches=d1.inches+d2.inches;

    f1=inches/12;

    inc1=feet%12;

    feet=feet+f1;

    inches=inc1;

    }

    int main() //begining of main f()

    {

    distance d1,d2,d3; //creation of object

    cout

  • 8/3/2019 C++ Practical File Complete

    33/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    34/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-15: WAP in C++ to demonstrate the concept of inline function.

    INPUT : Enter any value of a & b.

    EXPLAINATION:Inline functions are used to remove the overheads by pasting the calculation

    in every function.Here in this program we take abc as class name ,two access

    levels private & public ,two member f()s as read & show .

    SOURCE CODE:

    #include

    #include

    class abc //name of class

    {

    int a,b,c; //data member

    public: //access level

    void read() //member f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    35/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cout

  • 8/3/2019 C++ Practical File Complete

    36/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-16: WAP in C++ to demonstrate the defination of member f()'s of a class.

    INPUT : Enter any value of a & b.

    EXPLAINATION:Here in this program we have to add two numbers. Taking add as class name ,two

    access levels private & public ,two member f()s as read & show .

    SOURCE CODE:

    #include

    #include

    class add //name of class

    {

    int a,b,c; //variable declaration

    public:

    void input(); //member function declaration

    void process();

    void output();

    };

    void add::input() //member f() defination

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    37/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    }

    void add::output() //member f() defination

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    38/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-17: WAP in C++ to demonstrate the concept of friend function.

    INPUT : Enter any value of a & b.

    EXPLAINATION: Friend function are used to access the data member.In this program the class name is

    abc.We declare data member i.e, a & b and member function read .We have to add two numbers. In

    main f() we create one objects i.e,o1.

    SOURCE CODE:

    #include

    #include

    class abc //name of class

    {

    int a,b; //data member

    public: //access level

    void read() //member funnction

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    39/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    int main() //beginning of main()

    {

    int c; //declaration

    abc o1; //creation of object

    o1.read(); //invoke M F()

    c=func(o1); //function call

    cout

  • 8/3/2019 C++ Practical File Complete

    40/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-18: WAP in C++ to demonstrate the concept of returning object.

    INPUT : Enter any two feet and inches.

    EXPLAINATION: Here the class name is distance, data members are feet and inches, member functions

    are read &show. In this program we have to pass object as argument in place of values and returning

    object.

    SOURCE CODE:

    #include

    #include

    class distance //name of class

    {

    int feet; //data member

    int inches;

    public:

    void read(int f, int i) //member f()

    {

    feet=f;

    inches=i;

    }

    void show() //member f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    41/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    distance dd3;

    dd3.feet=dd1.feet+dd2.feet;

    dd3.inches=dd1.inches+dd2.inches;

    if(dd3.inches>=12)

    {

    dd3.feet++;

    dd3.inches=dd3.inches-12;

    }

    return dd3; //returning object

    }

    int main() //begining of main()

    {

    distance d1,d2,d3; //creation of object

    cout

  • 8/3/2019 C++ Practical File Complete

    42/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    43/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-19: WAP in C++ to demonstrate the concept of default and parameterised constructor.

    EXPLAINATION:Constructor are special type of member functions. Constructors are always public. Here

    the class name is abc,one default constructor,one parameterised constructor we have just to show the

    values.

    SOURCE CODE:

    #include

    #include

    class abc //name of class

    {

    int a,b; //data member

    public: //access level

    abc() //default constructor

    {

    a=4;

    b=3;

    }

    abc(int i,int j) //parameterised constructor

    {

    a=i;

    b=j;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    44/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    };

    int main() //beginning of main f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    45/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-20: WAP in C++ to demonstrate the concept of destructors.

    EXPLAINATION: Destructors are also special type of member functions. Here the class name is alpha,

    Constructor nameis alpha() and destructor name is ~alpha().

    SOURCE CODE:

    #include

    #include

    int count=0;

    class alpha //name of class

    {

    public:

    alpha() //constructor

    {

    count++; //increment

    cout

  • 8/3/2019 C++ Practical File Complete

    46/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    alpha a2;

    //destructor invoke automatically

    return 0; //return type

    } //end of program

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    47/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-21: WAP to count the no. of object in a program by using static data member and static

    member f().

    EXPLAINATION: Static member functions are those functions which contain only static data members

    and static member functions . Here in this program we have to count the no. of objects in a program.

    SOURCE CODE:

    #include

    #include

    class x //name of class

    {

    int itemno; //data member

    float price;

    static int count; //static data member

    public: //access level

    void read(int i,float f) //member function

    {

    itemno=i;

    price=f;

    count++;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    48/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cout

  • 8/3/2019 C++ Practical File Complete

    49/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    50/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-22: WAP in c++ to demonstrate the concept of copy constructor.

    EXPLAINATION: Copy constructor copies values from main function to member function.Here the class

    name is abc.In this program we use three constructors one is default constructor,second is

    parameterised constructor and third is copy constructor.

    SOURCE CODE:

    #include

    #include

    class abc //name of class

    {

    int i,j; //variable declaration

    public: //member f()

    abc() //default constructor

    {

    i=5;

    j=4;

    }

    abc(int a, int b) //parameterised constructor

    {

    i=a;

    j=b;

    }

    abc(abc&s) //copy constructor

    {

    i=s.i;

    j=s.j;

    }

  • 8/3/2019 C++ Practical File Complete

    51/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    void display() //member f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    52/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    53/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-23: WAP in c++ to demonstrate the concept of single inheritance.

    INPUT : Enter any two numbers.

    EXPLAINATION: When a derived class inherits the features of base class,it is single inheritance. Here we

    take two classes one is base and other is derived class.We have to add two numbers.

    SOURCE CODE:

    #include

    #include

    class base //name of class

    {

    private: //access level

    int a; //data member

    protected:

    int b;

    public:

    void read() //member function

    {

    couta; //standard input

    }

    void get()

    {

    coutb;

    }

    void show()

    {

  • 8/3/2019 C++ Practical File Complete

    54/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cout

  • 8/3/2019 C++ Practical File Complete

    55/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    }

    };

    int main() //beginning of main f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    56/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-24: WAP in c++ to demonstrate the concept of multiple inheritance.

    INPUT : Enter the value of a & b.

    EXPLAINATION: When a derived class inherits the features of more than one base class then it is

    multiple inheritance.Here we take two base classes and one derived class. In this program we have to

    add two numbers.

    SOURCE CODE:

    #include

    #include

    class base1 //name of class

    {

    protected: //access level

    int a; //data member

    public:

    void read() //member function

    {

    couta; //standard input

    }

    void show()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    57/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    int b;

    public:

    void get()

    {

    coutb;

    }

    void put()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    58/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    }

    };

    int main() //beginning of main f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    59/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-25: WAP in C++ to demonstrate the concept of multilevel inheritance.

    INPUT : Enter the value of a & b.

    EXPLAINATION:The transitive nature of inheritance is reflected by this form of inheritance. When a

    subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.

    SOURCE CODE:

    #include

    #include

    class student //name of class

    {

    protected: //access level

    int rollno; //data member

    char name[20];

    public:

    void read() //member function

    {

    coutrollno; //standard input

    coutname;

    }

    void show()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    60/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    class student_details:public student

    {

    protected:

    int marks[3];

    public:

    void read()

    {

    student::read();

    coutmarks[0];

    coutmarks[1];

    coutmarks[2];

    }

    void show()

    {

    student::show();

    cout

  • 8/3/2019 C++ Practical File Complete

    61/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    public:

    void read()

    {

    student_total::read();

    }

    void cal()

    {

    total=marks[0]+marks[1]+marks[2];

    }

    void show()

    {

    student_details::show();

    cout

  • 8/3/2019 C++ Practical File Complete

    62/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    63/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-26: WAP in C++ to demonstrate the concept of hierarchical inheritance.

    INPUT : Enter any roll no., name and three subjects marks.

    EXPLAINATION: When many subclasses inherit from a single base class, it is known as hirarchical

    inheritance.

    SOURCE CODE:

    #include

    #include

    class stud_basic //NAME OF CLASS

    {

    private: //ACCESS LEVEL

    char name[20]; //DATA MEMBER

    int rollno;

    public:

    void read() //MEMBER FUNCTION

    {

    coutrollno; //STANDARD INPUT

    coutname;

    }

    void show()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    64/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    private:

    float marks[30],marks_obt;

    float per;

    public:

    void input()

    {

    stud_basic::read();

    cout

  • 8/3/2019 C++ Practical File Complete

    65/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    marks_obt=marks[0]+marks[1]+marks[2];

    per=float(marks_obt/300)*100;

    }

    };

    class stud_fitness:public stud_basic

    {

    private:

    float height,weight;

    public:

    void read()

    {

    stud_basic::read();

    cout

  • 8/3/2019 C++ Practical File Complete

    66/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    };

    void main() //BEGINNING OF MAIN F()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    67/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-27: WAP in C++ to demonstrate the concept of hybrid inheritance.

    EXPLAINATION: When a derived class inherits the features from multiple base classes which again

    inherits the featurs of a single base class then it is known as hybrid inheritance.Here we take four classes

    ,in this program we have to find the percentage of all the marks obtained.

    SOURCE CODE:

    #include

    #include

    class stud_basic //name of class

    {

    private: //access level

    char name[20]; //data member

    int rollno;

    public:

    void read() //member function

    {

    coutrollno; //standard input

    coutname;

    }

    void show()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    68/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    class stud_acd_marks:public stud_basic

    {

    protected:

    float marks[3];

    public:

    void read()

    {

    stud_basic::read(); //call read of base

    cout

  • 8/3/2019 C++ Practical File Complete

    69/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    protected:

    float score;

    public:

    void read()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    70/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    stud_acd_marks::show();

    sports::show();

    cout

  • 8/3/2019 C++ Practical File Complete

    71/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    72/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-28: WAP in c++ to demonstrate the concept of unary operator overloading.

    EXPLAINATION: Let us consider the unary plus operator. A plus operator when used as a unary,takes

    just one operand. The given program shows how the unary plus operator is overloaded.

    SOURCE CODE:

    #include

    #include

    class abc //name of class

    {

    int count; //declaration

    public: //access level

    abc()

    {

    count=0;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    73/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    74/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-29: WAP in C++ to demonstrate the concept of binary operator overloading.

    INPUT : Enter any values of hours and minutes.

    EXPLAINATION: Operator overloading means to do additional job to an operator a special function with

    that operator called as operator overloading. The same mechanism of unary operator can be used to

    overload a binary operator.The f()notation c=sum(a,b);can be replaced by a natural looking expression

    c=a+b;

    SOURCE CODE:

    #include

    #include

    class time //name of class

    {

    int hours; //data member

    int minutes;

    public: //access level

    void read() //member function

    {

    couthours; //standard input

    coutminutes;

    }

    void show() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    75/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    time tt3;

    tt3.hours=hours+tt2.hours;

    tt3.minutes=minutes+tt2.minutes;

    if(tt3.minutes>=60)

    {

    tt3.hours++;

    tt3.minutes=tt3.minutes-60;

    }

    return tt3;

    }

    };

    int main()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    76/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    77/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    Program-30: WAP in C++ to demonstrate the concept of function overloading.

    INPUT : Enter any value of a,b,c,g,h.

    Explaination: Overloading refers to the use of same thing for different purposes. C++ also permits

    overloading of functions. This means that we can use the same functionname to create functions that

    perform a variety of different tasks, using the concept of function overloading. For e.g: In this program

    an overloaded add() function handles different types of data as shown below:

    Source Code:

    #include

    #include

    int main() //beginning of main

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    78/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    cin>>h;

    d=add(a); //function call

    e=add(a,b);

    f=add(a,b,c);

    i=add(g,h);

    cout

  • 8/3/2019 C++ Practical File Complete

    79/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    int add(int p,int q,int r)

    {

    int s;

    s=p+q+r;

    return s;

    }

    float add(float m,float n)

    {

    float o;

    o=m+n;

    return o;

    }

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    80/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-31: WAP in C++ to demonstrate the concept of virtual f()'s.

    EXPLAINATION: How we do achieve polymorphism? It is achieved using what is known as virtual

    functions. When we use the same f()name in both the base&derived classes, the f() in base class is

    declared as virtual using the keyword virtual preceding its normal declaration.

    SOURCE CODE:

    #include

    class base //name of class

    {

    public: //access level

    void display() //member function

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    81/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    {

    coutshow();

    return 0; //return type

    } //end of program

  • 8/3/2019 C++ Practical File Complete

    82/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    83/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-32: WAP in C++ to demonstrate the concept of templates.

    EXPLAINATION: Templates is a new concept which enable us to define generic programming. Generic

    programming is an approach where generic types are used as parameters in algorithms so that they

    work for a variety of suitable data types and data structure.

    SOURCE CODE:

    #include

    template

    class test //name of class

    {

    t1 a;

    t2 b;

    public: //access level

    test(t1 x,t2 y)

    {

    a=x;

    b=y;

    }

    void show() //member f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    84/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    int main() //begining of main f()

    {

    cout

  • 8/3/2019 C++ Practical File Complete

    85/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-33: WAP in c++ to demonstrate the concept of files and streams.

    INPUT : Enter ur name and roll no.

    EXPLAINATION: Files are used to store the output of program.

    SOURCE CODE:

    #include

    #include

    int main() //begining of main f()

    {

    char name[20]; //variable declaration

    int rollno;

    cout

  • 8/3/2019 C++ Practical File Complete

    86/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    return 0; //return type

    } //end of program

    OUTPUT:

  • 8/3/2019 C++ Practical File Complete

    87/88

    By: Prabhat Kumar

    ASHOKA COLLEGE OF COMPUTER EDUCATION

    PROGRAM-34: WAP in C++ to demonstrate the concept of get and put.

    INPUT : Enter ur name.

    EXPLAINATION: The functions get() and put() are member functions of the file stream class fstream.get()

    is used to read a single character from the file.put() is used to write a single character to the output file.

    SOURCE CODE:

    #include

    #include

    int main() //beginning of main function

    {

    char a[20]; //variable declaration

    cout

  • 8/3/2019 C++ Practical File Complete

    88/88

    By: Prabhat Kumar

    OUTPUT: