You are on page 1of 8

1. Write C++ program to design a class called BankAccount.

Include following data members like


name of the depositor, account number and balance. Use following member functions a) to
initialize values b) deposit an amount c) to withdraw an amount d) to display name and balance.

#include<iostream>
using namespace std;
class BankAccount {
private: char CName[20];
int Accno;
double balance;
public:BankAccount(){
cout<<"Enter the customer name\n";
cin>> CName;
cout<<"Enter the Account Number\n";
cin>>Accno;
cout<<"Enter the initial balance\n";
cin>>balance;
}

void deposite(){
double amt;
cout<<"Enter the amount to be deposited\n";
cin>>amt;
balance=balance+amt;
cout<<"Current Balance ="<<balance;
}

void withdraw() {
double amt;
cout<<"How much amount to be withdrawn \n";
cin>>amt;
if(amt<=balance){
balance=balance-amt;
cout<<"The amount withdrawn"<<amt<<endl;
cout<< "Current Balance= "<<balance<<endl;
}
else{
cout<<"You can't withdrawn";
}
}

void display() {
cout<<"Customer Name \t Acc Number\t Balance"<<endl;
cout<<CName<<"\t\t"<<Accno<<"\t\t"<<balance<<endld;

}
void menu() {
cout<<"****** Banking Transactions ******"<<endl;
cout<<"1--> Deposite"<<endl;
cout<<"2--> Withdraw"<<endl;
cout<<"3--> Show Status"<<endl;
cout<<"4--> Exit"<<endl;
}
};

int main(){

BankAccount ba;

int rpt=1, ch;


while(rpt){
ba.menu();
cout<<"Enter your choice\n";
cin>>ch;
switch(ch){
case 1:ba.deposite();
break;
case 2:ba.withdraw();
break;
case 3:ba.display();
break;
case 4:return 0;
}
cout<<"\nDo you wan't to continue (Type 0/1)?"<<endl;
cin>>rpt;
}
cout<<"Thank you for banking with us Visit Again \n";
}
--------------------------------------------------------------------------------------------------------------------
2. Write a C++ program with class Time with data members that represents hours and minutes.
Include appropriate member functions to compute time in hours and minutes . (Use of objects as
arugemnts).

// C++ program to create class to read and add two times


// Using Object as arguments

#include <iostream>
using namespace std;

class Time {
private:
int hr;
int min;
int sec;

public:
void getTime(void);
void putTime(void);
void addTime(Time T1, Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours?: ";
cin >> hr;
cout << "Minutes?: ";
cin >> min;
cout << "Seconds?: ";
cin >> sec;
}

void Time::putTime(void)
{
cout << endl;
cout << "Time after adding: ";
cout << hr << ":" << min << ":" << sec << endl;
}

void Time::addTime(Time T1, Time T2)


{

this->sec = T1.sec + T2.sec;


this->min = T1.min + T2.min + this->sec / 60;
;
this->hr = T1.hr + T2.hr + (this->min / 60);
this->min %= 60;
this->sec %= 60;
}

int main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();

return 0;
}
-----------------------------------------------------------------------------------------------------------------
3. Given that an EMPLOYEE class ontains following members. Data members:Eno, Ename and
salary Member functions: to read the data , to print data members.Write a C++ program to read
the data of N employees and display detials of each employee.(use Array of objects concept).

----------------------------------------------------------------------------------------------------------------
4. Write a C++ program to create a class sample with integer ,character and float data members.
Demonstrate Constructor Overloading on this class with all types of constructors including
default argument constructor.
#include<iostream>
using namespace std;

class Sample {
int a;
char b;
float c;
public:
Sample(){
cout<<"Default constructor called \n";
a=0;
b='N';
c=0.0;
}

Sample(int x, char y='A', float z=34.54){


cout<<"Default argument constructor called \n";
a=x;
b=y;
c=z;
}

void printdata(){
cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b<<endl;
cout<<"Value of c="<<c<<endl;
}
Sample(Sample &m){
cout<<"Copy constructor overlaoded"<<endl;
a=m.a;
b=m.b;
c=m.c;
}
};

int main(){

Sample s,s1;
s.printdata();

Sample s2(1,'e',45.5);
s2.printdata();

s1=Sample(10,'Z');
s1.printdata();

Sample s3(s1);
s3.printdata();
}
------------------------------------------------------------------------------------------------------------------
5. Write a C++ program to create a class called COMPLEX and implement the following overloading
function ADD that return a COMPLEX number.

i. ADD(a,c2);- where a is an integer(real part) & c2 is a complex no.

ii. ADD(c1,c2);- where c1 & c2 are complex nos. use Function overloading(ADD) and Friend function
concept for the implementation

#include<iostream>

using namespace std;

class complex

int r,i;

public:

void read();

void print();

friend complex add(int a,complex c);

friend complex add(complex c1,complex c2);

};

void complex::read()

cout<<"Enter real and imaginary\n";

cin>>r>>i;

void complex::print()

cout<<r<<"+i"<<i<<endl;

complex add(int a,complex c)

complex t;

t.r=a+c.r;

t.i=c.i;

return t;
}

complex add(complex c1,complex c2)

complex t;

t.r=c1.r+c2.r;

t.i=c1.i+c2.i;

return t;

int main()

int a=2;

complex s1,s2,s3;

s1.read();

cout<<"\ns1 : ";

s1.print();

s2=add(a,s1);

cout<<"s2 : 2+s1\n";

cout<<" : ";

s2.print();

s3=add(s1,s2);

cout<<"s3=s1+s2\n";

cout<<"s1 : ";

s1.print();

cout<<"s2 : ";

s2.print();

cout<<"s3 : ";

s3.print();

return 0;

------------------------------------------------------------------------------------------------------------------
6. Write a C++ program with two classes A and B with one integer data member in each class. Write
member functions to read and display, place a friend function in these classes which takes the data
members of these classes and computes maximum of two data members. Demonstrate using main
function.

#include<iostream>
using namespace std;

class A;

class B
{
int num;
public:
void read()
{
cout<<"\n Enter number for class B - ";
cin>>num;
}
friend void greatest(A a1,B b1);
};

class A
{
int num;
public:
void read()
{
cout<<"\n Enter number for class A - ";
cin>>num;
}
friend void greatest(A a1,B b1);
};

void greatest(A a1,B b1)


{
if(a1.num>b1.num)
{
cout<<"\n Number in class A is greatest i.e."<<a1.num;
}
else if(a1.num<b1.num)
{
cout<<"\n Number in class B is greatest i.e."<<b1.num;
}
else
{
cout<<"\n Number in both classes are equal";
}
}
int main()
{

A a1;
a1.read();

B b1;
b1.read();

greatest(a1,b1);

return 0;
}

-----------------------------------------------------------------------------------------------------------------------

You might also like