Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.

Similar presentations


Presentation on theme: "Copyright  Hannu Laine C++-programming Part 1 Hannu Laine."— Presentation transcript:

1 Copyright  Hannu Laine C++-programming Part 1 Hannu Laine

2 HL2 C language in included in C++ (almost). C++ contains additional tools for the management of complexity. The most important tool of these is the class, that makes possible object based and object oriented programming. What is ++ in C? C C++ f.eg.. void f(); There are many other “smaller” improvements in C++. All of these new features support in a way the management of the complexity: Some of these things are: Function overloading Operator overloading Default parameters Reference parameters Inline-functions etc

3 HL3 There are some differences in function declarations between C and C++. C-language Function prototype void f(); Nothing is determined about parameters. So you can call it for example in the following ways: f(3), f(1.1, 2.2) and f(). C++-language Function prototype void f(); Function has no parameters. So the only possible way to call it is in the form f(); This is same than void f(void) C- and C++-language Function prototype void f(void); Function has no parameters. So the only possible way to call it is in the form f(); C++-language Function prototype void f(...); Nothing is determined about parameters. So you can call it for example in the following ways: f(3), f(1.1, 2.2) and f(). Remark. The new versions of C-language often support the... as in C++. Function prototype f()

4 HL4 Let’s assume that we have two types Tpoint and Ttriangle and we need the functions that read information into variables of these types. These types and functions are used in the same program. C-language We need different names for the functions: void read_point(Tpoint *point); void read_triangle(Ttriangle *triangle); int main (void) { Tpoint p; Ttriangle t; read_point(&p); read_triangle(&t);... } C++-language We can use function overloading and define several functions that all have the same name: void read(Tpoint *point); void read(Ttriangle *triangle); int main (void) { Tpoint p; Ttriangle t; read(&p); read(&t);... } Function overloading

5 HL5 Let’s assume that we want to implement an ADT for complex number calculations. C-language We could write and ADT that we could use in the following way: #include “complex.h” void main(void) { Tcomplex a, b, c; read_complex(&a); read_complex(&b); c = sum_complex(a,b); print_complex(c); } C++-language Now we can use operator overloading in the implementation of the ADT. This means that we can do input, output and addition of complex numbers in the way we are used to use for other numbers (ints or floats). This means that our main program becomes generic. #include “complex.h” void main(void) { Tcomplex a, b, c; cin >> a; cin >> b; c = a + b; cout << c; } Operator overloading Remark. This program is generic. It works even for floats when only typename is changed

6 HL6 Example. (We now assume that x is a column number and y is a row number) //function prototype void outputxy (const char *text, int x=-1, int y=-1); //How to use the function in the main outputxy (“C-language”, 35, 3); //output goes // to column 35 at row 3 outputxy (“C++-language”); //the new text follows // the previous one outputxy (“Pascal-language”, 10); // the output // goes to column 10 at the current row //The implementation of the function void output (const char *text, int x, int y) { if (x==-1) x = wherex(); if (y==-1) y = wherey(); gotoxy(x,y); cout << text; } Default parameters

