1 / 38

C++ Objects S tructs and Classes

C++ Objects S tructs and Classes. Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill. The Sin Wave. To properly describe a sin wave: Amplitude Period Phase offset DC offset Suppose we wanted to store data for many sin waves at once?. The old way?.

zelia
Download Presentation

C++ Objects S tructs and Classes

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. C++ ObjectsStructs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill

  2. The Sin Wave • To properly describe a sin wave: • Amplitude • Period • Phase offset • DC offset • Suppose we wanted to store data for many sin waves at once?

  3. The old way? double Wave1Amp, Wave2Amp, Wave3Amp… double Wave1Period, Wave2Period, Wave3… double Wave1Phase, Wave2Phase, Wave3… double Wave1DC, Wave2DC, Wave3DC…

  4. Slightly Better… double Amplitudes[3]; double Periods[3]; double PhaseOffsets[3]; double DCOffsets[3];

  5. Problems • Related data is not kept together • Amp[0] goes with Period[0] with DC[0] • Multiple arrays to pass back and forth in functions • Leads to messy code that can be complex to read and problem-solve

  6. A Struct is One Solution • Is a data type created by the programmer • Contains any number of different data elements, of any type desired • “Member Data” or “Data Members”

  7. To Declare A Struct structSin_Wave { intSampleCount; double Amplitude; double Period; double Phase; };

  8. To Instantiate A Struct structSin_Wave { intSampleCount; double Amplitude; double Period; double Phase; }; int main() { int Counter1, Counter2; Sin_WaveSampledSinWave; Sin_WaveOutputSinWave[10]; }

  9. To Access A Struct’s Member Data int main() { Sin_WaveSampledSinWave; Sin_WaveOutputSinWave[10]; SampledSinWave.Amplitude=4; cout << OutputSinWave[2].Period; }

  10. Beyond Structs • Structs allow for useful data types to be created as needed • Data types are often less important then the functions that operate on the data • If the functions are properly written, the data itself does not ever need to be directly accessed by the programmer

  11. Classes • Classes • Contain any desired data types, like a struct • Also contain the functions that operate on the data • Called “Member Functions” or “Methods” • The two terms are interchangeable • The data is typically “private”, and not accessible to the programmer directly • The Methods are typically the only public (accessible to the programmer) interface

  12. To Program A Class classCRectangle { private: int Height, Width; //has data members just like a struct public: voidset_values (int,int); int area () {return (Height*Width);} }; voidCRectangle::set_values (int a, int b) { Height = a; Width = b; } int main() {…

  13. To Instantiate A Class classCRectangle { private: int Height, Width; //has data members just like a struct public: voidset_values (int,int); int area () {return (Height*Width);} }; int main() { CRectangleBigRectangle, SmallRectangle; CRectangleLottaRectangles[100]; }

  14. get() and set() • The most common functions in a c++ class are get() and set() • set() lets the programmer ENTER data into a class • void setHeight(int) • void setGrade(char) • get() lets the programmer EXTRACT data • intgetHeight() • char getGrade() • Functions like this are REQUIRED if the data member is private • These functions MUST be public to be used

  15. To Access A Class’s Data classCRectangle { private: int Height, Width; public: voidset_values (int,int); intget_Height () {return (Height);} }; int main() { CRectangleBigRectangle, SmallRectangle; CRectangleLottaRectangles[100]; BigRectangle.Height=100; // FAILS BigRectangle.set_Height(100); //WORKS cout << BigRectangle.Height; //FAILS cout << BigRectangle.get_Height(); //WORKS }

  16. WHY? • Allowing a programmer to access data directly means: • Student.Grade=‘Q’; • Requiring a set function lets you check for errors (or malicious hacking attempts) and prevent them from ruining the system

  17. Coding Example class Date { private: int Month, Day, Year; public: void setMonth(int); void setDay(int); void setYear(int); intgetMonth(); intgetDay(); intgetYear(); };

  18. Coding Example int Date::getMonth(){ return(Month); } int Date::getYear(){ return(Year); } int Date::getDay(){ return(Day); }

  19. Coding Example void Date::setMonth(int m){ if (m<=12 && m>=1) { Month=m; } else { Month=0; } return; }

  20. Coding Example void Date::setDay(int d){ if (d<=31 && d>=1) { Day=d; } else { Day=0; } return; }

  21. Coding Example void Date::setYear(int y){ if (y<100) y+=2000; if (y<10000) Year=y; else Year=0; return; }

  22. Coding Example int main(){ Date Tdy; Tdy.setDay(13);Tdy.setYear(2011); Tdy.setMonth(4); cout << “Today is: “ << Tdy.getMonth() << “/” << Tdy.getDay() << “/” << Tdy.getYear() << endl; return 0; }

  23. Constructor • When you instantiate a class, the data contained inside is un-initialized • Similar to when you instantiate native data types: • int j; // j can contain any random value • int j=0; //j is initialized cleanly to 0 • A Constructor lets you provide initialization values to the class • A Constructor is… A TYPE OF SET() FUNCTION

  24. A Constructor • Is called once, and only once, when you instantiate the class: • MyStudentClassBrianL; // It just got called • Writing your own constructor lets you assign default values or enter your own values • MyStudentClassBrianL; //Legal constructor • MyStudentClassBrianL(“Brian”,”F”); //also legal

  25. Coding A Constructor class MyClass{ int i; public: MyClass(); //default constructor MyClass(int); // another constructor set_i(int); // a set function };

  26. Coding A Constructor MyClass::MyClass(){ set_i(0); } MyClass::MyClass(intnewAye){ set_i(newAye); } MyClass::set_i(intnewAye){ i=newAye; }

  27. Using The Constructor int main(){ MyClass Counter; //legal, Counter.i==0 MyClass Class2(30); //Class2.i==30 Counter.set_i(31); //set functions work too return 0; }

  28. Organization Of Objects In Files • There is no physical reason you cannot describe a class, define the class, and use it in a program’s main() function all in one file. • This is a bad habit to get into, it will make larger programs appear disorganized and hard to edit/correct/change.

  29. Organization Of Objects In Files • Class definitions are typically organized: • Class description in a header file (ClassName.h) • Class definition in a c++ code file (ClassName.cpp) • The .h file is included in the main program’s file using the include directive • #include “ClassName.h” • All files related to a class should be named after the class for organization purposes

  30. Create A Header File

  31. Create A Header File

  32. Create A Header File

  33. Header File Contents

  34. Create The Definition .cpp File

  35. Create The MyClass.cpp File

  36. MyClass.cpp File Contents

  37. MyClass.cpp File Contents

  38. Main *.cpp File Contents

More Related