Use object pointer to reference virtual method
#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();
}
Use & to get object address
#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();
}
Change the object pointer behaviour
#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;
}
Using an array of class objects
#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;
}
Use dynamic_cast to convert object pointer to its subclass
#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);
}
Need reinterpret cast to perform pointer conversion from unrelated classes
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);
}
Need reinterpret_cast to go from pointer to int and from int to pointer
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);
}
Need reinterpret cast to perform reference conversion from unrelated classes -- static_cast doesn't work
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);
}