C++ Programs
C++ Programs
#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;
#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;
}
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;
}
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.
ii. ADD(c1,c2);- where c1 & c2 are complex nos. use Function overloading(ADD) and Friend function
concept for the implementation
#include<iostream>
class complex
int r,i;
public:
void read();
void print();
};
void complex::read()
cin>>r>>i;
void complex::print()
cout<<r<<"+i"<<i<<endl;
complex t;
t.r=a+c.r;
t.i=c.i;
return t;
}
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);
};
A a1;
a1.read();
B b1;
b1.read();
greatest(a1,b1);
return 0;
}
-----------------------------------------------------------------------------------------------------------------------