Example Read all integers until EOF and skip non-integers
Command Line Arguments
- Write a program that takes an integer and a string as command line arguments and prints the second integer argument a number of times equal to the first argument. *
e.x.
./args 5 hello
output is expected to be:
Here is the code:
Here are some notes that I am making after the lecture
- Above, we passed
argc
andarv
to main, these are the command line arguments we can pass to main. Again, command-line arguments are given after the name of the program in command-line shell Operating Systems. Essentially, the command line arguments you used in bash. - To pass command line arguments in C/C++, we need the
argc
andargv
. argc
(which can be though of as argument count) stores the number of command line arguments passed by the user, including the name of the program. So, its basically always thenumber of arguments + 1
argv
(which can be thought of as argument vector) is an array of character pointers listing all of the arguments.- If
argc > 0
, then the array elements fromargv[0] to argv[argc-1]
will contain pointers to strings argv[0]
is the name of the program, after that, every element inargv
is command line argument
In fact, when we open a C++
file in Xcode, the main you see looks like this:
Good practice, implement cat
Default function parameters
NOTE: Default parameters must be last
e.g.
Overloading
- Same function name → multiple possible data parameters - used in classes
e.g.
- Can have functions with the same name, so long as they differ in number or type of parameters
- Differing in return type is NOT enough
- *Can overload operators as well **
e.g.
We’ve already seen overloading
- Another example is
std::cin
andstd::cout
, can read in or output strings, integers… many data types - This is overloading
Structures
Constants
- Declare as many things as you can as
const
, it helps you catch errors
- *Parameter Passing **
- References (Very important)
- we say that z is an
alias
for y - It is important to recognize that references are NOT
pointers
- They do behave like
const
pointers with automatic dereferencing - So, in the above,
z++
will incrementy
- if i add:
NOTE: You can never change what z is a reference for
Also:
THINGS YOU CANNOT DO WITH AN L-VALUE REFERENCE
- Cannot leave references uninitialized
- cannot just say
int &bruh
- cannot just say
- Must be initialized with something that has an address
- i.e. an l-value
- so
int &lol = 3
is **not allowed ** - Cannot be referencing a **r-value **
- create a pointer to a reference (it might not even have an address, so how can we have a pointer to it)
- however, we CAN have a reference to a pointer
- cannot create a
reference
to areference
- cannot create an array of references
- e.g.
int & r[3] = {n,n,n}
- e.g.
So, what CAN we do with references?
- So, why does
cin>>x
work?- because
>>
takes x by reference, i.e std::istream &operator>>(std::istream &n, in &x)
- because
Prefer pass by const
reference over pass by value, unless, the function needs to make a copy anyways, then pass by value
END OF LECTURE 6