L01a From C to C++
L01a From C to C++
main () {
int radius;
main () {
int radius;
main () {
int radius;
6
C++: Input & Output
C++
#include <iostream>
using namespace std;
int main() {
cout << “Is the answer of 1 + 1 is" << 1 + 1 << ”?” << endl;
return 0;
}
Memory Allocation
#include <iostream>
using namespace std;
int main() {
int num;
double *student_mark;
cout << “How many students do you have?" << endl;
cin >> num;
student_mark = new double [num]
for (int i = 0; i< num; i++)
cout << “Enter the mark of Student “ << i + 1 << “: “;
cin >> student_mark[i];
}
// do something about the mark
delete [] student_mark;
return 0;
}
Pass by Values in C
• Without “Pass by Reference” • And use the
function by
void swap(int* a, int* b)
{
int temp = *a; int main() {
*a = *b; int x,y;
*b = temp; x = 1;
} y = 2;
swap(&x, &y);
}
Pass by Reference in C++
void swap(int& a, int& b) • And use the
{ function by
int temp = a;
a = b;
int main() {
b = temp;
int x,y;
}
x = 1;
y = 2;
swap(x, y);
}
Various Parameter Passing in C++
• Consider three functions • and the calling:
function1(int a) int main()
{ {
a = 10; int x1 = 1;
} int x2 = 1;
int x3 = 1;
function2(int& a) function1(x1);
function2(x2);
{
function3(&x3);
a = 10;
cout << x1 << endl;
} cout << x2 << endl;
cout << x3 << endl;
function3(int* a) }
{ • Output:
*a = 10;
1
}
10
10
Parameter Passing
• function1 is called pass by value
– a and x1 are two separated variables with two different
memory locations (two individual copies of data), changing
a will not make x1 changed
• function2 is called pass by reference
– a and x2 are the same entity (one memory location),
changing a will change x2
• function3 is called pass by pointer
– a is a pointer, and it is pointing at the memory of x3, so
changing the memory pointed by a will change x3
Function Overloading
int max(int a, int b) {
if (a > b) return a;
else return b;
}
14
OOP!
Object-Oriented Programming
How Old People Store Their Money in
the Past?
How do We Store Money Now?
Difference between
Difference between
Cookie Can ATM Machine
• Only Store Money • Provides a lot of functions
• Anyone can access the – Query
money – Withdraw
– Deposit
– Transfer, etc..
• “Clearance/Access levels”
– You can access your own
money
– Bank staffs/technicians can
open up the machine
Bank Account: using C
typedef struct {
int acc_num;
double balance;
} BankAcct;
20
Usage
• Correct usage
BankAcct ba;
initialize(&ba, 1000);
deposit(&ba, 42.2);
withdraw(&ba, 500);
modifies
22
Procedural Languages
In our C implementation
̶ Data (struct) and process (functions) are separate entities
passed into
modifies
23
Conceptual view of OOP
Encapsulation
̶ Representation of data is encapsulated in the object
̶ No direct access to data
̶ Only access using exposed functions
̶ Data + Function abstraction
No direct access
Data
Functions
Data
Functions
25
Classes vs Instances
• Instance
• Class
• - Actual copies you use
- Blueprints
29
Bank Account: using C++
class BankAcct { class follows normal identifier rule
private:
int _acc_num;
double _balance;
public:
int withdraw(double amt);
void deposit(double amt) {
...
}
};
AlanAcc.deposit(1000);
AlanAcc.withdraw(500);
BillyAcc.deposit(9000);
BillyAcc.withdraw(1000);
PeterAcc.deposit(100);
PeterAcc.withdraw(10);
}
Private vs Public
• Outside the class, you can only use/access the
public attributes or functions
• Private attributes/functions are used
internally
– You cannot directly modify
AlanAcc._acc_num = 1000
Instances
Instance attributes/properties
̶ belong to the instance
̶ each instance has their own data
Instance methods
̶ belong to the instance
̶ and operate on its own data
Output:
2,1
Swap Function
void swap(int* a, int* b) {
int temp; a b
temp = *a;
*a = *b;
*b = temp;
}
Swap Function
void swap(int* a, int* b) {
int temp; a b
*a = *b;
temp
*b = temp;
}
Swap Function Summon! by *
temp = *a;
*a = *b;
temp
*b = temp;
}
Swap Function
void swap(int* a, int* b) {
int temp; a b
temp = *a;
Summon! by *
*a = *b;
temp
*b = temp;
}
However
• It is so inconvenient to have more than one
Pokemon!
A Pokeball that has Many Pokemons
• = array!!!!
Array = Pointers
Six Pokemons!!!!
int *arr = new int [6];
One Pokeball
Array = Pointers
• int *arr = new int [6];
• arr[0];
First
Pokemon
• arr[5];
LastPokemon
Note that no need to use “*”
to “summon”. The blanket []
does the trick
Free the memory
int *arr = new int [6];
• Use ”[]”
delete [] arr;
int main() {
int *ptr;
ptr = new int;
*ptr = 10
cout << *ptr << endl;
delete ptr;
}
More C++, OOP