c++_1
c++_1
boolean a= true;
a= 6 > 5;
boolean b= false;
if(!b)
std::cout << “Hello!\n”;
DIFFERENCES
INPUT/OUTPUT
#include <iostream>
The new and delete keywords are used to allocate and free
memory
They are "object-aware" so you'd better use them instead
of malloc and free
üIn any case, never cross the streams (new/free or malloc/delete)
int x;
int& foo = x; Foo has type “reference to int”
foo = 42;
int num= 1;
int& test();
5
int main() {
test() = 5;
cout << num;
return 0;
} return num + 1; NO!
int& test() {
return num;
}
As a general rule:
üUse references in function parameters and return types to
define useful and self-documenting interfaces
üUse pointers to implement algorithms and data structures
DEFAULT PARAMETERS
cout << foo(1) << endl << foo(1,2) << endl <<
foo(1,2,3) << endl;
#include <iostream>
using namespace std;
int main() {
namespace C {
var x= 6;
}
cout << “Print “ << A::x;
cout << “ “ << C::x << std::endl;
}
NAMESPACE TO AVOID COLLISIONS
#include <iostream>
using namespace std;
namespace {
#include <iostream> int x= 8;
using namespace std; }
namespace A {
static int x= 8; int main() {
} cout << “Print “ << x << endl;
}
int main() {
using namespace A;
cout << “Print “ << x << endl;
}
NAME COLLISION FOR NAMESPACES
Yes No
#include <iostream> #include <iostream>
using namespace std; using namespace std;
namespace A { namespace A {
static int x= 8; static int x= 8;
} }
namespace A { namespace A {
static int y= 8; static int x= 8;
} }
try {
float *array = new float[a];
}
catch( std::bad_alloc e ) {
std::cerr << “I caught: ” << e.what() << std::endl;
}
I caught: std::bad_alloc
CREATING EXCEPTIONS
#include <stdexcept>
};
EXCEPTIONS
The term exception is typically used in a specific sense
to denote a data structure storing information about an
exceptional condition
One mechanism to transfer control, or raise an
exception, is known as a throw
From the point of view of the author of a routine, raising
an exception is a useful way to signal that a routine could
not execute normally - for example, when an input
argument is invalid
Lisp ’60s-’70s
Table-driven approach: it creates static tables at compile
time and link time that relate ranges of the program
counter to the program state with respect to exception
handling. Compiler + runtime system
OBJECT-ORIENTED
PROGRAMMING
CLASSES
class Foo {
int attribute;
int function( void ) { };
};
Foo foo; foo.attribute = 1; // WRONG
Bar bar; bar.attribute = 1; // OK
struct Bar {
int attribute;
int function( void ) { };
};
ACCESS SPECIFIERS
public:
void set_values (int x, int y) {
width = x; height = y;
}
int area() {
return width*height;
} area: 12
};
int main () {
Rectangle rect;
rect.set_values (3,4);
std::cout << "area: " << rect.area();
return 0;
}
CONSTRUCTOR
public:
// constructors:
MyString() : ptr(new string) {}
MyString(const string& str) : ptr(new string(str)) {}
// destructor:
~MyString () {delete ptr;}
// access content:
const string& content() const {return *ptr;}
};
int main () {
Example4 foo;
Example4 bar ("Example");
cout << "bar's content: " << bar.content() << '\n';
return 0;
}
SOME MORE DIFFERENCES
int i; int j;
(i, j) = 1; // Valid C++, invalid C
SOME MORE DIFFERENCES
int i;
int i= 3;
int main() {
cout << “Print “ << i << endl;
}
C++ does not allow this. Only one definition of any given
variable is allowed within a program
TO BE CONTINUED…E.G.,
Overloading operators
Keyword this
Static and const function members
Copy constructor
Friend functions and classes
Inheritance (multiple)
Polymorphism
Templates
HOW TO START
PROGRAMMING IN C++
G++
INSTALL G++
Linux: sudo apt-get update
üto update your package list with the most recent version of
g++
sudo apt-get install g++
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!\n";
return 0;
}
int main(void) {
printf("Hello world %d\n", i);