lecture14%2Dpost

lecture14%2Dpost - Wednesday,November24th StructureReview

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, November 24 th Structure Review More on Objects and Classes in C++ A little more on pointers: New and Delete
Background image of page 1

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

View Full DocumentRight Arrow Icon
Structure Review truct PERSON string name; int age; ; truct FAMILY PERSON mother; PERSON father; PERSON child; string last_name; // for the entire family ; nt main(void) FAMILY my_family, *ptr, other; // Q1: Set my_family ’s mother’s name to Linda // Q2: Set ptr to point to my_family // Q3: Set the age of my_family ’s father to 55, using ptr // Q4: Set the other family equal to my_family
Background image of page 2
struct PERSON { string name; int age; }; PERSON get_person(void) { PERSON temp; cin >> temp.name; cin >> temp.age; return(temp); } int main(void) { PERSON me; me = get_person(); cout << me.name; } Structure Review What is printed out if the user types  “Voldemort” for the name?
Background image of page 3

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

View Full DocumentRight Arrow Icon
struct foo { int geek; char nerd; float goober; }; void func(foo achoo) { cout << achoo.geek << endl; achoo.nerd = ‘C’; } void main(void) { foo a = {10,’A’,3.14159}; func(a); cout << a.nerd << endl; }   Structure Review What is printed by this program? Let’s work it out on the chalk  board.
Background image of page 4
Classes and Member Functions You can define your member  functions directly inside  your class. class  Rect {      public:     int width;     int height; }; private:   Or you can define your member  functions outside the class too.    int GetArea()    {       int area = width * height;      return(area);    }       void Initialize(int stW, int stH)    {        width = stW;       height = stH;    }     int GetArea()    void Initialize(int stW, int stH)       int GetArea()    {       int area = width * height;      return(area);    }       void Initialize(int stW, int stH)    {        width = stW;       height = stH;    }
Background image of page 5

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

View Full DocumentRight Arrow Icon
class  Rect {      public:     int width;     int height; }; private:      int GetArea()    void Initialize(int stW, int stH)         int GetArea()    int GetArea()    {       int area = width * height;      return(area);    }      void Initialize(int stW, int stH)    {        width = stW;       height = stH;    } ; ; These are basically prototypes  now… ( don’t forget the semicolons ) And we have to make a small  change to our actual functions  too… GetArea() Initialize(int stW, int stH) We place the  class’s name   followed by  two colons   between the return type and  the function name. int main(void) {    Rect r;    r.Initialize(10,20);   // OK    cout  << r.GetArea (); // OK } Rect Rect :: :: Classes and Member Functions
Background image of page 6
Both are The Same! class
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

lecture14%2Dpost - Wednesday,November24th StructureReview

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online