DOC PREVIEW
CSUN COMP 106 - Implementation of Classes in C++

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Implementation of classes May 16, 20061Implementation of Classes in Implementation of Classes in C++C++Larry CarettoComputer Science 106Computing in Engineering and ScienceMay 16, 2006Schedule• Today: Second lecture on classes• Thursday– Project 3 deadline– Review for final• Tuesday, May 23: Final exam 12:45 to 2:45 pm in this room• Friday, May 26: Last day for late assignments2Outline• Quiz results and comments• Review idea of classes • Review example of a class to be used for calculations with complex numbers– Definition of class– Use of class• Details of class functions3Quiz Results• Number of students: 8• Maximum possible score: 25• Mean score: 19.1• Median score: 19.5• Standard deviation of scores: 4.82• Grade distribution:12 15 15 19 20 22 25 254Basic Code for Solutiondouble rmsV = 0;double rmsI = 0;double power = 0;for ( int k = 0; k < n; k++ ) {rmsV += v[k] * v[k];rmsI += i[k] * i[k];power += v[k] * i[k];}rmsV = sqrt( rmsV / n );rmsI = sqrt( rmsI / n );power /= n;5Quiz Comments• Remember to use use pass-by-reference to return more than one function calculation to the calling function• In loops, do only the sums– Leave final divisions, subtractions, square roots, etc. to be done after loop ends• Note difference between array size and number of elements actually defined• Use meaningful variable names6Implementation of classes May 16, 20062Review Class Definition and Use• Class declaration– Specify data and functions as public or private– Declares data items belonging to class– Provides prototypes of class member functions and friend functions• May have function body declared as inline functions• Only member functions can access private member• Definition of class member functions– Separate from class declaration– Usually done in separate file7Review File Structure• Header file– Contains class declaration (with member function prototypes)– Used in files that define member functions and files that use member functions• Definition of class member functions in separate file• Other files that use classes include header file with class definitions8Review Complex Number Class• Shows example of class that allows operations on complex numbers• Usually seen in electrical circuits, aerodynamics, and electromagnetics• Complex numbers have a real and an imaginary part• We want to be able to perform mathemat-ical operations with complex numbers• Also need input/output9Review Complex Number, z• Two basic representations– Rectangular: real (x) and imaginary (y) parts– Polar: magnitude (r) and angle (θ)1−==+= jrejyxzjθ⎟⎠⎞⎜⎝⎛=+===−xyyxrryrx122tan)sin()cos(θθθ10Review Complex Number Operations121212cyycxxczz ==⇒=213213213yyyxxxzzz±=±=⇒±=⎩⎨⎧+=−=⇒=1221321213213xyxyyyyxxxzzz()()⎩⎨⎧−=++=+⇒=122132222212132222213xyxyyyxyyxxxyxzzz11Review Complex class• Class declaration– Two member data components: real and imaginary parts of a complex number– Various member functions to get data about the complex number, provide input and output and define operators for complex numbers• Complex class objects are complex numbers• Functions specified in class declaration implemented in separate (header) file12Implementation of classes May 16, 20063Review Code for Complex Class• Start with class declaration that will show prototypes of available functions/operators • In file complex.h used by other functions• Show main function that uses the complex class – show commands and output from the commands• Show definition of member functions after showing prototypes and result of use• All files have header, “complex.h”13class complex{private:double Re; // Real part of complex numberdouble Im; // Imaginary part of complex number// Member functions (only prototypes required in class // definition. These are public so that they can be used// by remainder of code. { Some are inline. }public:complex() { Re = 0; Im = 0; } // constructorcomplex( double inRe, double inIm ) // second constuctor{ Re = inRe; Im = inIm; }double getRe() { return Re; } // returns valuedouble getIm() { return Im; } // returns valuedouble getMagnitude() { returnsqrt( Re * Re + Im * Im ); }// magnitude of numberdouble getPhase() // phase angle{ return atan2( Im, Re ); }14// Various function operators. Note overloading and “friends.”friend ostream& operator<< ( ostream& os, const complex& c ) { os << '(' << c.Re << ", " << c.Im << ')'; return os; friend istream& operator>> ( istream& is, complex& c ) { is >> c.Re >> c.Im; return is; }complex plus( complex c );friend complex add( complex c1, complex c2 );complex operator+( complex c );complex operator*( complex c );friend complex operator*( complex c, double d );friend complex operator*( double d, complex c );}; // End of class definition uses a } plus a semicolon!15Review Code for Complex Class• Members and prototypes provided in class declaration tell us what is available• If we understand what the functions are supposed to do, we can use them without knowing their details• The following charts show a main function that declares and uses complex objects• Output shown as comments in the code to see results of class functions• Note: 32+ 42= 52and tan(4/5) = 53.1301o16// main shows use of the complex class#include “complex.h”DEGREES_PER_RADIAN = 45 / atan(1); // global constantint main(){ // Declare and output complex data types.complex a; // same as complex a( 0, 0 )complex b( 3, 4 ); // Re = 3 and Im = 4cout << "a = " << a << " and b = " << b << endl;cout << "The magnitude of b is: "<< b.getMagnitude() << endl;cout << "The phase angle of b is: " << ( b.getPhase() * DEGREES_PER_RADIAN ) << " degrees.\n";/* Program Outputa = (0, 0) and b = (3, 4)The magnitude of b is: 5The phase angle of b is: 53.1301 degrees. */17// Show output operator and addition operationscomplex c( 7, 10 );cout << "\nc = " << c << endl;cout << "Use of add function, add( b, c ) = " << add( b, c );cout << "Use of + operator, b + c = " << ( b + c ) << endl;cout << "Before b.plus( c ), b = " << b;cout << " and b.plus( c ) = " << b.plus( c ) << endl;cout << "After b.plus( c ), b = " << b << endl;// Show multiplication operations/* Program Outputc = (7, 10)Use of add function, add( b, c ) = (10, 14)Use of + operator, b + c = (10, 14)Before b.plus( c ), b = (3, 4) and b.plus( c ) = (10,


View Full Document

CSUN COMP 106 - Implementation of Classes in C++

Download Implementation of Classes in C++
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Implementation of Classes in C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Implementation of Classes in C++ 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?