{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture14 - Wednesday November 24th Str uctur e Revi ew M...

Info icon This preview shows pages 1–10. 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
Image of page 1

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

View Full Document Right Arrow Icon
Operator Precedence…
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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?
Image of page 4
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.
Image of page 5

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

View Full Document Right Arrow Icon
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; }
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
Both are The Same! class Rect { int width; int height; }; private: int GetArea() { int area = width * height; return(area); } void Initialize(int stW, int stH) { width = stW; height = stH; } public: class Rect { int width; int height; }; private: int GetArea(); void Initialize(int stW, int stH); public: int Rect::GetArea() { int area = width * height; return(area); } void Rect::Initialize(int stW, int stH) { width = stW; height = stH; } class declaration class implementation class declaration
Image of page 8
Reduce, Reuse, Recycle Once you have designed , programmed and tested a useful class, you can reuse it over and over in all of your programs. When you’re done programming a new class, do the following: 1. Place your class declaration into a .H file. 2. Place your class implementation (methods defined outside the class) into a .CPP file.
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
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