disc 9 notes

disc 9 notes - Cat *pKitty2 = new Cat(10);...

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

View Full Document Right Arrow Icon
Discussion 9 Output 1 Kitty 1 is 1 years old Kitty 2 is 3 years old Meow! //Member functions class Cat { public: Cat(); Cat(int initAge); int m_age; //member variables void meow(); }; void Cat::meow() { cout << "Meow!" << endl; m_age++; } //constructor Cat::Cat() { setAge(0); cout << "A cat's born"; } // //destructor //deleting a cat Cat *p1 = n32 Cat; delete p1; Cat::Cat(int initAge) { Cat(); setAge(initAge); } //modifier void Cat::setAge(int newAge) { //set m_age only if // 0 <= newAge <= 100
Background image of page 1

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

View Full DocumentRight Arrow Icon
m_age = newAge } int main() { Cat kitty1; //calls the default one Cat kitty2(2);//calls the second cat function Cat *p1 = new Cat(2); //Dynamic Allocation Cat *pKitty = new Cat;
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Cat *pKitty2 = new Cat(10); (*pKitty).meow(); //same as the next line. It calls the meow function on the cat that pkitty points to pKitty-&gt;meow(); } class SomeOtherClass { public: ... void someFunction(Cat* cp); }; class Cat { public: ... void someCatFunction(); }; void Cat::someCatFunction() { SomeOtherClass soc; soc.someFunction(this); } //global function void printCat(Cat *p) { cout &lt;&lt; p-&gt;age(); //arrow because p is a pointer } //Let's say you want to use the global function printCat inside a member function void Cat::printMe { printCat(this); } in the valley, robot points to itself //...
View Full Document

Page1 / 3

disc 9 notes - Cat *pKitty2 = new Cat(10);...

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

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