Skip to content

Commit 28b7b65

Browse files
committed
files added
1 parent 6cd9645 commit 28b7b65

File tree

14 files changed

+973
-0
lines changed

14 files changed

+973
-0
lines changed

Beginners_3/Stage_10/manual.docx

18.3 KB
Binary file not shown.

Beginners_3/Stage_10/task1.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
This C++ program demonstrates the concept of polymorphism using a simple banking system.
3+
4+
There is a base class 'Account' that represents a generic bank account with attributes 'AN' (Account Number) and 'AB' (Account Balance).
5+
It has member functions to get and set these attributes, and also virtual functions 'Print', 'Credit', and 'Debit'.
6+
7+
Two derived classes, 'CurrentAccount' and 'SavingAccount', are created from the 'Account' class.
8+
The 'CurrentAccount' class adds attributes 'SC' (Service Charges) and 'MB' (Minimum Balance) and overrides the 'Print', 'Credit', and 'Debit' functions.
9+
The 'SavingAccount' class adds an attribute 'IR' (Interest Rate) and also overrides the 'Print', 'Credit', and 'Debit' functions.
10+
11+
In the 'main' function, an array of pointers to the 'Account' base class is created, and three different account objects (one 'SavingAccount', one 'CurrentAccount', and one 'Account') are dynamically allocated and assigned to the array elements.
12+
13+
Polymorphism is demonstrated by calling the 'Print', 'Credit', and 'Debit' functions on the array elements, which results in calling the appropriate functions based on the object type.
14+
15+
Sample output:
16+
------------------
17+
Account Number : 0
18+
Account Balance : 0
19+
Interest Rate : 5
20+
Account Number : 0
21+
Account Balance : 0
22+
Services Charges : 100
23+
Minimum Balance : 1000
24+
Account Number : 0
25+
Account Balance : 0
26+
Account destroyed !!
27+
Saving Account destroyed !!
28+
Current Account destroyed !!
29+
------------------
30+
*/
31+
32+
#include <iostream>
33+
using namespace std;
34+
35+
class Account {
36+
private:
37+
int AN, AB;
38+
39+
public:
40+
int getAN() {
41+
return AN;
42+
}
43+
44+
int getAB() {
45+
return AB;
46+
}
47+
48+
void setAN(int acn) {
49+
AN = acn;
50+
}
51+
52+
void setAB(int acb) {
53+
AB = acb;
54+
}
55+
56+
virtual void Print() {
57+
cout << "Account Number : " << AN << endl
58+
<< "Account Balance : " << AB << endl;
59+
}
60+
61+
virtual void Credit(float) {
62+
}
63+
64+
virtual void Debit(float) {
65+
}
66+
67+
virtual ~Account() {
68+
cout << "Account destroyed !!" << endl;
69+
}
70+
};
71+
72+
class CurrentAccount : public Account {
73+
private:
74+
int SC, MB;
75+
76+
public:
77+
CurrentAccount() {
78+
SC = 100; // Default service charges
79+
MB = 1000; // Default minimum balance
80+
}
81+
82+
void Print() {
83+
Account::Print();
84+
cout << "Services Charges : " << SC << endl
85+
<< "Minimum Balance : " << MB << endl;
86+
}
87+
88+
void Credit(float amount) {
89+
setAB(getAB() + amount);
90+
}
91+
92+
void Debit(float amount) {
93+
if (amount <= getAB()) {
94+
setAB(getAB() - amount);
95+
if (getAB() < MB) {
96+
setAB(getAB() - SC);
97+
}
98+
}
99+
}
100+
101+
~CurrentAccount() {
102+
cout << "Current Account destroyed !!" << endl;
103+
}
104+
};
105+
106+
class SavingAccount : public Account {
107+
private:
108+
int IR; // Interest Rate
109+
110+
public:
111+
SavingAccount() {
112+
IR = 5; // Default interest rate
113+
}
114+
115+
void Print() {
116+
Account::Print();
117+
cout << "Interest Rate : " << IR << endl;
118+
}
119+
120+
void Credit(float amount) {
121+
setAB(getAB() + amount);
122+
}
123+
124+
void Debit(float amount) {
125+
if (amount <= getAB()) {
126+
setAB(getAB() - amount);
127+
}
128+
}
129+
130+
~SavingAccount() {
131+
cout << "Saving Account destroyed !!" << endl;
132+
}
133+
};
134+
135+
int main() {
136+
// Array of base pointers
137+
Account** alist = new Account*[3];
138+
alist[0] = new SavingAccount;
139+
alist[1] = new CurrentAccount;
140+
alist[2] = new Account;
141+
142+
// Print data of all accounts polymorphic behavior
143+
for (int i = 0; i < 3; i++)
144+
alist[i]->Print();
145+
146+
// Credit and debit polymorphic behavior
147+
alist[0]->Credit(50);
148+
alist[2]->Debit(333);
149+
150+
// Freeing the dynamically allocated objects
151+
for (int i = 0; i < 3; i++) {
152+
delete alist[i];
153+
}
154+
delete[] alist;
155+
156+
return 0;
157+
}

