Categories
Uncategorised

Inheritance – Multiple Inheritance

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Inheritance – Multiple Inheritance /

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class person
{
protected:
char name[42], add[100];
int age;
public:
void getdata()
{
cout<<endl<<“Enter name : “;
gets(name);
cout<<“Enter address : “;
gets(add);
cout<<“Enter age : “;
cin>>age;
}
void showdata()
{
cout<<endl<<“Displaying details”
<<endl<<“Name : “<<name
<<endl<<“Address : “<<add
<<endl<<“Age : “<<age<<endl;
}
};

class employee : protected person
{
protected:
int emp_no;
float basic, gross;
public:
void getempdata()
{
getdata();
cout<<“Enter employee number : “;
cin>>emp_no;
cout<<“Enter employee basic salary : “;
cin>>basic;
cout<<“Enter employee gross salary : “;
cin>>gross;
}
void showempdata()
{
showdata();
cout<<“Employee number : “<<emp_no
<<endl<<“Basic salary : “<<basic
<<endl<<“Gross salary : “<<gross<<endl;
}
};

class manager : public employee
{
protected:
char dept[42], des[42];
int numemp;
public:
void getmgrdata()
{
getempdata();
cout<<“Enter department : “;
gets(dept);
cout<<“Enter number of employees : “;
cin>>numemp;
cout<<“Enter designation : “;
gets(des);
}
void showmgrdata()
{
showempdata();
cout<<“Department : “<<dept
<<endl<<“Number of employees : “<<numemp
<<endl<<“Designation : “<<des<<endl;
}
} obj;

void main()
{
clrscr();
cout<<“Enter details for one person”;
obj.getmgrdata();
cout<<endl<<“Displaying details for person”;
obj.showmgrdata();
cout<<endl;
getch();
}

/ Output /

Enter details for one person
Enter name : Arthur Dent
Enter address : 42, Quiz Complex, New Delhi
Enter age : 17
Enter employee number : 42
Enter employee basic salary : 15000
Enter employee gross salary : 42000
Enter department : Research
Enter number of employees : 12
Enter designation : Chief Researcher

Displaying details for person
Displaying details
Name : Arthur Dent
Address : 42, Quiz Complex, New Delhi
Age : 17
Employee number : 42
Basic salary : 15000
Gross salary : 42000
Department : Research
Number of employees : 12

Designation : Chief Researcher

/ Note – According to the teachers, this question was only designed to test whether the students would be able to declare the class properly, rather than testing actual programming skills. */

Categories
Uncategorised

Inheritance – Account Managment

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Inheritance – Account Management /

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class account
{
protected:
int num;
char name[30];
float bal;
public:
void getdata()
{
cout<<endl<<“Enter details for customer account”
<<endl<<“Enter account number : “;
cin>>num;
cout<<“Enter name : “;
gets(name);
cout<<“Enter account balance : “;
cin>>bal;
}
void showdata()
{
cout<<endl<<“Displaying details of customer”
<<endl<<“Account number : “<<num
<<endl<<“Customer name : “<<name
<<endl<<“Account balance : “<<bal;
}
};


class transaction : private account
{
char t;
float amt;
public:
char type;
float amount;
void trans()
{
cout<<endl<<“Enter type of transaction – ‘d’ for “
<<“deposit and ‘w’ for withdrawal : “;
cin>>t;
cout<<“Enter transaction amount : “;
cin>>amt;
update(t, amt);
}
void update(char type, float
amount)
{
if (type==’d’)
{
bal += amount;
cout<<endl<<“Transaction successful”;
showdata();
}
else if (type==’w’)
{
if (bal > amount)
{
bal -= amount;
cout<<endl<<“Transaction successful”;
showdata();
}
else
{
cout<<“Sorry! Not enough balance to carry out transaction”<<endl
<<“Do you want to enter
the withdrawal amount again (y / n)? : “;
char ch;
cin>>ch;
if (ch==’y’)
trans();
}
}
}
void get()
{
getdata();
}
void show()
{
showdata();
} < /span>
};

transaction t[3];

void main()
{
clrscr();
int i, c, n;
char ans;
cout<<“\n\nAccount Management Program”
<<endl<<“Enter initial account data”;
for (i = 0; i < 3; i++)
t[i].get();
do
{
cout<<endl<<“Program Menu”
<<endl<<“1. Display account details”
<<endl<<“2. Carry out transaction”
<<endl<<“Enter your choice : “;
cin>>c;
cout<<“Enter required account number : “;
cin>>n;
switch(c)
{
case 1 : t[n-1].show();
break;
case 2 : t[n-1].trans();
break;
default : cout<<endl<<“You entered a wrong choice!”;
}
cout<<endl<<“Do wish to continue (y / n)? : “;
cin>>ans;
}
while (ans==’y’ || ans==’Y’);

cout<<endl;
getch();
}

/ Output /

Account Management Program
Enter initial account data
Enter details for customer account

Enter account number : 1
Enter name : Ankur Banerjee
Enter account balance : 456

Enter details for customer account
Enter account number : 2
Enter name : Naman Bagga
Enter account balance : 741

Enter details for customer account
Enter account number : 3
Enter name : Rachit Agarwal
Enter account balance : 132

Program Menu
1. Display account details
2. Carry out transaction
Enter your choice : 1
Enter required account number : 2

Displaying details of customer
Account number : 2
Customer name : Naman Bagga
Account balance : 741

Program Menu
1. Display account details
2. Carry out transaction
Enter your choice : 2
Enter required account number : 1

Enter type of transaction – ‘d’
for deposit and ‘w’ for withdrawal : d
Enter transaction amount : 200

Transaction successful
Displaying details of customer
Account number : 1
Customer name : Ankur Banerjee
Account balance : 656

Program Menu
1. Display account details
2. Carry out transaction
Enter your choice : 2
Enter required account number : 3

Enter type of transaction – ‘d’
for deposit and ‘w’ for withdrawal : w
Enter transaction amount : 500
Sorry! Not enough balance to carry
out transaction
Do you want to enter the
withdrawal amount again (y / n)? : y

Enter type of transaction – ‘d’
for deposit
and ‘w’ for withdrawal : w

Enter transaction amount : 100

Transaction successful
Displaying details of customer
Account number : 3
Customer name : Rachit Agarwal
Account balance : 32

/ Program limitations: For the program to work, it is essential to enter sequential account numbers starting from one. Could have been accomplished by automatically assigning numbers too. */