C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.
-
Upload
sharyl-armstrong -
Category
Documents
-
view
212 -
download
0
Transcript of C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.
![Page 1: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/1.jpg)
C++ ObjectsStructs and Classes
Presented by: Brian Lojeck4/25/2011
ET286, Prof. Hill
![Page 2: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/2.jpg)
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?
![Page 3: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/3.jpg)
The old way?
double Wave1Amp, Wave2Amp, Wave3Amp…double Wave1Period, Wave2Period, Wave3…double Wave1Phase, Wave2Phase, Wave3…double Wave1DC, Wave2DC, Wave3DC…
![Page 4: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/4.jpg)
Slightly Better…
double Amplitudes[3];double Periods[3];double PhaseOffsets[3];double DCOffsets[3];
![Page 5: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/5.jpg)
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
![Page 6: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/6.jpg)
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”
![Page 7: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/7.jpg)
To Declare A Struct
struct Sin_Wave {
int SampleCount;double Amplitude;double Period;double Phase;
};
![Page 8: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/8.jpg)
To Instantiate A Struct
struct Sin_Wave {int SampleCount; double Amplitude;double Period; double Phase;
};
int main() {int Counter1, Counter2;Sin_Wave SampledSinWave;Sin_Wave OutputSinWave[10];
}
![Page 9: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/9.jpg)
To Access A Struct’s Member Data
int main() {Sin_Wave SampledSinWave;Sin_Wave OutputSinWave[10];
SampledSinWave.Amplitude=4;cout << OutputSinWave[2].Period;
}
![Page 10: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/10.jpg)
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
![Page 11: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/11.jpg)
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
![Page 12: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/12.jpg)
To Program A Classclass CRectangle { private:
int Height, Width; //has data members just like a struct public:
void set_values (int,int);int area () {return (Height*Width);}
};
void CRectangle::set_values (int a, int b) { Height = a; Width = b; }
int main() {…
![Page 13: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/13.jpg)
To Instantiate A Classclass CRectangle { private:
int Height, Width; //has data members just like a struct public:
void set_values (int,int);int area () {return (Height*Width);}
};
int main() {CRectangle BigRectangle, SmallRectangle;CRectangle LottaRectangles[100];
}
![Page 14: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/14.jpg)
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• int getHeight()• char getGrade()
– Functions like this are REQUIRED if the data member is private
– These functions MUST be public to be used
![Page 15: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/15.jpg)
To Access A Class’s Dataclass CRectangle { private: int Height, Width; public: void set_values (int,int); int get_Height () {return (Height);} };
int main() {CRectangle BigRectangle, SmallRectangle;CRectangle LottaRectangles[100];
BigRectangle.Height=100; // FAILSBigRectangle.set_Height(100); //WORKScout << BigRectangle.Height; //FAILScout << BigRectangle.get_Height(); //WORKS
}
![Page 16: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/16.jpg)
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
![Page 17: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/17.jpg)
Coding Example
class Date {private:
int Month, Day, Year;public:
void setMonth(int);void setDay(int);void setYear(int);int getMonth();int getDay();int getYear();
};
![Page 18: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/18.jpg)
Coding Example
int Date::getMonth(){return(Month);
}
int Date::getYear(){return(Year);
}
int Date::getDay(){return(Day);
}
![Page 19: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/19.jpg)
Coding Example
void Date::setMonth(int m){if (m<=12 && m>=1) {
Month=m;}else {
Month=0;}return;
}
![Page 20: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/20.jpg)
Coding Example
void Date::setDay(int d){if (d<=31 && d>=1) {
Day=d;}else {
Day=0;}return;
}
![Page 21: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/21.jpg)
Coding Example
void Date::setYear(int y){
if (y<100) y+=2000;
if (y<10000) Year=y;else Year=0;
return;}
![Page 22: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/22.jpg)
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;
}
![Page 23: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/23.jpg)
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
![Page 24: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/24.jpg)
A Constructor
• Is called once, and only once, when you instantiate the class:– MyStudentClass BrianL; // It just got called
• Writing your own constructor lets you assign default values or enter your own values– MyStudentClass BrianL; //Legal constructor– MyStudentClass BrianL(“Brian”,”F”); //also legal
![Page 25: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/25.jpg)
Coding A Constructor
class MyClass{int i;
public:MyClass(); //default constructorMyClass(int); // another constructorset_i(int); // a set function
};
![Page 26: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/26.jpg)
Coding A Constructor
MyClass::MyClass(){set_i(0);}
MyClass::MyClass(int newAye){set_i(newAye);}
MyClass::set_i(int newAye){i=newAye;}
![Page 27: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/27.jpg)
Using The Constructor
int main(){MyClass Counter; //legal, Counter.i==0MyClass Class2(30); //Class2.i==30Counter.set_i(31); //set functions work tooreturn 0;
}
![Page 28: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/28.jpg)
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.
![Page 29: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/29.jpg)
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
![Page 30: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/30.jpg)
Create A Header File
![Page 31: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/31.jpg)
Create A Header File
![Page 32: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/32.jpg)
Create A Header File
![Page 33: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/33.jpg)
Header File Contents
![Page 34: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/34.jpg)
Create The Definition .cpp File
![Page 35: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/35.jpg)
Create The MyClass.cpp File
![Page 36: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/36.jpg)
MyClass.cpp File Contents
![Page 37: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/37.jpg)
MyClass.cpp File Contents
![Page 38: C++ Objects Structs and Classes Presented by: Brian Lojeck 4/25/2011 ET286, Prof. Hill.](https://reader034.fdocuments.in/reader034/viewer/2022051820/56649da75503460f94a93587/html5/thumbnails/38.jpg)
Main *.cpp File Contents