1 / 12

PHYS707: Special Topics

PHYS707: Special Topics. C++ Lectures. Lecture 2. Summary of Today’s lecture:. More data types Flow control (if-else, while, do-while, for ) B rief introduction to classes: The complex class Functions Built-in Do it yourself. More Data Types .

maia
Télécharger la présentation

PHYS707: Special Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHYS707: Special Topics C++ Lectures Lecture 2

  2. Summary of Today’s lecture: • More data types • Flow control (if-else, while, do-while, for) • Brief introduction to classes: The complex class • Functions • Built-in • Do it yourself

  3. More Data Types Boolean (bool): Boolean logic; can have one of 2 values, true or false. Example: bool b1, b2; b1= true; b2=false; cout << “b1 is “ << b1 << “\nb2 is “ << b2 << endl; Output: b1 is 1 b2 is 0 Character (char): a single symbol Example: char c1, c2; c1= ‘C’; c2=“+”; cout << c1 << c2 << c2 << endl; Output: C++

  4. Control the Flow! You've already used some flow control. Here are the most important ones: // The “for loop”: for (i = imin; i <= imax; i++) { [stuff goes here] } // this ends the “loop” // the case construct (great for menus) switch(case_number) { case 0: [stuff goes here] break; case 1: [stuff goes here] break; case 3: [stuff goes here] break; default: [stuff goes here] } // the “while loop”: imax = 6; i = 0; while (i < imax) { [stuff goes here] i++; }

  5. If…else if construct if(a > b) { [stuff goes here] } else if((c==d)|| (i == j)) { [other stuff goes here] } Some logic operators: > greater than < less than == is equal to ! NOT, eg: != NOT equal to !> NOT greater than, etc >= greater than or equal to <= less than or equal to || logical OR && logical AND

  6. Complex Class A “class” is an abstract data type that includes members. Members can include variables (data members) and functions (member functions). You can easily create your own classes, or use classes that others have already created: Example, complex: cout<< "a= " << a << endl; cout << "b= " << b << endl; cout << "a + b = " << a+b << endl; cout << "a * b = " << a*b << endl; cout << "a/b = " << a/b << endl; cout << "|a| = " << abs(a) << endl; cout << "cmplxconjof a = " << conj(a) << endl; cout << "norm of a = " << norm(a) << endl; cout << "exp(a) = " << exp(a) << endl; cout << "Re(a) = " << a.real() << endl; cout << "Im(a) = " << a.imag() << endl; cout << "a^3 = " << pow(a,3) << endl; cout << "c= " << c << endl; c=polar(3.0,PI/4.0); cout << "3*e^(PI/4) = " << c << endl; cout << "|c| = " << abs(c) << endl; cout << “phase of c = “ << arg(c) << endl; return 0; } // program to demonstrate use of classes // use the complex class!! // written 07/23/2012 by B. DePaola #include<complex> #include<iostream> using namespace std; typedef complex<double> dcmplx; int main() { dcmplx a(5.0,6.0),b,c; const double PI=3.14159; // input value from keyboard: (x,y) cout << "enter b: "; cin >> b; cout << endl;

  7. Output: a= (5,6) b= (1,2) a + b = (6,8) a * b = (-7,16) a/b = (3.4,-0.8) |a| = 7.81025 cmplxconj of a = (5,-6) norm of a = 61 exp(a) = (142.502,-41.4689) Re(a) = 5 Im(a) = 6 a^3 = (-415,234) c= (0,0) 3*e^(PI/4) = (2.12132,2.12132) |c| = 3 phase of c = 0.785397 Homework: Revisit the quadratic equation problem, BUTnow allow for complex roots!

  8. Functions Built-in: May have to #include a library (like <cmath> etc). Otherwise completely straightforward. Examples: sin(x), fabs(x), sqrt(x), etc.

  9. User-Defined Functions Example – main program: #include <iostream> using namespace std; double quadeq(double a, double b, double c); // function declaration // computes degenerate soln of quadratic equation, given the coefficients int main() { double aa, bb, cc; [input coeff part…] cout << quadeq(aa,bb,cc); return 0; }

  10. double quadeq(double a, double b, double c) // note: no semicolon!! { double arg_of_sqrt, x; const double eps = 1.0e-6; arg_of_sqrt = b*b – 4.0*a*c; if (fabs(arg_of_sqrt) > eps) { cout << "Equation has > 1 solution\n"; return 0; } else { return –b/(2.0*a); } }

  11. Key Points • Arguments passed by position (mess up the order => mess up the calculation!) • Can also pass by reference (next lecture!) • Functions can return: • int • floating point • string or char • boolean • nothing at all! (Huh???) • Use lots of functions! "Block Structure" • Scope & local variables • Debug functions separately

  12. MORE Homework! Write and implement a function that returns the product of three floating point numbers Write and implement a function that returns the mean of two floating point numbers Write and implement a function that passes three boolean arguments, and returns the logical AND of the third argument with the logical OR of the first two arguments: x(a,b,c) = (a OR b) and c

More Related