Beginners_3/Stage_11/manual.docx

16.2 KB
Binary file not shown.

Beginners_3/Stage_11/task1.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
This C++ program demonstrates the use of a function template to compare two values for equality.
3+
4+
The function template 'isEqualTo' is defined, which takes two parameters of the same data type 'T'.
5+
It compares the values of 'a' and 'b' using the '==' operator and returns true if they are equal, otherwise false.
6+
7+
In the 'main' function, we have variables of different data types: 'num1' and 'num2' of type int, 'f1' and 'f2' of type float, and 'ch1' and 'ch2' of type char.
8+
9+
The 'isEqualTo' function is called with each pair of variables, and the result is printed to the console.
10+
11+
Sample output:
12+
------------------
13+
Is num1 equal to num2? 0
14+
Is f1 equal to f2? 1
15+
Is ch1 equal to ch2? 0
16+
------------------
17+
Here, num1 is not equal to num2, f1 is equal to f2, and ch1 is not equal to ch2.
18+
*/
19+
20+
#include <iostream>
21+
using namespace std;
22+
23+
template <typename T>
24+
bool isEqualTo(T a, T b) {
25+
return a == b;
26+
}
27+
28+
int main() {
29+
int num1 = 5, num2 = 10;
30+
float f1 = 3.14, f2 = 3.14;
31+
char ch1 = 'A', ch2 = 'B';
32+
33+
cout << "Is num1 equal to num2? " << isEqualTo(num1, num2) << endl;
34+
cout << "Is f1 equal to f2? " << isEqualTo(f1, f2) << endl;
35+
cout << "Is ch1 equal to ch2? " << isEqualTo(ch1, ch2) << endl;
36+
37+
return 0;
38+
}

