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

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Copyright  Hannu Laine C++-programming Part 5 Strings.
Kernighan/Ritchie: Kelley/Pohl:
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 10.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Guide To UNIX Using Linux Third Edition
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Basic Elements of C++ Chapter 2.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
C++ Review (3) Structs, Classes, Data Abstraction.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Dynamic memory allocation and Pointers Lecture 4.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Programming
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Session 1 - Introduction
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Dynamic Memory Allocation Reference Variables
CS150 Introduction to Computer Science 1
Chapter 15 - C++ As A "Better C"
Arrays Arrays A few types Structures of related data items
C++ Programming Basics
Presentation transcript:

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

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

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()

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

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

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

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.

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

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

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.

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

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

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