Object Oriented Programming 2071

Tribhuwan University
Institute of Science and Technology
2071
Bachelor Level / Third Semester / Science
Computer Science and Information Technology ( CSC-202 )
( Object Oriented Programming )
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to give their answers in their own words as far as practicable.
The figures in the margin indicate full marks.

Section A

Attempt any two questions: (2x10=20)

1. Why do we need object oriented programming? How can we use inheritance to reuse already written and tested code in programs? Discuss with suitable example. (3+3+4)

10 marks view

2. Discuss features of class and object. Design a class to represent a bank account with data members name, account-number, account-type, and balance and functions to assign initial values, to deposit an amount, to withdraw an amount after checking balance, and to display the name and balance. (4+6)

10 marks view

Class

  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class name must start with an uppercase letter. If class name is made of more than one word, then first letter of each word must be in uppercase.
  • Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers.
  • Class's member functions can be defined inside the class definition or outside the class definition.
  • Objects of class holds separate copies of data members. We can create as many objects of a class as we need.

Object

  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables.
  • Objects are initialized using special class functions called Constructors
  • And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object. 


Program:

#include <iostream>

#include<stdio.h>

#include<conio.h>

using namespace std;

class Bank

{

 public:

  char name[20];

  char account_type[20];

  int account_number;

  int balance;


void initialize()

{

  cout<<"\\nEnter Account Holders Name:";

  cin>>name;

  cout<<"\\nEnter Account type:";

  cin>>account_type;

  cout<<"\\nEnter account number:";

  cin>>account_number;

  cout<<"\\nEnter balance to deposit:";

  cin>>balance;

}


void deposit()

{

  int bal;

  cout<<"\\nEnter the amout to deposit:";

  cin>>bal;

  balance=balance+bal;

  cout<<"\\nAmount deposited successfuly!!\\nYour New Balance:"<<balance;

}


void withdraw()

{

  int bal;

  cout<<"\\nYour balance :"<<balance<<"\\nEnter amount to withdraw:";

  cin>>bal;

  if(bal<=balance)

  {

    balance=balance-bal;

    cout<<"\\nRemaining Balance:"<<balance;

  }

  else

  {

    cout<<"Cannot withdraw amount!!";

  }

}


void display()

{

  cout<<"\\nName :"<<name;

  cout<<"\\nAccout Type:"<<account_type;

  cout<<"\\nAccount No."<<account_number;

  cout<<"\\nBalance :"<<balance;

}

};

int main()

{

  int i;

  Bank bk;

  bk.initialize();

  cout<<"\\n1. Your Information\\n2. Deposit\\n3. Withdraw\\nEnter your choice:\\n";

  cin>>i;

  if(i==1)

  {

    bk.display();

  }

  else if(i==2)

  {

    bk.deposit();

  }

  else if(i==3)

  {

    bk.withdraw();

  }

  getch();

  return 0;

 }

3. What is operator overloading? What are the benefits of operator overloading? How is operator overloading different from function overloading. Write a program that shows an example of function overloading. (2+2+2+4)

10 marks view

Section B

Attempt any eight questions: (8x5 = 40)

4. Display polymorphism with example.

5 marks view

5. What is library function? How is it different from user defined function?

5 marks view

6. What is library function? How is it different from user defined function? 

5 marks view

7. Discuss the use of inline function with example.

5 marks view

8. What is constructor? Differentiate it with destructor.

5 marks view

9. Explain about accessing a member function outside a class with example.

5 marks view