C++: Getting Started: ... and Some More
C++: Getting Started: ... and Some More
C++: Getting Started: ... and Some More
A do-nothing program
/* A sample program in C++ with comments */
int main () { cout << "Hello there! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a C++ program return 0; }
Demonstrates standard ways to output text Introduces standard headers and the concept of namespaces
Dissecting...
Relevant comments are important for maintainability. also a must have another form:
intmain(intargc,char**argv)
Do-nothing program #2
/* A sample program in C++ with comments */
#include <iostream> int main () { std::cout << "Hello there!\n"; std::cout << "I'm a C++ program" << std::endl; return 0; }
Note...
which entity do I belong to? std::cout There's a class SR operator Can be used to unhide, as we shall see as we talk about globals
Quick declarators
The above declaration shows use of templates (more on that later in the course)
Know these?
volatile int i = 0; register c = 11; char **names; const int *j = &i; extern int userID; extern const volatile int clock;
Volatiles, registers
volatiles are variables values of which can change implicitly, i.e. without an explicit assignment statement in the program code.
it's a request, so it may fail extremely fast access, since it's not in RAM
Globals are to be avoided (the OO school of thought) Called Global because, it's scope starts where it is declared
Creates confusion with locals Can be inadvertently changed. causing endless frustrations, broken relationships, hair loss, weight gain, etc etc (not really)
Global Example
#include<iostream> intk=10,i=0;
That said...
meaning, data and code are tightly coupled and thus, C++ programmers tend to not use globals
Function calls
Difference between declaration and definition? declaration allocates no memory, definition does. Function calling and returning process
For reference and value parameters For inline functions (in C-lingo, Macros)
Calling process
allocate memory for local variables and/or arguments (important distinction) copy value or address or arguments transfer control of program to function address.
Return process
Check the return address for validity Transfer control to return address if valid
Demo
intCallDemoFunction(intsomeArg) { std::cout<<"JustsomeArgwhichis"<<someArg<<std::endl; return12; } intmain() { inttoPass=10; CallDemoFunction(toPass); ...
Points to note
Meaning, changes to arguments may not stick or not. Why? Since the stack frame is destroyed, so are the copies (and hence local changes)
Instead, must pass address of arguments This is Pass by reference Otherwise, it's Pass by value To reiterate
So how do we?
C++ is simpler
No-Worky version
Passing by value:
voidswap(intx,inty) { intz=x; x=y; y=z; } ... inti=10,j=20; swap(i,j);
Complete Listing
// //memory_sample.cc // #include<iostream> usingnamespacestd; voidswap(int&x,int&y) { intz=x; x=y; y=z; } intmain() { intn1=10,n2=100; swap(n1,n2); cout<<"N1is"<<n1<<"andN2is"<<n2<< endl; return0; }
With g++...
g++ -o memory_sample memory_sample.cc -g2 -Wall ./memory_sample
The flags
-Wall: means with all warnings -g: include debugging information. 0,1,2,3 indicates increasing degrees of information. *VERY USEFUL* -o: name of the executable output. if omitted, defaults to a.out.