Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

C+ +

Bjarne Stroustrup C+ + Creator

Resources
C++ primer plus by Stephen Prata
(pdf: https://www.google.co.in/# q=C%2B%2B+Primer+plus )
The C++ Programming Language by Bjarne Stroustrup
Programming Principles and Practices by Bjarne Stroustrup
Effective C++ by Scott Meyers
Effective STL
http://www.cplusplus.com/doc/tutorial /
https://isocpp.org/faq
Stanford C Lectures: https://
www.youtube.com/watch?v=Ps8jOj7diA0&list=PLjn3WmBeabPO
UzxcCkzk4jYMGRZMZ6ylF

C+ + Features
Abstraction
Inheritance
Polymorphism
STL Library (Containers)
Overloading
Overriding

C+ + Com piled Language


Compiler processes source text, generates object files
Linker links object files and creates executable program
Source
file 1

compil
e

Object file
1

Source
file 2

compil
e

Object file
2

link

Executable
file

Statically typed language. Compiler must know type of


every entity (object, value, name and expression) at its
point of use
Type of an object determines set of operations

D ata Types and O perations


Declaration: statement that introduces a name into program
Type: defines a set of possible values and a set of operations
Object: memory that holds a value of some type
Value: set of bits (bit pattern) interpreted according to a type
Variable: a named object
C++ offers built-in data types: integer, float, double, char,
bool, string
Arithmetic Operations: x+y, ++x, x-y, --y, x*y, x/y, x%y
Comparison Operators: x==y, x!=y, x<y, x>y, x<=y, x>=y

Pointers, Arrays, and References


Object resides at a specific address in memory
Object can be accessed if its address and type is known
Pointers & References: language construct to hold and
use addresses
void * can point to an object of an unknown type
void * is used in generic programming
pointer to function or pointer to member can not be
assigned to void*
Array: contiguous memory allocations of same data type

Contd
Array Initialization:
int v1[] = {1,2,3,4}, int v2[4] = {1, 2}
v1 = v2 // error: no array assignment
String Literals: character sequence enclosed with
double quotes, terminated by null character
this is a string, sizeof(Bohr) = 5, strlen(Bohr) = 4
Literal string storage is different than character array

Contd
Literal String assignment:
const char *p = Plato // pointer to const char
p = zinga

char *p = apple

p[1] = c

Character array initialization:


char p[] = zeno
pointer to char

=> char * const p = zeno // const

p[1] = a p++
char *q = p // implicit conversion of char[] to char *
sizeof (q) is different than sizeof(p)

Reference O perator &


Pointer allows to pass large amounts of data around at low cost
Instead of copying data simply pass its address and access data
Pointer usage differs from object name usage
struct s {
int a; int b;
};
struct s1, *ptr;
s1.a

ptr->b

Contd
Pointer drawbacks:

it can point to different objects at different times


must be more careful when using pointers than using an object
pointer can be null or it points to an unexpected object at given
point
Reference mechanism can address above problem
Reference is an alias for an object
Implemented to hold a machine address of an object
does not impose performance overhead compared to pointer

Contd
Reference differs from pointer in following way:
to access memory reference syntax is same as object name
struct s &ref = a; // reference must be initialized to some
object
ref.b = 5
reference always refers to the object to which it was initialized
struct s &ref = b
there is no null reference
struct s &ref

END

You might also like