{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis preview shows pages 1–9. 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 Document Right 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 Document Right 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 Document Right 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 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
Background image of page 7

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

View Full Document Right Arrow Icon
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.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}