Skip to content

Commit 21b989b

Browse files
committed
add: cpp-programming-questions 13_virtual_abstract 05
1 parent 15f0c4c commit 21b989b

File tree

3 files changed

+139
-6
lines changed

3 files changed

+139
-6
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"typeinfo": "cpp"
4+
}
5+
}

13_virtual_function_and_abstract_class/05_bank.cpp

Lines changed: 134 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// // Design a class hierarchy for a banking system with a base class Account and two derived classes SavingsAccount and CurrentAccount. Each class should have data members for account balance, account number, and an interest rate (for savings account). Implement pure virtual functions for deposit, withdraw, and display account details in Account class. Demonstrate the use of these classes by creating objects and performing transactions.
1+
// // Design a class hierarchy for a banking system with a base class Account and two derived classes SavingsAccount and CurrentAccount. Each class should have data members for account balance, account number, and an interest rate (for savings account). Implement pure virtual functions for set account number, deposit, withdraw, and display account details in Account class. Demonstrate the use of these classes by creating objects and performing transactions.
22

33
// // Header files
44
#include <iostream>
@@ -13,6 +13,7 @@ class Account
1313
{
1414
public:
1515
// // static member variable
16+
static const int MAX_CHARS_IN_NAME = 31;
1617
static const int MAX_DIGITS_IN_ACCOUNT_NUM = 6;
1718

1819
protected:
@@ -22,6 +23,7 @@ class Account
2223

2324
public:
2425
// // pure virtual functions to be overridden by derived classes
26+
virtual void setAccountNumber(const char *) = 0;
2527
virtual void deposit(double) = 0;
2628
virtual void withdraw(double) = 0;
2729
virtual void displayAccountDetails() const = 0;
@@ -35,8 +37,31 @@ class SavingAccount : public Account
3537
static float rateOfInterest;
3638

3739
public:
40+
SavingAccount()
41+
{
42+
accountBalance = 0;
43+
}
44+
45+
// // override base class setAccountNumber function
46+
void setAccountNumber(const char *accountNumber) override
47+
{
48+
int length = strlen(accountNumber);
49+
50+
this->accountNumber = new char[length > MAX_DIGITS_IN_ACCOUNT_NUM - 1 ? MAX_DIGITS_IN_ACCOUNT_NUM : length + 1];
51+
52+
if (length > MAX_DIGITS_IN_ACCOUNT_NUM - 1)
53+
{
54+
strncpy(this->accountNumber, accountNumber, MAX_DIGITS_IN_ACCOUNT_NUM - 1);
55+
this->accountNumber[MAX_DIGITS_IN_ACCOUNT_NUM - 1] = 0; // // terminate with null character
56+
}
57+
else
58+
{
59+
strcpy(this->accountNumber, accountNumber);
60+
}
61+
}
62+
3863
// // override base class deposit function
39-
void deposit(double amount)
64+
void deposit(double amount) override
4065
{
4166
// // invalid amount
4267
if (amount < 0)
@@ -47,11 +72,13 @@ class SavingAccount : public Account
4772

4873
// // add amount in accountBalance
4974
accountBalance += amount;
50-
cout << "\nMoney Deposited Successfully";
75+
cout << endl
76+
<< amount << "Rs. "
77+
<< " Have Deposited Successfully...";
5178
}
5279

5380
// // override base class withdraw function
54-
void deposit(double amount)
81+
void withdraw(double amount) override
5582
{
5683
// // invalid amount
5784
if (amount < 0)
@@ -67,14 +94,17 @@ class SavingAccount : public Account
6794

6895
// // add amount in accountBalance
6996
accountBalance -= amount;
70-
cout << "\nMoney Has Withdrawal Successfully";
97+
cout << endl
98+
<< amount << "Rs."
99+
<< " Have Withdrawal Successfully...";
71100
}
72101

73102
// // override base class displayAccountDetails function
74-
void displayAccountDetails()
103+
void displayAccountDetails() const override
75104
{
76105
cout << "\nAccount Number => " << accountNumber;
77106
cout << "\nAccount Balance => " << accountBalance;
107+
cout << "\nRate of Interest => " << rateOfInterest;
78108
}
79109

80110
// // static member function to set rate of interest
@@ -90,9 +120,107 @@ class SavingAccount : public Account
90120
}
91121
};
92122

