Object Oriented Programming - Old Questions

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 | Asked in 2071

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;

 }