Cse Assignment

download Cse Assignment

of 33

Transcript of Cse Assignment

  • 7/29/2019 Cse Assignment

    1/33

    Assignment : Object Oriented

    Programming using C++

    Name:Ashish Baheti

    Reg. no:11007494

    Section:E3R33

    Course Code:CSE 202T

    Submitted to: Saloni

    Madam

  • 7/29/2019 Cse Assignment

    2/33

    Que 1.

    Program is as follows:-

    #include

    #include

    #include

    struct student

    {int stu_id;

    char stu_name[21];

  • 7/29/2019 Cse Assignment

    3/33

    char stu_address[51];

    int stu_section;

    long int contact_no;

    int total_marks;

    };

    int main()

    {

    int i,N;

    coutN;

    struct student st[N];

    cout

  • 7/29/2019 Cse Assignment

    4/33

    {

    coutst[i].stu_section;

    cin>>st[i].contact_no;

    cin>>st[i].total_marks;

    cout

  • 7/29/2019 Cse Assignment

    5/33

    cout

  • 7/29/2019 Cse Assignment

    6/33

    cout

  • 7/29/2019 Cse Assignment

    7/33

    {

    private:

    char acc_holder[21];

    long int acc_no;

    char acc_type[11];

    long int acc_balance;

    public:

    account(char name[21],long int accno,char

    acctype[11],long int balance){

    acc_holder=name;

    acc_no=accno;

    acc_type=acctype;

    acc_balance=balance;

  • 7/29/2019 Cse Assignment

    8/33

    }

    void balance(account a1)

    {

    cout

  • 7/29/2019 Cse Assignment

    9/33

    coutamt;

    a3.acc_balance=a3.acc_balance-amt;cout

  • 7/29/2019 Cse Assignment

    10/33

    {

    cout

  • 7/29/2019 Cse Assignment

    11/33

    char c;

    class account

    acc1(ashish,11007494,saving,1000);

    cout

  • 7/29/2019 Cse Assignment

    12/33

    elseif(c==2)

    {

    acc1.deposit(acc1);

    }

    elseif(c==3)

    {

    acc1.withdrawl(acc1);

    }

    elseif(c==4)

    {

    acc1.detail(acc1);

    }else

    {

  • 7/29/2019 Cse Assignment

    13/33

    cout

  • 7/29/2019 Cse Assignment

    14/33

    should have a function header and a

    function body. An important difference

    between a member function and a normal

    function is that a member function

    incorporates a membership identity label

    in the header. This label tells the compiler

    which class the function belongs to. Thegeneral form of a function definition is:

    return-type class-name :: function-name

    (argument-declaration)

    {

    Function-body

    }

    The membership label class-name :: tells

    the compiler that the function function-

    name belongs to the class class-name. That

  • 7/29/2019 Cse Assignment

    15/33

    is, the scope of the function is restricted to

    the class-name specified in the header line.

    The symbol :: is called the scope resolution

    operator.

    The member functions have some special

    characteristics that are often used in the

    program development. These

    characteristics are:

    1. Several different classes can use thesame function name. The membership

    label will resolve their scope.

    2. Member functions can access theprivate data of the class. A non-

    member function cannot do so.

    (However an exception to this rule is a

    friend function.)

  • 7/29/2019 Cse Assignment

    16/33

    3. A member function can call anothermember function directly, without

    using the dot operator.

    Inside the class definition

    Another method of defining a

    member function is to replace the function

    declaration by the actual function definition

    inside the class. For example, we could

    define the item class as follows:

    Class item{

    Int number;

    Float cost;

    Public:

  • 7/29/2019 Cse Assignment

    17/33

    Void getdata (int a, float b);

    // declaration

    // Inline function

    Void putdata (void)

    //definition inside the class

    {

    Cout

  • 7/29/2019 Cse Assignment

    18/33

    a function that is expanded in line when it is

    invoked. That is, the compiler replaces the

    function call with the corresponding

    function code. Usually, the functions are

    made inline when they are small enough to

    be defined in one or two lines. Example:

    Inline double cube (double a)

    { return (a*a*a); }

    The inline keyword merely sends a

    request, not a command, to the compiler.The compiler may ignore this request if the

    function definition is too long or to

    complicated and compile the function as a

    normal function.

    Some of the situations where inline

    expansions may not work are:

  • 7/29/2019 Cse Assignment

    19/33

    1. For functions returning values, if aloop, a switch, or a goto exists.

    2. For functions not returning values, if areturn statement exists.

    3. If functions contain static variables.4. If inline functions are recursive.

    Here is a program to calculate factorial

    of a no. using friend function and inline

    function with classes.

    #include

    #include

    Class fact

    {

    Int n,b;

    Public:

  • 7/29/2019 Cse Assignment

    20/33

    Fact (int x)

    {

    n=x;

    b=1;

    }

    Friend void fac (fact);

    };

    Void fac (fact s)

    {

    For (int i=1; i

  • 7/29/2019 Cse Assignment

    21/33

    }

    Void main ()

    {

    Clrscr();

    Int a;

    Cout > a;

    Class fact d(a);

    fac (d);

    getch();

    }

    Que 4.The statement

    C2=5.0+C1;

  • 7/29/2019 Cse Assignment

    22/33

    Will not work. This is because the left-

    hand operand which is responsible for

    invoking the member function should be an

    object of the same class. However friend

    function allows both approaches because

    an object need not be used to invoke a

    friend function but can be passed as anargument. Thus we can use a friend

    function with a built-in type data as the left-

    hand operand and an object as an right-

    hand operand.

    Que 5.

    (a). It will not be allowed by the

    compiler to create a class which has

    constructors defined under the private

    label. The compiler will not execute the

    program containing that class. The compiler

  • 7/29/2019 Cse Assignment

    23/33

    will report that in the main function the

    declaration of the object is not possible

    because the constructor which is created

    for the initialization of the object is under

    the private label.

    (b). if somehow, the compiler allows

    creating private constructors, I will not be

    able to create an instance of this class and

    use its instance member function in one

    way or another.

    Que 6.

    Consider a situation where all the

    three kinds of inheritance, namely,

    multilevel, multiple and hierarchical

    inheritance , are involved. This is illustrated

    in the following example. The child has

    two direct base classes parent 1 and

  • 7/29/2019 Cse Assignment

    24/33

    parent 2 which themselves have a

    common base class grandparent. The

    child inherits the traits of grandparent via

    two separate paths. It can also inherit

    directly as shown by the broken line. The

    grandparent is sometimes referred to as

    indirect base class.

    Inheritance by the child as shown in

    the figure might pose some problems. All

    the public and protected members of

    grandparent are inherited into child

  • 7/29/2019 Cse Assignment

    25/33

    twice, first via parent 1 and again via

    parent 2. This means, child would have

    duplicate sets of the members inherited

    from grandparent. This introduces

    ambiguity and should be avoided.

    The duplication of inherited members

    due to these multiple paths can be avoided

    by making the common base class (ancestor

    class) as virtual base class while direct or

    intermediate base classes as shown below.

    Class a //grandparent

    {

    };

  • 7/29/2019 Cse Assignment

    26/33

    Class B1 : virtual public A //parent 1

    {

    .

    .

    };

    Class B2 : public virtual A //parent 2

    {

    .

    .

    };

    Class C : public B1, Public B2 //child

    {. // only one copy of A

    . // will be inherited

  • 7/29/2019 Cse Assignment

    27/33

    };

    When a class is made avirtual

    base class, C++ takes necessary care to see

    that only one copy of that class is inherited,

    regardless of how many inheritance paths

    exist between the virtual base class and a

    derived class.

    A program to implement the concept of

    virtual base class is illustrated in the

    following example.

    #include

    #include

    Class student

    {

    Protected :

  • 7/29/2019 Cse Assignment

    28/33

    Int roll_number;

    Public :

    Void get_number ( int a )

    {

    roll_number = a;

    }

    Void put_number ( void )

    {

    Cout

  • 7/29/2019 Cse Assignment

    29/33

    Protected :

    Float part1, part2;

    Public :

    Void get_marks ( float x, float y)

    {

    Part1 = x;

    Part2 = y;

    }

    Void put_marks ( void )

    {

    Cout

  • 7/29/2019 Cse Assignment

    30/33

    };

    Class sports : public virtual student

    {

    Protected :

    Float score;

    Public:

    Void get_score ( float s )

    {

    Score = s;

    }

    Void put_score ( void )

    {Cout

  • 7/29/2019 Cse Assignment

    31/33

    };

    Class result :public test, public sports

    {

    Float total;

    Public :

    Void display ( void );

    };

    Void result :: display ( void )

    {

    Total = part1 + part2 + score;

    Put_number();

    Put_marks();Put_score();

    Cout

  • 7/29/2019 Cse Assignment

    32/33

    }

    Int main()

    {

    Result student_1;

    Student_1.get_number ( 678 );

    Student_1.get_marks ( 30.5, 25.5 );

    Student_1.get_score ( 7.0 );

    Student_1.display ();

    getch();

    Return 0;

    }

    The output of the above program wouldbe :

    Roll No : 678

  • 7/29/2019 Cse Assignment

    33/33

    Marks obtained :

    Part1 = 30.5

    Part2 = 25.5

    Sport wt : 7

    Total score : 63

    END OF THE ASSIGNMENT