You are on page 1of 24

C++: Getting started

...and some more.

A do-nothing program
/* A sample program in C++ with comments */

#include <iostream> using namespace std;

int main () { cout << "Hello there! "; // prints Hello World!

cout << "I'm a C++ program"; // prints I'm a C++ program return 0; }

About this program

Very simple non-OO program.

Difference with Java, where you must create classes

Demonstrates standard ways to output text Introduces standard headers and the concept of namespaces

Dissecting...

Use // or /* ... */ to put comments.

Relevant comments are important for maintainability. also a must have another form:
intmain(intargc,char**argv)

note the signature of themain program


Must include the standard I/O header for input/output

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...

The scope resolution (SR) operator ::

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

In this case, namespace association


Quick declarators

Declarators designate data objects or function/methods


inti=10;charc='A';strings=waah!; template<typenameT>voidSortData(T*data);

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.

for example a hardware interrupt can change volatiles

variables declared as registers are placed in the CPU registers


it's a request, so it may fail extremely fast access, since it's not in RAM

Memory structure (simplified)


High address Command line arguments Environment variables

Stack (Function/method arguments, locals, return addresses)

Free store (Heap) Dynamically allocated memory

Code (Read-only) Uninitialized/Initialized data (No eXecute) Low address

Locals vs Globals vs Members

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;

voidMessMeUp() { intk=11; for(i=0;i<N;i++) //Dosomething... }

That said...

Globals are important in C because of no data association C++ provides classes


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

Push return address

saves the where was I information

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

Pop the stack frame

DESTROYS local variables automatically (unless there's dynamic memory allocated)

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

Copies of arguments may be created, or not.

Meaning, changes to arguments may not stick or not. Why? Since the stack frame is destroyed, so are the copies (and hence local changes)

To make changes persistent, must NOT be creating copies of arguments

Value vs Reference Params

Instead, must pass address of arguments This is Pass by reference Otherwise, it's Pass by value To reiterate

To make permanent changes to arguments, pass by reference Otherwise, pass by value

So how do we?

Swap example: C-style first


voidswap(int*x,int*y) { intz=*x; *x=*y; *y=*z; } ... inti=10,j=20; swap(&i,&j);

C++ is simpler

C++ has Reference types


voidswap(int&x,int&y) { intz=x; x=y; y=z; } ... inti=10,j=20; swap(i,j);

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.

You might also like