Oop1

650 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
650
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Oop1

  1. 1. Introduction to C++Noppadon KamolvilassatianDepartment of Computer EngineeringPrince of Songkla University 1
  2. 2. Contentss 1. Introductions 2. C++ Single-Line Commentss 3. C++ Stream Input/Outputs 4. Declarations in C++s 5. Creating New Data Types in C++s 6. Reference Parameterss 7. Const Qualifiers 8. Default Argumentss 9. Function Overloading 2
  3. 3. 1. Introductions C++ improves on many of C’s features.s C++ provides object-oriented programming (OOP).s C++ is a superset to C.s No ANSI standard exists yet (in 1994). 3
  4. 4. 2. C++ Single-Line Commentss In C,/* This is a single-line comment. */s In C++,// This is a single-line comment. 4
  5. 5. 3. C++ Stream Input/Outputs In C, printf(“Enter new tag: “); scanf(“%d”, &tag); printf(“The new tag is: %dn”, tag);s In C++, cout << “Enter new tag: “; cin >> tag; cout << “The new tag is : “ << tag << ‘n’; 5
  6. 6. 3.1 An Example// Simple stream input/output#include <iostream.h>main(){ cout << "Enter your age: "; int myAge; cin >> myAge; cout << "Enter your friends age: "; int friendsAge; cin >> friendsAge; 6
  7. 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;} 7
  8. 8. 4. Declarations in C++s In C++, declarations can be placed anywhere (except in the condition of a while, do/while, f or or if structure.)s An examplecout << “Enter two integers: “;int x, y;cin >> x >> y;cout << “The sum of “ << x << “ and “ << y << “ is “ << x + y << ‘n’; 8
  9. 9. s Another examplefor (int i = 0; i <= 5; i++) cout << i << ‘n’; 9
  10. 10. 5. Creating New Data Types in C++struct Name { char first[10]; char last[10];};s In C,struct Name stdname;s In C++,Name stdname;s The same is true for enums and unions 10
  11. 11. 6. Reference Parameterss In C, all function calls are call by value. – Call be reference is simulated using pointerss Reference parameters allows function arguments to be changed without using return or pointers. 11
  12. 12. 6.1 Comparing Call by Value, Call by Referencewith Pointers and Call by Reference with References#include <iostream.h>int sqrByValue(int);void sqrByPointer(int *);void sqrByRef(int &);main(){ int x = 2, y = 3, z = 4; cout << "x = " << x << " before sqrByValn" << "Value returned by sqrByVal: " << sqrByVal(x) << "nx = " << x << " after sqrByValnn"; 12
  13. 13. cout << "y = " << y << " before sqrByPointern"; sqrByPointer(&y); cout << "y = " << y << " after sqrByPointernn"; cout << "z = " << z << " before sqrByRefn"; sqrByRef(z); cout << "z = " << z << " after sqrByRefn"; return 0;} 13
  14. 14. int sqrByValue(int a){ return a *= a; // callers argument not modified}void sqrByPointer(int *bPtr){ *bPtr *= *bPtr; // callers argument modified}void sqrByRef(int &cRef){ cRef *= cRef; // callers argument modified} 14
  15. 15. Output$ g++ -Wall -o square square.cc$ squarex = 2 before sqrByValueValue returned by sqrByValue: 4x = 2 after sqrByValuey = 3 before sqrByPointery = 9 after sqrByPointerz = 4 before sqrByRefz = 16 after sqrByRef 15
  16. 16. 7. The Const Qualifiers Used to declare “constant variables” (instead of #define) const float PI = 3.14156;s The const variables must be initialized when declared. 16
  17. 17. 8. Default Argumentss When a default argument is omitted in a function call, the default value of that argument is automati cally passed in the call.s Default arguments must be the rightmost (trailing) arguments. 17
  18. 18. 8.1 An Example// Using default arguments#include <iostream.h>// Calculate the volume of a boxint boxVolume(int length = 1, int width = 1, int height = 1) { return length * width * height; } 18
  19. 19. main(){ cout << "The default box volume is: " << boxVolume() << "nnThe volume of a box with length 10,n" << "width 1 and height 1 is: " << boxVolume(10) << "nnThe volume of a box with length 10,n" << "width 5 and height 1 is: " << boxVolume(10, 5) << "nnThe volume of a box with length 10,n" << "width 5 and height 2 is: " << boxVolume(10, 5, 2) << n; return 0;} 19
  20. 20. Output$ g++ -Wall -o volume volume.cc$ volumeThe default box volume is: 1The volume of a box with length 10,width 1 and height 1 is: 10The volume of a box with length 10,width 5 and height 1 is: 50The volume of a box with length 10,width 5 and height 2 is: 100 20
  21. 21. 9. Function Overloadings 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 nu mber of parameters). 21
  22. 22. 9.1 An Example // Using overloaded functions #include <iostream.h> 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; } 22
  23. 23. Output$ g++ -Wall -o overload overload.cc$ overloadThe square of integer 7 is 49The square of double 7.5 is 56.25 23

×