Beginners_3/Stage_11/task2.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
This C++ program demonstrates the use of abstract classes and inheritance to create different types of bank accounts.
3+
4+
The 'Account' class is defined as an abstract class with two pure virtual functions: 'Debit' and 'Credit'.
5+
The class also has two member variables: 'accountNumber' to store the account number and 'accountBalance' to store the current balance.
6+
7+
Two derived classes 'CurrentAccount' and 'SavingAccount' inherit from the 'Account' class and provide their implementations of the 'Debit' and 'Credit' functions.
8+
Additionally, a new class 'BasicAccount' is created that also inherits from the 'Account' class and implements its own version of 'Debit' and 'Credit'.
9+
10+
In the 'main' function, we create an array of pointers to 'Account' class objects to hold different types of accounts.
11+
We create instances of 'CurrentAccount', 'SavingAccount', and 'BasicAccount' using dynamic memory allocation.
12+
We then print the account details using the 'Print' function for each account type and finally, free the dynamically allocated memory.
13+
14+
Sample output:
15+
------------------
16+
Account Number: 456
17+
Account Balance: 2000
18+
Minimum Balance: 500
19+
Service Charges: 50
20+
Account Number: 789
21+
Account Balance: 3000
22+
Interest Rate: 5
23+
Account Number: 123
24+
Account Balance: 1000
25+
------------------
26+
Here, we see the details for each account, including the minimum balance and service charges for the current account and the interest rate for the saving account.
27+
The basic account has no additional features, and its details only include the account number and balance.
28+
*/
29+
30+
#include <iostream>
31+
using namespace std;
32+
33+
class Account {
34+
protected: // Change access specifier to protected
35+
int accountNumber;
36+
float accountBalance;
37+
38+
public:
39+
Account(int num, float balance) : accountNumber(num), accountBalance(balance) {}
40+
41+
int getAccountNumber() const {
42+
return accountNumber;
43+
}
44+
45+
float getAccountBalance() const {
46+
return accountBalance;
47+
}
48+
49+
virtual void Debit(float amount) = 0;
50+
virtual void Credit(float amount) = 0;
51+
52+
virtual void Print() {
53+
cout << "Account Number: " << accountNumber << endl;
54+
cout << "Account Balance: " << accountBalance << endl;
55+
}
56+
57+
virtual ~Account() {}
58+
};
59+
60+
class CurrentAccount : public Account {
61+
private:
62+
float serviceCharges;
63+
float minBalance;
64+
65+
public:
66+
CurrentAccount(int num, float balance, float charges, float minBal)
67+
: Account(num, balance), serviceCharges(charges), minBalance(minBal) {}
68+
69+
void Debit(float amount) override {
70+
if (amount <= getAccountBalance()) {
71+
accountBalance -= amount;
72+
} else {
73+
cout << "Insufficient balance!" << endl;
74+
}
75+
}
76+
77+
void Credit(float amount) override {
78+
accountBalance += amount;
79+
if (accountBalance < minBalance) {
80+
accountBalance -= serviceCharges;
81+
}
82+
}
83+
84+
void Print() override {
85+
Account::Print();
86+
cout << "Minimum Balance: " << minBalance << endl;
87+
cout << "Service Charges: " << serviceCharges << endl;
88+
}
89+
};
90+
91+
class SavingAccount : public Account {
92+
private:
93+
float interestRate;
94+
95+
public:
96+
SavingAccount(int num, float balance, float rate)
97+
: Account(num, balance), interestRate(rate) {}
98+
99+
void Debit(float amount) override {
100+
if (amount <= getAccountBalance()) {
101+
accountBalance -= amount;
102+
} else {
103+
cout << "Insufficient balance!" << endl;
104+
}
105+
}
106+
107+
void Credit(float amount) override {
108+
accountBalance += amount;
109+
}
110+
111+
void Print() override {
112+
Account::Print();
113+
cout << "Interest Rate: " << interestRate << endl;
114+
}
115+
};
116+
117+
class BasicAccount : public Account {
118+
public:
119+
BasicAccount(int num, float balance)
120+
: Account(num, balance) {}
121+
122+
void Debit(float amount) override {
123+
if (amount <= getAccountBalance()) {
124+
accountBalance -= amount;
125+
} else {
126+
cout << "Insufficient balance!" << endl;
127+
}
128+
}
129+
130+
void Credit(float amount) override {
131+
accountBalance += amount;
132+
}
133+
};
134+
135+
int main() {
136+
Account* accounts[4];
137+
accounts[0] = new CurrentAccount(456, 2000, 50, 500);
138+
accounts[1] = new SavingAccount(789, 3000, 5);
139+
accounts[2] = new BasicAccount(123, 1000);
140+
141+
for (int i = 0; i < 3; ++i) {
142+
accounts[i]->Print();
143+
delete accounts[i];
144+
}
145+
146+
return 0;
147+
}

Beginners_3/Stage_12/manual.docx

38.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)