-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_creation.cpp
More file actions
204 lines (145 loc) · 2.95 KB
/
object_creation.cpp
File metadata and controls
204 lines (145 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// class and objects in oops
/*
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
int roll_no;
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Roll No: " << roll_no << endl;
}
};
int main() {
Student s1;
s1.name = "Rahul";
s1.age = 20;
s1.roll_no = 101;
s1.display();
return 0;
}
*/
// without using the oops no object and class
/*
#include <iostream>
using namespace std;
string name;
int age;
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
int main() {
name = "Rahul";
age = 20;
display();
return 0;
}
*/
// more than two objects or students
/*
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
int roll_no;
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Roll No: " << roll_no << endl;
}
};
int main() {
Student s1;
Student s2;
s1.name = "Rahul";
s1.age = 20;
s1.roll_no = 101;
s2.name="sudeep";
s2.age=21;
s2.roll_no=560;
s1.display();
cout<<endl;
s2.display();
return 0;
}
*/
//bank account examples
#include <iostream>
using namespace std;
class bank_account {
private:
int balance;
int account_nbr;
string name;
static int totalaccounts;
public:
// Constructor
bank_account(string accountholder, int initialbalance) {
name = accountholder;
balance = initialbalance;
totalaccounts++;
account_nbr = totalaccounts;
}
// Deposit function
void deposit(int amount) {
balance += amount;
cout << "Deposited: " << amount << endl;
}
// Withdraw function
void withdraw(int amount) {
if (amount <= balance) {
balance -= amount;
cout << "Withdrawn: " << amount << endl;
} else {
cout << "Insufficient balance" << endl;
}
}
// Display account details
void display() {
cout << "Account Number: " << account_nbr << endl;
cout << "Name: " << name << endl;
cout << "Balance: " << balance << endl;
}
};
// Static variable initialization
int bank_account::totalaccounts = 0;
int main() {
bank_account acc1("Rahul", 1000);
bank_account acc2("Anjali", 2000);
acc1.deposit(500);
acc1.withdraw(300);
acc1.display();
cout << endl;
acc2.display();
return 0;
}
// car class and objects oops
/*
#include <iostream>
using namespace std;
class Car {
public:
string brand;
string color;
void start() {
cout << brand << " car has started." << endl;
}
void stop() {
cout << brand << " car has stopped." << endl;
}
};
int main() {
Car c1;
c1.brand = "Toyota";
c1.color = "Red";
c1.start();
c1.stop();
return 0;
}
*/