DOC PREVIEW
CSUN COMP 106 - Introduction to Structures and 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:

Introduction to Classes May 11, 20061Introduction to Structures and Introduction to Structures and Classes in C++Classes in C++Larry CarettoComputer Science 106Computing in Engineering and ScienceMay 11, 2006Schedule• Today and Tuesday: Lecture on classes• Thursday (May 18)– 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• Introduction to C/C++ structured variables (structs)• Expansion of structs to classes• Classes versus objects of a class• Example of a class to be used for calculations with complex numbers– Definition of class– Use of class– Details of class functions3Goals for This Topic• Provide introduction to structs and classes used in object-oriented programming• Show how functions similar to those we have used for input/output (e.g., .good(), .open(), etc.) are written• Give background appropriate for– students interested in further study of C++– Individuals working on programs that use structs and classes• Topics will not be covered on final4What is a Structure?• Previously, we have used simple variables and arrays• Arrays as example of a data structure– x is array x[i] is array element– all elements must have the same type• What if we want a structure to represent data for each student at CSUN• We would want several pieces of information, name, ID, etc. with different data types5Another Example of a Structure• In exercise eight we had data on x and y for which we wanted to compute the count, mean, standard deviation, max and min• We could declare a structure to hold the data and the values to be computed.• We would then declare different variables as having the type named by the struct• Each struct variable has each component defined in the structure6Introduction to Classes May 11, 20062Definition of a Structurestruct dataSet{double data[MAX_DATA]; // all data// valuesint count; // number of valuesdouble mean; // mean valuedouble max; // maximum valuedouble min; // minimum valuedouble stdDev;// standard deviation}; // note ; at end• dataSet is name of struct (like a type)7Use of a Structure• When we define a structure like dataSet, we have a user defined type (UDT)• We can declare variables of type dataSet, and each variable will have all the fields listed in the structure • To refer to a field we use the name of the variable, followed by a period, followed by the component name• See examples on the next chart8Use of a Structure IIstruct dataSet {double data[MAX_DATA]; int count;double mean; double max;double min; double stdDev;};dataSet x, y, xlow, ylow, xhigh,yhigh; // declare variablesgetInput ( x.data, x.count )getInput ( y.data, y.count )x.mean = getAverage( x.data, x.count )• Each variable has all the fields defined in the structure used as <variable>.<field>9Use of a Structure IIIdataSet x, y, xlow, ylow, xhigh,yhigh; // declare variablesgetInput ( x.data, x.count )getInput ( y.data, y.count )x.mean = getAverage( x.data, x.count )• Can pass entire structures or individual fields to functions• Note that struct dataSet is abstract concept until variables with that struct are created• Look at simple example of a struct with count = 3 and data[] = { -1, 0, 1}10Struct Example111x.stdDev-1x.min1x.max0x.mean3x.countundefinedx.data[3] to x.data[MAX_DATA-1]1x.data[2]0x.data[1]-1x.data[0]From structs to classes• Classes are similar to structs– Abstract data type that defines a structure– Variables are declared that belong to a class• In addition to having data values, classes can have functions that operate on data• Can “hide” data from user in classes– User cannot change data by accident– User can only access data through functions– Use can change data in ways allowed by functions for the class12Introduction to Classes May 11, 20063Class 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 file13File 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 definitions14Complex 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/output15Complex Number, z• Two basic representations– Rectangular: real (x) and imaginary (y) parts– Polar: magnitude (r) and angle (θ)1−==+= jrejyxzjθ⎟⎠⎞⎜⎝⎛=+===−xyyxrryrx122tan)sin()cos(θθθ16Complex Number Operations121212cyycxxczz ==⇒=213213213yyyxxxzzz±=±=⇒±=⎩⎨⎧+=−=⇒=1221321213213xyxyyyyxxxzzz()()⎩⎨⎧−=++=+⇒=122132222212132222213xyxyyyxyyxxxyxzzz17Complex 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) file18Introduction to Classes May 11, 20064Code 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”19class 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 =


View Full Document

CSUN COMP 106 - Introduction to Structures and Classes in C++

Download Introduction to Structures and 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 Introduction to Structures and 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 Introduction to Structures and 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?