mc0066 spring drive assignment 2012

Upload: chandan-pant

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    1/23

    Spring 2012

    Master of Computer Application (MCA)

    Semester IIMC0066 OOPS using C++ 4 Credits (Book ID: B0681 &B0715)

    Assignment Set 1 (40 Marks)

    Questions From Book-ID : B0681

    1.Distinguished between procedural language and OOP

    language. And Explain the key features of OOP.

    Answer: Object Orientation Languages (OOL) is concerned to

    develop an application based on real timewhile Procedural Programing Languages (PPL) aremore concerned with the processing of procedures andfunctions.

    In OOL, more emphasis is given on data rather than

    procedures, while the programs are divided intoObjects and the data is encapsulated (Hidden) from theexternal environment, providing more security to datawhich is not applicable or rather possible in PPL. In PPL,its possible to expose Data and/or variables to theexternal entities which is STRICTLY restricted IN OOL.

    In OOL, the Objects communicate with each other viaFunctions while there is no communication in PPL rather

    its simply a passing values to the Arguments to theFunctions and / or procedures.

    OOL follows Bottom Up Approach of Program Executionwhile in PPL its Top Down approach.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    2/23

    OOL concepts includes Inheritance, Encapsulation and

    Data Abstraction, Late Binding, Polymorphism,Multithreading, and Message Passing while PPL issimply a programming in a traditional way of callingfunctions and returning values.

    Below is the list of OOL languages :- JAVA, VB.NET,C#.NET

    Below is the list of PPL languages :- C, VB, Perl, Basic,

    FORTRAN.

    Features of OOPs

    Encapsulation: Wrapping up of data and function into a

    single unit is known as Encapsulation.

    Inheritance: Ability of a new class to be created, from an

    existing class by extending it, is called inheritance.

    Polymorphism: Polymorphism means same operation maybehave differently on different classes.

    Polymorphism is a generic term that means'many shapes'. More precisely Polymorphisms means theability to request that the same operations be performed bya wide range of different types of things.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    3/23

    2.What is function overloading? Write a c++ program toimplement a function overloaded.

    Answer:Function overloading or method overloading that allowscreating several methodswith the same name which differfrom each other in the type of the input and the output ofthe function. It is simply defined as the ability of onefunction to perform different tasks.

    Rules in function overloading

    The overloaded function must differ either by thearity ordata types.

    The same function name is used for various instances offunction call.

    Example: function overloading in c++

    main()

    {cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    4/23

    // volume of a cuboidlong volume(long l,int b,int h){

    return(l*b*h);}

    In the above example, the volume of various componentsare calculated using the same function call "volume", witharguments differing in their data type or their number.

    3.Discuss the constructors and Destructors with suitableexample.

    ANSWER:

    A constructor is a member function with the same name as itsclass. For example:

    class X {public:

    X(); // constructor for class X};

    Constructors are used to create, and can initialize, objects of theirclass type.

    You cannot declare a constructor as virtual or static, nor can you

    declare a constructor as const, volatile, or const volatile.

    You do not specify a return type for a constructor. A returnstatement in the body of a constructor cannot have a returnvalue.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    5/23

    Destructors are usually used to deallocate memory and do othercleanup for a class object and its class members when the object

    is destroyed. A destructor is called for a class object when thatobject passes out of scope or is explicitly deleted.

    A destructor is a member function with the same name as itsclass prefixed by a ~ (tilde). For example:

    class X {public:

    // Constructor for class XX();

    // Destructor for class X~X();};

    A destructor takes no arguments and has no return type. Itsaddress cannot be taken. Destructors cannot bedeclared const, volatile, const volatileor static. A destructor canbe declared virtual or pure virtual.

    4.What do you mean by operator overloading? Illustrate with

    suitable example for overloading Unary operators.

    ANSWER:

    The operator overloading (less commonly known as ad-hoc

    polymorphisms) is a specific case ofpolymorphisms in which

    some or all of operators like +, - or == are treated as

    polymorphic functions and as such have different behaviors

    depending on the types of its arguments.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    6/23

    overload a unary operator with either a nonstatic memberfunction that has no parameters, or a nonmember function thathas one parameter. Suppose a unary operator @ is called withthe statement @t, where t is an object of type T. A nonstatic

    member function that overloads this operator would have thefollowing form:

    return_type operator@()

    A nonmember function that overloads the same operator wouldhave the following form:

    return_type operator@(T)

    An overloaded unary operator may return any type.

    The following example overloads the ! operator:

    #include using namespace std;

    struct X { };

    void operator!(X) {

    cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    7/23

    The following is the output of the above example:

    void operator!(X)

    void Y::operator!()

    The operator function call !ox is interpreted as operator!(X). Thecall !oy is interpreted as Y::operator!().

    (The compiler would not allow !oz because the ! operator has notbeen defined for class Z.)

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    8/23

    Questions From Book-ID : B0715

    5.Write C++ program which demonstrate the difference

    between static and dynamic binding.ANSWER:

    C++ matches a function call with the correct function definition at compile time. This is called static

    binding. You can specify that the compiler match a function call with the correct function definition at

    run time; this is called dynamic binding. You declare a function with the keyword virtual if you want

    the compiler to use dynamic binding for that specific function.

    The following examples demonstrate the differences between static and dynamic binding. The firstexample demonstrates static binding:

    #include using namespace std;

    struct A {void f() { cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    9/23

    virtual void f() { cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    10/23

    void set_i(int arg) { i = arg; }static void set_si(int arg) { si = arg; }

    void print_i() {cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    11/23

    void printall();};

    void C::printall() {cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    12/23

    int st[100];};

    string st[100];};

    The main program would declare and use these stacks as follows:

    IntStack ii;StringStack ss;ii.push(25);ss.push("Hello");

    Notice that the only difference between these classes (other than the name of the class) is the type ofdata that's put onto the stack. C++ allows you to define a single, template class to represent a stack ofany possible datatype (including a user-defined datatype). The declaration would look like this:

    #ifndef STACK_H#define STACK_Htemplate class Stack {

    public:Stack() { top = -1; }void push(T i)

    { st[++top] = i; }T pop()

    { return st[top--]; }private:

    int top;T st[100];

    };

    #endif

    The T represents the type of stack desired. The main program would declare and use the stacks asfollows:

    Stack ii;Stack ss;ii.push(25);ss.push("Hello");

    There is never an implementation file (*.cpp) for a template class. All of the member functions should bedeclared in the header file ( in this case, table.h). The following shows how the functions for this classwould be declared after the declaration of the class.

    #ifndef STACK_H

    #define STACK_H

    template class Stack {public:

    Stack();void push(T i);T pop();

    private:int top;

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    13/23

    T st[100];};

    template Stack::Stack(){

    top = -1;}

    template void Stack::push(T i){

    st[++top] = i;}

    template T Stack::pop(){

    return st[top--];}

    #endif

    8.What is template specialization? Describe a scenario in which

    template class partial specialization is considered

    appropriate.

    ANSWER:

    Declaration should include the templatekeyword with the class name followed by thetype in the angular brackets.

    templateclass MyQueue{

    std::vector data;

    public:void Add(double const &);void Remove();void Print();

    };

    The above C++ specialized template is the specialized version of the example used intheC++ Class Templatearticle. This sample uses the doubledata type for specialization.

    http://www.codersource.net/C/CTutorialonTemplates/cpp_class_templates.htmlhttp://www.codersource.net/C/CTutorialonTemplates/cpp_class_templates.htmlhttp://www.codersource.net/C/CTutorialonTemplates/cpp_class_templates.htmlhttp://www.codersource.net/C/CTutorialonTemplates/cpp_class_templates.html
  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    14/23

    Though this specific class mentioned here does not show any optimized code for thedouble data type, this can serve the purpose of understanding how a C++ classtemplate specialization works.

    Partial Specialization - C++ Class Template Specialization:

    The above is the example for full specialization. Partial C++ template specializationsare also possible. A good example code will be as follows.

    template class MyQueue{};

    The above is the example for partial specialization with the same type.

    template class MyQueue{};

    The above code is the example for partial specialization with a data type different fromthe template typename.

    C++ Class Template specialization can be employed wherever it is believed that thecode can be optimal if written with the specific data type. Otherwise a normal genericc++ class template can serve the purpose.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    15/23

    Spring 2012Master of Computer Application (MCA)

    Semester II

    MC0066 OOPS using C++ 4 Credits (Book ID: B0681 &B0715)

    Assignment Set 2 (40 Marks)Questions From Book-ID : B0681

    1.Write advantage of multiple inheritances. And write a c++program to implement the multiple inheritances.ANSWER:

    Advantages of multiple inheritances:

    Multiple inheritance allows a class to inherit the functionalityof more than one base class thus allowing for modeling ofcomplex relationships.

    You categorize classes in many ways. Multiple inheritance isa way of showing our natural tendency to organize theworld. During analysis, for example, we use multiple

    inheritance to capture the way users classify objects.

    By having multiple super-classes, your subclass has moreopportunities to reuse the inherited attributes andoperations of the super-classes.

    C++ Languages most powerful feature is multiple inheritancewhich makes it more powerful than Java.

    It is possible for one class to inherit the attributes of two or moreclasses.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    16/23

    The general form is

    class DerivedClass : BaseClassList //seperated with commas

    {

    };

    Here is a simple example.

    class X{

    int a;

    };

    class Y{

    int b;};

    class Z : public X, public Y

    {

    int c;

    };

    Here Z class has the access to both the members of X & Y class.

    2.Discuss the types of Inheritance with suitable example for each.

    ANSWER:

    C++ is rich in its data types. Inheritance is the concept to

    inherit the properties of one class to another class. This

    has also known as class structure again. For example,

    classes A contains two-member function ads and subtracts

    and class b contain two another functions multiply and

    divide.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    17/23

    Inheritance is the concept to inherit the properties of one class toanother class. This has also known as class structure again. For

    example, classes A contains two-member function ads andsubtracts and class b contain two another functions multiply anddivide. We want to use all these function with one object then weneed to use inheritance where class B inherits all the property ofclass, which is public, but class B cannot use theprivate properties of class A. There are following types ofinheritance:

    1. Single class Inheritance:When class a gas inherited in class has known as base class andB class is know as derived class. Here only two classes

    have linked to each other.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    18/23

    2. Multilevel Inheritance:In this type of inheritance, there are number of level and it hasused in that cases where we want to use all properties in numberof levels according to the requirement. For example, class

    A inherited in class b and class b has inherited in class c for classb so on. Where class A is base class c. In another way we can sayb is derived class a base class for c and a indirect base class for cis indirect base class for c and c indirect derived class for class A.

    3. Multiple Inheritances:In this type of inheritance, number of classes has inherited in a

    single class. Where two or more classes are, know as baseclass and one is derive class.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    19/23

    4. Hierarchical Inheritance:This type of inheritance helps us to create a baseless for number

    of classes and those numbers of classes can have furthertheir branches of number of class.

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    20/23

    5. Hybrid Inheritance:In this type of inheritance, we can have mixture of number ofinheritances but this can generate an error of using same name

    function from no of classes, which will bother the compiler to howto use the functions. Therefore, it will generate errors in theprogram. This has known as ambiguity or duplicity.

    3.Write a c++ program to implements the relational operatoroverloading for the distance class.ANSWER:

    #include using namespace std;

    class Distance{

    private:int feet; // 0 to infinite

    int inches; // 0 to 12public:// required constructorsDistance(){

    feet = 0;inches = 0;

    }Distance(int f, int i){

    feet = f;inches = i;

    }

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    21/23

    // method to display distancevoid displayDistance(){

    cout

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    22/23

    Questions From Book-ID : B0715

    4. Write the advantages of using exception handling with its basic

    models.

    ANSWER:

    The advantage of exceptions are two fold:

    They can't be ignored. You must deal with them at some level, or they will terminate your

    program. With error code, you must explicitly check for them, or they are lost.

    They can be ignored. If an error can't be dealt with at one level, it will automatically bubble up

    to the next level, where it can be. Error codes must be explicitly passed up until they reach the

    level where it can be dealt with.

    5. Discuss the various STL components in brief.

    ANSWER:

    STL Components

    Containers classes for objects that containIterators pointers into containers

    Generic algorithms functions that work on different types of

    containers

    Adaptors classes that adapt other classes

    Allocators objects for allocating space

    6. Describe the time overhead of operations on sequence

    containers.

    ANSWER:

  • 7/31/2019 MC0066 Spring Drive Assignment 2012

    23/23

    7. Discuss how object diagrams are different from class diagram.ANSWER:A class diagram is a graph of Classifier elements connected by their various static

    relationships. Note that a classdiagram may also contain interfaces, packages,relationships, and even instances, such as objects and links. Perhaps a better name wouldbe static structuraldiagram, butclassdiagram is shorter and well established.

    An object diagram is a graph of instances, including objects and data values. A static objectdiagram is an instance of a class diagram; it shows a snapshot of the detailed state of a

    system at a point in time. The use of object diagrams is fairly limited, mainly to showexamples of data structures.

    Tools need not support a separate format for object diagrams. Class diagrams can containobjects, so a class diagram with objects and no classes is an objectdiagram. The phraseis useful, however, to characterize a particular usage achievable in various ways.

    The basic idea is that class diagrams focus on classes and object diagrams focus on objects,but it is possible to mix classes and objects on the same diagram for various purposes, sothe separation is not rigid.