C++/Class/object pointer
Содержание
- 1 Call virtual function through object pointer
- 2 Change the object pointer behaviour
- 3 Declare object pointer
- 4 Demonstrating the class member access operators . and ->
- 5 Need reinterpret_cast to go from pointer to int and from int to pointer
- 6 Need reinterpret cast to perform pointer conversion from unrelated classes
- 7 Need reinterpret cast to perform reference conversion from unrelated classes -- static_cast doesn"t work
- 8 pointers to base class
- 9 Use dynamic_cast to convert object pointer to its subclass
- 10 Use object pointer to reference virtual method
- 11 Use & to get object address
- 12 Using an array of class objects
Call virtual function through object pointer
<source lang="cpp">
- include <iostream>
using namespace std; class base {
public: virtual void vfunc(void) { cout << "This is base"s vfunc()." << endl; }
}; class derived1 : public base {
public: void vfunc(void) { cout << "This is derived1"s vfunc()." << endl; }
}; class derived2 : public derived1 { }; int main(void) {
base *p, b; derived1 d1; derived2 d2; p = &b; p->vfunc(); p = &d1; p->vfunc(); p = &d2; p->vfunc(); } </source>
Change the object pointer behaviour
<source lang="cpp">
- include <iostream>
using namespace std; class convert {
protected: double val1; double val2; public: convert(double i) { val1 = i; } double getconv(void) {return val2;} double getinit(void) {return val1;} virtual void compute(void) = 0;
}; class l_to_g : public convert {
public: l_to_g(double i) : convert(i) { } void compute(void) { val2 = val1 / 3.7854; }
};
class f_to_c : public convert {
public: f_to_c(double i) : convert(i) { } void compute(void) { val2 = (val1 - 32) / 1.8; }
}; int main(void) {
convert *p; l_to_g lgob(4); f_to_c fcob(70); p = &lgob; cout << p->getinit() << " liters is "; p->compute(); cout << p->getconv() << " gallons." << endl; p = &fcob; cout << p->getinit() << " in Fahrenheit is "; p->compute(); cout << p->getconv() << " Celsius." << endl;
}
</source>
Declare object pointer
<source lang="cpp">
- include <iostream>
using namespace std; class Base {
public: void base_message(void) { cout << "This is the base class\n"; };
}; class Derived: public Base {
public: void derived_message(void) { cout << "This is the derived class\n" ; };
}; int main(void) {
Base *base_pointer = new Base; Derived *derived_pointer = new Derived; base_pointer->base_message(); derived_pointer->derived_message();
}
</source>
Demonstrating the class member access operators . and ->
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl; // Simple class Count class Count { public:
int x; void print() { cout << x << endl; }
}; int main() {
Count counter, *counterPtr = &counter, &counterRef = counter; cout << "Assign 7 to x and print using the object"s name: "; counter.x = 7; counter.print(); cout << "Assign 8 to x and print using a reference: "; counterRef.x = 8; counterRef.print(); cout << "Assign 10 to x and print using a pointer: "; counterPtr->x = 10; counterPtr->print(); return 0;
}
</source>
Need reinterpret_cast to go from pointer to int and from int to pointer
<source lang="cpp"> class X {}; class Y {}; int main(int argc, char** argv) {
int i = 3; X x; Y y; X* xp; Y* yp; i = reinterpret_cast<int>(xp); xp = reinterpret_cast<X*>(i); return (0);
}
</source>
<source lang="cpp"> class X {}; class Y {}; int main(int argc, char** argv) {
int i = 3; X x; Y y; X* xp; Y* yp; xp = reinterpret_cast<X*>(yp);
return (0);
}
</source>
<source lang="cpp"> class X {}; class Y {}; int main(int argc, char** argv) {
int i = 3; X x; Y y; X* xp; Y* yp; X& xr = x; Y& yr = reinterpret_cast<Y&>(x); return (0);
}
</source>
pointers to base class
<source lang="cpp">
- include <iostream>
using namespace std; class CPolygon {
protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; }
}; class CRectangle: public CPolygon {
public: int area () { return (width * height); }
}; class CTriangle: public CPolygon {
public: int area () { return (width * height / 2); }
}; int main () {
CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0;
}
</source>
Use dynamic_cast to convert object pointer to its subclass
<source lang="cpp">
- include <typeinfo>
- include <iostream>
using namespace std; class Base{ public:
Base() {}; virtual ~Base() {}
}; class Derived : public Base{ public:
Derived() {} virtual ~Derived() {}
}; int main(int argc, char** argv){
Base* b; Derived* d = new Derived(); b = d; d = dynamic_cast<Derived*>(b); Base base; Derived derived; Base& br = base; try { Derived& dr = dynamic_cast<Derived&>(br); } catch (bad_cast&) { cout << "Bad cast!\n"; } return (0);
}
</source>
Use object pointer to reference virtual method
<source lang="cpp">
- include <iostream>
using namespace std; class Base {
public: virtual void show_message(void) { cout << "This is the base class\n"; };
}; class Derived: public Base {
public: virtual void show_message(void) { cout << "This is the derived class\n" ; };
}; int main(void){
Base *base_pointer = new Base; base_pointer->show_message(); base_pointer = new Derived; base_pointer->show_message();
}
</source>
Use & to get object address
<source lang="cpp">
- include <iostream>
using namespace std; class base {
public: virtual void vfunc(void) { cout << "This is base"s vfunc()." << endl; }
}; class derived1 : public base {
public: void vfunc(void) { cout << "This is derived1"s vfunc()." << endl; }
}; class derived2 : public derived1 {
public: void vfunc(void) { cout << "This is derived2"s vfunc()." << endl; }
}; int main(void) {
base *p, b; derived1 d1; derived2 d2; p = &b; // Point to base class p->vfunc(); p = &d1; // Point to first derived class p->vfunc(); p = &d2; // Point to second derived class p->vfunc();
}
</source>
Using an array of class objects
<source lang="cpp">
- include <iostream>
using namespace std; class CBox { public: CBox(double lv, double bv = 1.0, double hv = 1.0): m_Length(lv), m_Breadth(bv), m_Height(hv) { cout << endl << "Constructor called."; } CBox() { cout << endl << "Default constructor called."; m_Length = m_Breadth = m_Height = 1.0; } // Function to calculate the volume of a box double Volume() const { return m_Length*m_Breadth*m_Height; } private: double m_Length; double m_Breadth; double m_Height; }; int main() { CBox boxes[5]; CBox cigar(8.0, 5.0, 1.0); cout << endl << "Volume of boxes[3] = " << boxes[3].Volume() << endl << "Volume of cigar = " << cigar.Volume(); cout << endl; return 0; } </source>