7 HL7 In old C-language standards variable declarations must located in the beginning of the code block. In C++-language, variable declarations are allowed to be done where ever in the block (before it is used). Example 1. int main(void) { int a = 1; a = a + 1; int b = 2; // This was syntax error in old C standard but not in C++ printf(”\n %d”, a + b); return 1; } Example 2. int main(void) { int a = 1; a = a + 1; { int b = 2; // Always Ok in C and in C++ printf(”\n %d”, a + b); } return 1; } Variable declarations Remark. This transparent is preserved for giving information from the history. Currently C and C++ are similar in this respect.

8 HL8 In C-language int type is used as boolean values. In C++-language there is a type bool for this purpose. The variable of type bool can have values false or true. Example. int main( ) { bool rich; int money = 20000; rich = money > 10000; //tai rich = true; if (rich) // or if (rich == true) spend_money(); return 0; } Sometimes it is useful to use int as boolean and as a amount at the same time. Example. int main ( ) { int coins = 100; while (coins) { play(); coins - -; } return 0; } Data type bool

9 HL9 Reference parameters make easier to use and develop the functions that modify their parameters. The compiler takes care that reference parameters are regarded internally as pointers. The request to the compiler is given in one place. This place is the function headline. Example and comparison to pointer parameters: Reference parameters and returning a reference See Part 1b Reference parameters and comparison to pointer parameters Function can return a reference. Example and comparison to the return of the pointer: Seet Part 1b Returning a reference and comparison to the returning of pointer

10 HL10 Using dynamic memory Simple variable int *p; p = (int*)malloc(sizeof(int)); *p = 7;... free(p); Array int *a; a = (int*)malloc(n*sizeof(int)); a[0] = 1; a[1] = 2;... free(a); Simple variable int *p; p = new int; *p = 7; // or p = new int (7);... delete p; Array int *a; a = new int [n]; a[0] = 1; a[1] = 2;... delete a; CC++ Differences between function malloc and operator new: 1) new calls constructor. 2) new is simpler to use. 3) malloc is a function, new is an operator. Differences between function free and operator delete: 1) delete calls destructor. 2) free is a function, delete is an operator.

11 HL There are many build-in classes and objects in C++ for different purposes like: - Input and output (keyboard and display) - File handling - String manipulation - Storing data items in different containers (container classes). Using build-in input and output objects Input and output Input and output objects cin and cout are ready-to- use objects (they are instances of classes, declared in the system). Input object cin is an instance of the class istream and output object cout is an instance of the class ostream.. These objects have many member functions and overloaded operators. The most widely used operators are input operator >> and output operator <<. Example. float a; cin >> a ; // input is read from the keyboard, // converted into float format and stored in // variable a cout << a ; // the contents of variable a is // converted to characters and displayed on // the screen. Operator overloading, function overloading and reference parameters are need

12 HL12 Initialization of a variable C-language: int a = 0; // This is an initialization int b; b = 0; // This is an assignment C++-language: Initialization and assignment can be done as above. In C++ there is also another way to initialize a variable: int a(0); // This is an initialization and works only in C++. In C++ it is also possible to initialize a variable that is allocated from the dynamic memory (new): int *p; p = new int(7); // Allocates and initializes int-variable Comments Strictly speaking // is a comment only in C++, not in C. Some C-compilers give a warning if you use // comments in C- program. C-compilers always accept /* …*/ comments. Headers C++-headers does not have.h extension. If you use C- libraries in C++ use header name that you get by dropping the extension and by prefixing with c. Example #include // C++ header #include // Vastaa C:n headeriä stdlib.h #include // Vastaa C:n headeriä string.h #include // C++ has it’s own string class More differences 1

13 HL13 Name spaces All names (identifiers) that belong to C++ are defined in the namespace std. To use names in the namespace is possible only by specifying also the namespace. There are two options to do that. Option 1. You can specify the namespace every time you use the name: std::cout << ”This goes to the screen” << std::endl; Option 2. You and tell in the beginning of the program what namespaces you are going to use: using namespace std; … cout << ””This goes to the screen” << endl; Prototypes of the functions C++-language: Prototype of the function is required before the function call. Otherwise you get an error message from the compiler. C-language: C-compiler compiles the function call even it has no prototype. The compiler cannot check whether you have called the function in the correct way. The compiled program can work in totally wrong way, if you have called it in wrong way (parameter types are not correct for example). (Automatic type casts) C++-language is more strict in doing automatic type casts than C. Examples: void *p; double *pd,,int *pi; p = pd; // OK in C and in C++ pd = p; // OK in C but error in C++ pd = pi; // warning in C but error in C++ More differences 2


Download ppt "Copyright  Hannu Laine C++-programming Part 1 Hannu Laine."

Similar presentations


Ads by Google