0% found this document useful (0 votes)
46 views11 pages

Assignment 3 Q1

The document contains code for two C++ programs. The first program defines a Time class with methods to set, add, and display time objects. It overloads the + operator to add two Time objects. The main function demonstrates creating and adding two Time objects. The second program defines a Bank class with methods to deposit, withdraw, and display bank account details. The main function demonstrates creating a Bank object, calling its methods, and displaying the account details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views11 pages

Assignment 3 Q1

The document contains code for two C++ programs. The first program defines a Time class with methods to set, add, and display time objects. It overloads the + operator to add two Time objects. The main function demonstrates creating and adding two Time objects. The second program defines a Bank class with methods to deposit, withdraw, and display bank account details. The main function demonstrates creating a Bank object, calling its methods, and displaying the account details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment 3

Q1
#include <iostream>
#include <conio.h>
class Time
{
int h,m,s;
public:
Time()
{
h=0, m=0; s=0;
}
void setTime();
void show()
{
cout<< h<< ":"<< m<< ":"<< s;
}

//overloading '+' operator


Time operator+(time);
};

Time Time::operator+(Time t1)//operator function


{
Time t;
int a,b;
a = s+t1.s;
t.s = a%60;
b = (a/60)+m+t1.m;
t.m = b%60;
t.h = (b/60)+h+t1.h;
t.h = t.h%12;
return t;
}

void time::setTime()
{
cout << "\n Enter the hour(0-11) ";
cin >> h;
cout << "\n Enter the minute(0-59) ";
cin >> m;
cout << "\n Enter the second(0-59) ";
cin >> s;
}

void main()
{
Time t1,t2,t3;

cout << "\n Enter the first time ";


t1.setTime();
cout << "\n Enter the second time ";
t2.setTime();
t3 = t1 + t2; //adding of two time object using '+'
operator
cout << "\n First time ";
t1.show();
cout << "\n Second time ";
t2.show();
cout << "\n Sum of times ";
t3.show();
getch();
}#include <iostream>
#include <conio.h>
class Time
{
int h,m,s;
public:
Time()
{
h=0, m=0; s=0;
}
void setTime();
void show()
{
cout<< h<< ":"<< m<< ":"<< s;
}

//overloading '+' operator


Time operator+(time);
};

Time Time::operator+(Time t1)//operator function


{
Time t;
int a,b;
a = s+t1.s;
t.s = a%60;
b = (a/60)+m+t1.m;
t.m = b%60;
t.h = (b/60)+h+t1.h;
t.h = t.h%12;
return t;
}
void time::setTime()
{
cout << "\n Enter the hour(0-11) ";
cin >> h;
cout << "\n Enter the minute(0-59) ";
cin >> m;
cout << "\n Enter the second(0-59) ";
cin >> s;
}

void main()
{
Time t1,t2,t3;

cout << "\n Enter the first time ";


t1.setTime();
cout << "\n Enter the second time ";
t2.setTime();
t3 = t1 + t2; //adding of two time object using '+'
operator
cout << "\n First time ";
t1.show();
cout << "\n Second time ";
t2.show();
cout << "\n Sum of times ";
t3.show();
getch();
}
Q2
#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

class bank
{
int acno;
char nm[100], acctype[100];
float bal;
public:
bank(int acc_no, char *name, char *acc_type, float
balance) //Parameterized Constructor
{
acno=acc_no;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit() //depositing an amount
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw() //withdrawing an amount
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display() //displaying the details
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<acno;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;
}
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;
bank b1(acc_no, name, acc_type, balance); //object
is created
b1.deposit(); //
b1.withdraw(); // calling member functions
b1.display(); //
return 0;
}

You might also like