C++ Week 6 Practice: Pointers, Static Members, Operator Overloading, and Inheritance
This Week 6 C++ programming practice focuses on key concepts such as pointers and static members that are defined at the class level but applicable across instances. Explore operator overloading for input output operations and the distinction between base and derived class functions through method overriding. Learn about polymorphism with virtual functions and constructors, observing the creation chain from the base class to derived classes. Enhance your understanding of class relationships and the use of friend functions in C++.
C++ Week 6 Practice: Pointers, Static Members, Operator Overloading, and Inheritance
E N D
Presentation Transcript
C++ Programming Practice Time Week 6.
This • Pointer for current executing instance
Practice : Static Member Member not on instance, but on definition Share-able in all instance of same class
File Separate Implement Define
Practice : Friend • Please Check Professor’s Material
Practice : Operator Overloading • Overload • operator << (int Int_Val) {printf(“%d”, Int_Val;)} • operator << (const char* pCh) {printf(“%s”, pCh;)} • … • Override • Base Class function f_A • Derived Class function f_A • BaseClass *pBc = new DerivedClass(); • pBc->f_A(); //Derived Class function f_A will be called
Inheritance • ex) • Room • Rm_Quiz, Rm_Dead, Rm_End
Protected • Allowed • Inner • Derived Class • Denied • Outer • Derived’s Derived
Constructor Chain • Base Class : Room • Derived Class : Rm_Dead • When Rm_Dead created. • 1) Room’s constructor called • 2) Rm_Dead’s constructor called • From Deep, To Shallow
Polymorphism • Override • Base Class function f_A • Derived Class function f_A • BaseClass *pBc = new DerivedClass(); • pBc->f_A(); //Derived Class function f_A will be called • Type : BaseClass • Instance : DerivedClass
Protected • Derived class will implement virtual function for its purpose