This line creates a new student variable called t

Info icon This preview shows pages 42–50. Sign up to view the full content.

View Full Document Right Arrow Icon
. This line creates a new  STUDENT  variable called  t .
Image of page 42

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Structures #include <iostream> struct STUDENT { string name; int ID; float GPA; }; main() { string s ; STUDENT t ; t . name = “Carey”; t.ID = 113353948; t . GPA = 3.99; . The variable  t  has 3 sub-variables  in it:  name ID_number  and  GPA . We can set  t ’s fields using this  syntax:     t  .   fieldname  =  value ;       s        “”       t          name       “” ID        -721 GPA        7.11 “carey” 113353948 3.99
Image of page 43
Structures Once we define our new  structure type  like  STUDENT , we can use it like  any other traditional data type. struct STUDENT { string name; int ID; float GPA; }; void blarf( STUDENT &x) { x.GPA++; } main() { STUDENT UCLA[ 50000 ]; STUDENT a,b,*ptr; blarf(a); ... Typically, you define new structure types  outside of all functions.    After you define a new structure type, you  can create variables with it throughout your  program.
Image of page 44

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to Classes Consider the following structure and associated function(s). struct Rect {    int width;    int height; }; int GetArea(Rect &a) {   int area = a.width *         a.height;   return(area); } int main(void) {    Rect r;    r.width = 10;    r.height = 20;    cout <<  GetArea( r ); r width height 10 20 a area              10 * 20 200 200
Image of page 45
Classes class  is basically a  struct  with  built-in functions  in addition to  field variables. struct Rect {    int width;    int height; }; int GetArea(Rect &a) {   int area = a.width *         a.height;   return(area); } class    int width;    int height; };   return(area); )
Image of page 46

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using a Class class  Rect {     int GetArea()     {        int area = width * height;       return(area);     }     int width;     int height; }; int main(void) {    Rect  r;    r.width = 10;    r.height = 20;    cout <<  r. GetArea();   r       width     height 10 20 area 10 * 200 200 Output: 200
Image of page 47
Using a Class class  Rect {     int GetArea()     {        int area = width * height;       return(area);     }     int width;     int height; }; int main(void) {    Rect   r s ;    r.width = r.height = 5;     s.width = s.height = 7;     cout << r.GetArea();    cout << s.GetArea();   r int GetArea()    int area = width * height;    return(area); } width height   s int GetArea()    int area = width * height;    return(area); } width height 5 5 7 7 5 * 5 area 25 25 25 Output: 25 7 * 7 area 49 49 49 49
Image of page 48

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using a Class class  Rect {     int GetArea()     {        int area = width * height;       return(area);     }     int width;     int height; }; int main(void) {    Rect   r s ;    r.width = r.height = 5;     s.width = s.height = 7;     cout << r.GetArea();    cout << s.GetArea();   r int GetArea()    int area = width * height;    return(area); } width height   s
Image of page 49
Image of page 50
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern