Beispiel #1
0
int main() {
  int a = 10;
  int* p = new int(1000);
  int& ref = a;

  std::cout << "Values of a p and ref: " << a << " " << *p << " " << ref << "\n";

  // Modify ref, check if a changed.
  ++ref;
  std::cout << "a and ref: " << a << " " << ref << "\n";

  // Implicit subclass to parent class pointer conversion
  Parent* parentP = new Child();
  parentP->print();
  Child* childP = static_cast<Child*>(parentP);
  childP->print();
  Parent* parentP1 = new Parent();
  parentP1->print();

  Child child = Child();
  parentP = &child;
  (*parentP).print();
  parentP = parentP1;
  parentP->print();
  child.print();
  
  return 0;
}
Beispiel #2
0
/*
 * This function prints the names of all the children in the family.
 *
 * @attribute: Child *children - head pointer to linked list of children
 */
void Wife::printChildren()
{
  Child *child = children;
  
  while(child != NULL){
    child->print();
    child = child->getSibling();
  }
}
Beispiel #3
0
Datei: 4.cpp Projekt: shixv/test
int main(void)
{
	Child array[] = { Child(0), Child(1), Child(2) };
	//						array[0]    array[1]  array[2]
	Child *cp = &array[0];
	Parent *pp = &array[0];
	cp->print(); // 子类
	pp->print(); // 子类
	//父类指针 指向子类对象的时候, 如果想发生多态,不要给父类指针累加, 
	cp++; //指针的++ cp+= sizeof(Child)
//	pp++;//               cp += sizeof(Parent)
//	cp->print(); // 子类
//	pp->print(); // 子类
	cout << "-----" << endl;
	int i = 0;
	for (i = 0, cp = &array[0], pp = cp; i < 3; i++, cp++, pp = cp) {
		cp->print();
		pp->print();
	}
	return 0;
}