second
Object-Oriented Programming BSC.CSIT 2078
Write a program according to the specification given below:
- Create a class Account with data members acc no, balance, and min_balance(static)
- Include methods for reading and displaying values of objects
- Define static member function to display min_balance
- Create array of objects to store data of 5 accounts and read and display values of each object
#include <iostream>using namespace std;
class Account{ int acc_no; float balance; static int min_balance; public: void read(){ cout<<"Enter account no : "; cin>>acc_no; cout<<"Enter balance : "; cin>>balance; } static void display_min_balance(){ cout<<"Minimum balance is " <<min_balance<<endl; } static void read_min_balance(){ cout<<" Enter Minimum balance = "; cin>>min_balance; } void display(){ cout<<"Account no = "<<acc_no<<endl; cout<<"Balance = "<<balance<<endl; }};int Account::min_balance;
int main(){ Account a[5];
//calling static function Account::read_min_balance(); // OR , a[0].read_min_balance();
//reading data for(int i=0;i<5;i++){ a[i].read(); }
//displaying data for(int i=0;i<5;i++){ a[i].display(); a[i].display_min_balance(); } return 0;}
#include <iostream>
using namespace std;
class Account{
int acc_no;
float balance;
static int min_balance;
public:
void read(){
cout<<"Enter account no : ";
cin>>acc_no;
cout<<"Enter balance : ";
cin>>balance;
}
static void display_min_balance(){
cout<<"Minimum balance is "
<<min_balance<<endl;
}
static void read_min_balance(){
cout<<" Enter Minimum balance = ";
cin>>min_balance;
}
void display(){
cout<<"Account no = "<<acc_no<<endl;
cout<<"Balance = "<<balance<<endl;
}
};
int Account::min_balance;
int main(){
Account a[5];
//calling static function
Account::read_min_balance();
// OR , a[0].read_min_balance();
//reading data
for(int i=0;i<5;i++){
a[i].read();
}
//displaying data
for(int i=0;i<5;i++){
a[i].display();
a[i].display_min_balance();
}
return 0;
}
OUTPUT:
Enter Minimum balance = 5
Enter account no : 5555
Enter balance : 55
Enter account no : 5656
Enter balance : 36
Enter account no : 11
Enter balance : 256
Enter account no : 7979
Enter balance : 223
Enter account no : 46565
Enter balance : 2222
Account no = 5555
Balance = 55
Minimum balance is 5
Account no = 5656
Balance = 36
Minimum balance is 5
Account no = 11
Balance = 256
Minimum balance is 5
Account no = 7979
Balance = 223
Minimum balance is 5
Account no = 46565
Balance = 2222
Minimum balance is 5
When class templates are useful? How can you define a class that can implement stack with integer as well as sack of strings? Illustrate with example. ( C++ )
Class Templates are useful because of the following reasons:
- No code repetition
- Less chance of errors and bugs
- Object code gets reduced and hence the execution speed is enhanced
#include <iostream>
using namespace std;
const int MAX = 5;
template <class T>
class stack
{
int top = 0;
T st[MAX];
public:
bool is_full()
{
if (top == MAX)
{
return true;
}
else
{
return false;
}
}
bool is_empty()
{
if (top == 0)
{
return true;
}
return false;
}
void push(T k)
{
if (!is_full())
{
st[top] = k;
top++;
cout<<"Pushed "<< k <<" to the stack "<<endl;
}
else
{
cout<<"Stack full cant push anymore."<<endl;
}
}
void pop()
{
if (!is_empty())
{
cout<<st[top-1]<<"popped from stack"<<endl;
top--;
}
else
{
cout<<"Cannot pop . already at the end"<<endl;
}
}
};
int main()
{
stack<int> s;
s.push(23);
s.pop();
s.pop();
s.push(47);
s.push(50);
s.push(9);
s.push(5);
s.pop();
s.push(11);
s.push(6);
s.push(6);
stack<string> s1;
string name = "abid";
s1.push(name);
s1.pop();
return 0;
}
OUTPUT:
Pushed 23 to the stack
23 popped from stack
Cannot pop . already at the end
Pushed 47 to the stack
Pushed 50 to the stack
Pushed 9 to the stack
Pushed 5 to the stack
5 popped from stack
Pushed 11 to the stack
Pushed 6 to the stack
Stack full cant push anymore.
Pushed abid to the stack
abid popped from stack