123+
// // define static member variable rateOfInterest
124+
float SavingAccount::rateOfInterest;
125+
126+
// // define class CurrentAccount by inheriting an abstract class Account
127+
class CurrentAccount : public Account
128+
{
129+
public:
130+
// // constructors
131+
CurrentAccount()
132+
{
133+
accountBalance = 0;
134+
}
135+
136+
// // override base class setAccountNumber function
137+
void setAccountNumber(const char *accountNumber) override
138+
{
139+
int length = strlen(accountNumber);
140+
141+
this->accountNumber = new char[length > MAX_DIGITS_IN_ACCOUNT_NUM - 1 ? MAX_DIGITS_IN_ACCOUNT_NUM : length + 1];
142+
143+
if (length > MAX_DIGITS_IN_ACCOUNT_NUM - 1)
144+
{
145+
strncpy(this->accountNumber, accountNumber, MAX_DIGITS_IN_ACCOUNT_NUM - 1);
146+
this->accountNumber[MAX_DIGITS_IN_ACCOUNT_NUM - 1] = 0; // // terminate with null character
147+
}
148+
else
149+
{
150+
strcpy(this->accountNumber, accountNumber);
151+
}
152+
}
153+
154+
// // override base class deposit function
155+
void deposit(double amount) override
156+
{
157+
// // invalid amount
158+
if (amount < 0)
159+
{
160+
cout << "\n!!! Given Amount is Invalid...";
161+
return;
162+
}
163+
164+
// // add amount in accountBalance
165+
accountBalance += amount;
166+
cout << endl
167+
<< amount << "Rs. "
168+
<< " Have Deposited Successfully...";
169+
}
170+
171+
// // override base class withdraw function
172+
void withdraw(double amount) override
173+
{
174+
// // invalid amount
175+
if (amount < 0)
176+
{
177+
cout << "\n!!! Given Amount is Invalid...";
178+
return;
179+
}
180+
else if (accountBalance < amount) // // Insufficient Balance
181+
{
182+
cout << "\n!!! Insufficient Balance...";
183+
return;
184+
}
185+
186+
// // add amount in accountBalance
187+
accountBalance -= amount;
188+
cout << endl
189+
<< amount << "Rs."
190+
<< " Have Withdrawal Successfully...";
191+
}
192+
193+
// // override base class displayAccountDetails function
194+
void displayAccountDetails() const override
195+
{
196+
cout << "\nAccount Number => " << accountNumber;
197+
cout << "\nAccount Balance => " << accountBalance;
198+
}
199+
};
200+
93201
// // Main Function Start
94202
int main()
95203
{
204+
SavingAccount sa1;
205+
sa1.setAccountNumber("27038");
206+
sa1.setRateOfInterest(2.5);
207+
cout << "\n\n>>>>>>> Saving Account Details Before Any Transaction <<<<<<<<";
208+
sa1.displayAccountDetails();
209+
cout << "\n\n>>>>>>> Transactions <<<<<<<<";
210+
sa1.deposit(570);
211+
sa1.withdraw(5);
212+
cout << "\n\n>>>>>>> Saving Account Details After Transactions <<<<<<<<";
213+
sa1.displayAccountDetails();
214+
215+
CurrentAccount ca1;
216+
ca1.setAccountNumber("272091");
217+
cout << "\n\n>>>>>>> Current Account Details Before Any Transaction <<<<<<<<";
218+
ca1.displayAccountDetails();
219+
cout << "\n\n>>>>>>> Transactions <<<<<<<<";
220+
ca1.deposit(100);
221+
ca1.withdraw(85);
222+
cout << "\n\n>>>>>>> Current Account Details After Transactions <<<<<<<<";
223+
ca1.displayAccountDetails();
96224

97225
cout << endl; // Add new line
98226
getch();
53.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)