c++ language first designed or implemented in 1980 by bjarne stroustrup, from bell labs. that would...

12

Upload: andra-flynn

Post on 03-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983
Page 2: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Page 3: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

The design goals of C++ were the easier development of C programs and an improved support for programmers. The new fundamental concepts in C++ are:

• Objects which communicate through messages and which represent a model of the re• al world through their relations. • The class concept defines user specific data types, related operators, and access

functions. The data encapsulation guarantees that private objects can exclusively be accessed through specified methods.

• Inheritance allows the specification of relations between classes in the sense of abstraction or specialization. Inheritance also helps to avoid multiple implementations.

• A strong type concept inhibits undesired side effects for implicit type conversions like in C. The programmer has to specify a type conversion explicitly in C++.

• Function and operator overloading allow to define existing functions for additional parameter types. The strong type concept allows a function call only with the parameter types defined for the function.

• Efficiency: except for virtual functions, the conceptual extensions in C++ do not cause any runtime delays compared to C. Even for heavily used virtual functions in class libraries there is only a small loss in efficiency.

Page 4: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Programming and Systax

Page 5: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

The main function

int main(int argc, char** argv);

This is where your code begins execution

Number of arguments

Array of strings

argv[0] is the program nameargv[1] through argv[argc-1] are command-line input

Page 6: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Strings

char myString[20];strcpy(myString, "Hello World");

myString[0] = 'H';myString[1] = 'i';myString[2] = '\0';

printf("%s", myString);

A string in C++ is an array of characters

Strings are terminated with the NULL or '\0' character

output: Hi

Page 7: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Parameter Passing

int add(int a, int b) { return a+b;}

int a, b, sum;sum = add(a, b);

pass by value

int add(int *a, int *b) { return *a + *b;}

int a, b, sum;sum = add(&a, &b);

pass by reference

Make a local copyof a and b

Pass pointers that reference a and b. Changes made to a or b will be reflected outside the add routine

Page 8: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Organizational Strategyimage.h Header file: Class definition & function prototypes

.C file: Full function definitions

Main code: Function references

image.C

main.C

void SetAllPixels(const Vec3f &color);

void Image::SetAllPixels(const Vec3f &color) { for (int i = 0; i < width*height; i++) data[i] = color;}

myImage.SetAllPixels(clearColor);

Page 9: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Constructors

Image(int w, int h) { width = w; height = h; data = new Vec3f[w*h];}

Constructors can also take parameters

Page 10: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

Passing Classes as Parameters

bool IsImageGreen(Image img);

If a class instance is passed by value, the copy constructor will be used to make a copy.

Computationally expensive

bool IsImageGreen(Image *img);

It’s much faster to pass by reference:

bool IsImageGreen(Image &img);

or

Page 11: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

The Copy ConstructorImage(Image *img) { width = img->width; height = img->height; data = new Vec3f[width*height]; for (int i=0; i<width*height; i++) data[i] = img->data[i];}

Image(Image *img) { width = img->width; height = img->height; data = img->data;}

A default copy constructor is created automatically,but it is often not what you want:

Page 12: C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983

BY

» Krithee Sirisith ID 49540255