1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Inheritance and Polymorphism Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Introduction to C++ Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Functions.
Chapter 1.2 Introduction to C++ Programming
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Chapter 3 - Functions Outline 3.1 Introduction
FUNCTIONS IN C++.
Dr. Shady Yehia Elmashad
Functions Najah Alsubaie Kingdom of Saudi Arabia
Data Types – Structures
Dr. Shady Yehia Elmashad
Data Types – Structures
Inheritance and Polymorphism
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers Call-by-Reference CSCI 230
CS150 Introduction to Computer Science 1
Variables have attributes
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 15 - C++ As A "Better C"
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University

2 Contents n 1. Introduction n 2. C++ Single-Line Comments n 3. C++ Stream Input/Output n 4. Declarations in C++ n 5. Creating New Data Types in C++ n 6. Reference Parameters n 7. Const Qualifier n 8. Default Arguments n 9. Function Overloading

3 1. Introduction n C++ improves on many of C’s features. n C++ provides object-oriented programming (OOP). n C++ is a superset to C. n No ANSI standard exists yet (in 1994).

4 2. C++ Single-Line Comments n In C, /* This is a single-line comment. */ n In C++, // This is a single-line comment.

5 3. C++ Stream Input/Output n In C, printf(“Enter new tag: “); scanf(“%d”, &tag); printf(“The new tag is: %d\n”, tag); n In C++, cout > tag; cout << “The new tag is : “ << tag << ‘\n’;

6 3.1 An Example // Simple stream input/output #include main() { cout << "Enter your age: "; int myAge; cin >> myAge; cout << "Enter your friend's age: "; int friendsAge; cin >> friendsAge;

7 if (myAge > friendsAge) cout << "You are older.\n"; else if (myAge < friendsAge) cout << "You are younger.\n"; else cout << "You and your friend are the same age.\n"; return 0; }

8 4. Declarations in C++ In C++, declarations can be placed anywhere (except in the condition of a while, do/while, for or if structure.) n An example cout << “Enter two integers: “; int x, y; cin >> x >> y; cout << “The sum of “ << x << “ and “ << y << “ is “ << x + y << ‘\n’;

9 n Another example for (int i = 0; i <= 5; i++) cout << i << ‘\n’;

10 5. Creating New Data Types in C++ struct Name { char first[10]; char last[10]; }; n In C, struct Name stdname; n In C++, Name stdname; The same is true for enum s and union s

11 6. Reference Parameters n In C, all function calls are call by value. –Call be reference is simulated using pointers n Reference parameters allows function arguments to be changed without using return or pointers.

Comparing Call by Value, Call by Reference with Pointers and Call by Reference with References #include int sqrByValue(int); void sqrByPointer(int *); void sqrByRef(int &); main() { int x = 2, y = 3, z = 4; cout << "x = " << x << " before sqrByVal\n" << "Value returned by sqrByVal: " << sqrByVal(x) << "\nx = " << x << " after sqrByVal\n\n";

13 cout << "y = " << y << " before sqrByPointer\n"; sqrByPointer(&y); cout << "y = " << y << " after sqrByPointer\n\n"; cout << "z = " << z << " before sqrByRef\n"; sqrByRef(z); cout << "z = " << z << " after sqrByRef\n"; return 0; }

14 int sqrByValue(int a) { return a *= a; // caller's argument not modified } void sqrByPointer(int *bPtr) { *bPtr *= *bPtr; // caller's argument modified } void sqrByRef(int &cRef) { cRef *= cRef; // caller's argument modified }

15 Output $ g++ -Wall -o square square.cc $ square x = 2 before sqrByValue Value returned by sqrByValue: 4 x = 2 after sqrByValue y = 3 before sqrByPointer y = 9 after sqrByPointer z = 4 before sqrByRef z = 16 after sqrByRef

16 7. The Const Qualifier n Used to declare “constant variables” (instead of #define) const float PI = ; n The const variables must be initialized when declared.

17 8. Default Arguments n When a default argument is omitted in a function call, the default value of that argument is automatically passed in the call. n Default arguments must be the rightmost (trailing) arguments.

An Example // Using default arguments #include // Calculate the volume of a box int boxVolume(int length = 1, int width = 1, int height = 1) { return length * width * height; }

19 main() { cout << "The default box volume is: " << boxVolume() << "\n\nThe volume of a box with length 10,\n" << "width 1 and height 1 is: " << boxVolume(10) << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 1 is: " << boxVolume(10, 5) << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 2 is: " << boxVolume(10, 5, 2) << '\n'; return 0; }

20 Output $ g++ -Wall -o volume volume.cc $ volume The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100

21 9. Function Overloading n In C++, several functions of the same name can be defined as long as these function name different sets of parameters (different types or different number of parameters).

An Example // Using overloaded functions #include int square(int x) { return x * x; } double square(double y) { return y * y; } main() { cout << "The square of integer 7 is " << square(7) << "\nThe square of double 7.5 is " << square(7.5) << '\n'; return 0; }

23 Output $ g++ -Wall -o overload overload.cc $ overload The square of integer 7 is 49 The square of double 7.5 is 56.25