Pages

Showing posts with label Basic Introduction. Show all posts
Showing posts with label Basic Introduction. Show all posts

Thursday, July 11, 2013

Banking System in Classes In C++

/*
QUESTION:
AIM: Define a class to represent a bank account .Include the following members:

Data Members:

a Name of the depositor

b Account number

c Type of account

d Balance amount in the account




Member Functions:


a To assign initial values

b To deposit an amount

c To withdraw an amount after checking the balance

d To display name and balance

CODE:
*/


 #include<iostream>
using namespace std;
class Bank{
private:
 char name[20];
 char type_ac[20];
 int ac_no;
 long int balance;
public:
 Bank(){
  for(int i=0;i<20;i++){
   name[i]= ' ';
   type_ac[i] = '\0';
  }
  ac_no =0;
  balance = 0;
 }
 void bank_funct(char *n,char *t_a,int a_n ,int bal){
  strcpy(name,n);
  strcpy(type_ac,t_a);
  ac_no = a_n;
  balance = bal;
 }
 void deposit_amount(int bal){
  balance = bal;
 
 
 }

 void withdraw(int bal){
  if(bal > balance)
   cout<<"\nYou don have enough balance";
  else{
   cout<<"\nAmount Withdrawn is"<<bal<<endl;
   balance -=bal;
  
  }
 
 }
 void display(){
  cout<<"\nName is: "<<name;
  cout<<"\nBalance is: "<<balance<<endl;
 
 
 }

};


int main(){
 Bank obj1;
 char opt = ' ';
 char name[20];
 char account_type[20];
 int account_No;
 int balance;

 while(1){
 cout<<"\nEnter 'a' To assign initial values";

 cout<<"\nEnter 'b' To deposit an amount";

 cout<<"\nEnter 'c' To withdraw an amount";

 cout<<"\nEnter 'd' To display name and balance"<<endl<<endl;
 cin>>opt;

 switch(opt){
 case 'a':
  {cout<<"\nEnter name: ";
   cin>>name;
   cout<<"\nEnter Account type: ";
   cin>>account_type;
   cout<<"\nEnter Account number: ";
   cin>>account_No;
   cout<<"\nEnter balance: ";
   cin>>balance;
         obj1.bank_funct(name,account_type,account_No,balance);

  }
  break;
 case 'b':
   
        cout<<"Enter amount you wan to deposit";
  int balance1;
  cin>>balance1;
  obj1.deposit_amount(balance1);
  break;
 case 'c':
  cout<<"\nEnter amount to be withdrawn: ";
  int bal;
  cin>>bal;
  obj1.withdraw(bal);

  break;
 case 'd':
  obj1.display();
  break;
 default:
 
  break;
 }

 }
 return 0;
}
 

Matrix problem of Classes in C++

/*
QUESTION:
A matrix is a two dimensional array. Create a class ‘matrix’ that contains two attributes

max_x and max_y of type int, that stores size of maximum columns and rows respectively. Add

another attribute of type int** to store the array.

Constructor should allow the program to specify the actual dimensions of the matrix and store it

to max_x and max_y, and define a two dimension array of the given size.

Member functions that access the data in the matrix will needed two index numbers: one for

each dimension in array.

Here what a fragment of main() program that operate on such class looks like:

matrix m1(3,4) // define a matrix object

int temp=1234; //define an int value

m1.putel(7,4,temp) //insert value of temp into matrix at 7,4 location,

//return true if temp is inserted and false if this location is out of bound

Temp=m1.gettel(7,4) // retrun value from matrix at 7,4 and display message if this given index

is out of bound.
 


CODE;
*/

#include <iostream>
using namespace std;

class matrix{
private:
int max_x;
int max_y;
    int **ptr;
    
public:
matrix(){
max_x=0;
max_y=0;
ptr = NULL;
}
matrix(int x,int y){
max_x = x;
max_y = y;
ptr = new int *[max_x];
for(int i=0;i<max_x;i++)
ptr[i]= new int[max_y];
}
    bool putel(int  x,int y,int temp){
if(x  >max_x || y >max_y){
cout<<"\nOut of Bound: ";
return 0;
}
else{
ptr[x][y]= temp;
return 1;
}
}
int gettel(int x,int y){
if(x>max_x || y >max_y){
cout<<"\nOut of Bound: ";
return 0;
}
else 
return ptr[x][y];
}
};

int main(){
matrix m1(8,4);
int temp=1234;
m1.putel(4,4,temp);
temp = m1.gettel(4,4);
cout<<endl<<temp<<endl;
return 0;
}

Assert function in C++

/*
QUESTION:
Define a class CRecord with two private data members that store a name and an integer item number.

Define a getRecord() function member of the CRecord class that will set values for the data members

by reading input from the keyboard and a putRecord() function member that outputs the values of

the data members. Implement the getRecord() function such that calling program can detect when a

zero item number is entered(use assert function). Test your CRecord class with a main() function that reads and outputs

CRecord objects until a zero item number is entered.
 




CODE:
*/
#include <iostream>
#include<cassert>
using namespace std;
class CRecord{
char name[20];
int item_no;
public:
 void getRecord(){
  cout<<"\n\n\nEnter Name: ";
  cin>>name;
  cout<<"\nEnter item no: ";
        cin>>item_no;
  assert(item_no != 0);
 }
 void putRecord(){
      cout<<"\n\nName is: "<<name ;
   cout<<"\nItem NO is: "<<item_no ;

 }
};


int main(){
 CRecord obj1;
 while(1){
 obj1.getRecord();
 obj1.putRecord();


 }

 return 0;
}

Saturday, June 29, 2013

Recursive solution of determining whether a string is plaindrome or not in C++

/*
QUESTION:
Write a recursive solution of determining whether a string is palindrome or not. Note: a palindrome string is that which can be read the same forward or backward e.g; racecar, mom etc
*/
// CODE:
#include <iostream>
using namespace std;

bool palindrome(char[], int size);
//MAIN STARTS
int main()
{
 cout << "Enter a stringing: ";

 char string[50];

 cin.getline(string,50,'\n');

 cout << "The entered stringing " << ( (palindrome(string,strlen(string)+1))? "is":"is not" ) << " a Palindrome string." << endl;
 return 0;
}

//MAIN ENDS
bool palindrome(char string[], int size)
{
 if (string[0] != string[size-2]) //check for first and last element
  return false;
 if (strlen(string) <= 1)
  return true;
 else
  return palindrome(string+1,size-2);
}

Thursday, June 27, 2013

INTRODUCTION TO STRUCTURES IN C++

A structure is a collection of different variable types grouped together.
You can refer to a structure as a single variable and to its parts as members of that 
variable by using the dot (.) operator.
For Example:
// struct1.cpp
struct PERSON { // Declare PERSON struct type
int age; // Declare member types
float weight;
char name[25];
}; 
int main() {
PERSON sister;
PERSON brother; // C++ style structure declaration
sister.age = 13; // assign values to members
brother.age = 7;
return 0;
}
Difference between Array and structures:
Although arrays greatly improved our ability to store data, there is one major drawback to 
their use ... each element (each box) in an array must be of the same data type.  It is 
often desirable to group data of different types and work with that grouped data as one 
entity.  We now have the power to accomplish this grouping with a new data type called 
a structure.
Struct initialization:
You can initialize a structure by:
1.  PERSON boy={20,65,”Ali”};   //age 20, weight 65, name Ali
// initializes are used when contiguous members may be given
2.  PERSON boy;
boy.age=20; boy.weight=65;