Object Oriented Programming is a programming technique in which programming is based upon objects.

What is Object?

Object is any entity about which data is collected. Object may be a Fan, Person, Car etc.

How to Display Your name on Taskbar in Windows 10

Before Understanding classes you have to practice functions.

What is Function?

Function is a piece of code or a block of code that is used to perform some action. There are two types of function

  • Built in Function
  • User Defined Function

Here we Concerned with User Defined Function

What is Class?

Class is collection of objects having same properties and function.

For example if we have a object like Person then

Person have following properties:

  • Color
  • Name
  • Age
  • Height etc.

Similarly it has following functions:

  • Speaking
  • Weeping
  • Eating
  • Walking etc.

Syntax of class Person in C++

class Person

{
  private:
//class member variables definitions. public:
//class member functions definition }

You can practice other programming questions for C and C++ Exercises.

WE provided a large Variety of solved programming exercises.

Functions Programming Exercises 

Question Number 2

Write a program in c++/c that creates a class bank account that includes the following data members:

  • Name of the depositor
  • Account Number
  • Type of account
  • Balance amount in the account

The class also contains the following member functions:

  • A constructor to assign initial values
  • Deposit function to deposit some amount.It should accept the amount as parameter.
  • Withdraw function to withdraw an amount after checking the balance.It should accept the amount as parameter.
  • Display function to display name and balance.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

class bank
{
     private:
        char name[20];
        int acno;
        char actype[20];
        int bal;
    public:
         bank();
         void deposit(int amt);
        void withdraw(int amt);
        void display();
};

     bank::bank()
    {
       cout<<"Enter account name:";
       gets(name);
       cout<<"Enter account no:";
       cin>>acno;
       cout<<"Enter account type:";
	   gets(actype);
	   cout<<"Enter opening balance:";
	   cin>>bal;
    }
    void bank::deposit(int amt)
    {
    	bal = bal + amt;
    	cout<<"\nBalance after deposit:"<<bal;
    }
    void bank::withdraw(int amt)
    {
    	if(amt > bal)
    	{
    		cout<<"Not enough amount in account!"<<endl;
    	}
    	
    	else
    	{
    		bal = bal - amt;
    		cout<<"Balance after withdraw:"<<bal;
    	}
    }
    void bank::display(void)
    {
    	cout<<"Account Details:"<<endl;
    	cout<<"Account Name:    "<<name<<endl;
    	cout<<"Account No.      "<<acno<<endl;
    	cout<<"Account Type:    "<<actype<<endl;
    	cout<<"Current Balance:"<<bal<<endl;
    }
    main()
    {
    	int a, choice;
    	bank ac;
    
    		cout<<"\n\nChoice List\n\n";
    		cout<<"1) Deposit\n";
    		cout<<"2) Withdraw\n";
    		cout<<"3) Display All Details\n";
    		cout<<"Enter your choice: ";
    		cin>>choice;
    		switch(choice)
    		{
    			case 1:
    				cout<<"Enter amount in deposit:";
    				cin>>a;
    				ac.deposit(a);
    				break;
    			case 2:
					cout<<"Enter amount to withdraw:";
    				cin>>a;
    				ac.withdraw(a);
    				break;
    			case 3:
    				ac.display();
    				break;
    		   default:
    				cout<<"Invalid Choice";
    		}
    		
    	
    
    }

Leave a Reply