Exemplo n.º 1
0
int main(void)
{
    Cat b;
    Animal *a = &b;
    a->eat();

    return 0;
}
Exemplo n.º 2
0
Arquivo: main.cpp Projeto: zoommm/long
int main(void)
{
    Animal an;
    Person p;
    an.eat();
    p.eat();
    
    return 0;
}
Exemplo n.º 3
0
int main()
{
    Fish* fish = Fish_new();    // 创建鱼对象
    Dog* dog = Dog_new();       // 创建狗对象
    Car* car = Car_new();       // 创建车子对象

    Animal* animals[2] = { 0 };     // 初始化动物容器(这里是Animal指针数组)
    IMoveable* moveObjs[3] = { 0 }; // 初始化可移动物体容器(这里是IMoveable指针数组)

    int i = 0;                  // i和j是循环变量
    int j = 0;

    // 初始化鱼对象的昵称为:小鲤鱼,年龄为:1岁
    fish->init(fish, "Small carp", 1);

    // 将fish指针转型为Animal类型指针,并赋值给animals数组的第一个成员
    animals[0] = SUPER_PTR(fish, Animal);

    // 初始化狗对象的昵称为:牧羊犬,年龄为:2岁
    dog->init(dog, "sheepdog", 2);

    // 将dog指针转型为Animal类型指针,并赋值给animals数组的第二个成员
    animals[1] = SUPER_PTR(dog, Animal);

    // 将fish指针转型为IMoveable接口类型指针,并赋值给moveOjbs数组的第一个成员
    moveObjs[0] = SUPER_PTR(fish, IMoveable);

    // 将dog指针转型为IMoveable接口类型指针,并赋值给moveOjbs数组的第二个成员
    moveObjs[1] = SUPER_PTR(dog, IMoveable);

    // 将car指针转型为IMoveable接口类型指针,并赋值给moveOjbs数组的第三个成员
    moveObjs[2] = SUPER_PTR(car, IMoveable);

    // 循环打印动物容器内的动物信息
    for(i=0; i<2; i++)
    {
        Animal* animal = animals[i];
        animal->eat(animal);
        animal->breathe(animal);
        animal->sayHello(animal);
    }

    // 循环打印可移动物体容器内的可移动物体移动方式的信息
    for(j=0; j<3; j++)
    {
        IMoveable* moveObj = moveObjs[j];
        moveObj->move(moveObj);
    }

    lw_oopc_delete(fish);
    lw_oopc_delete(dog);
    lw_oopc_delete(car);

    return 0;
}
Exemplo n.º 4
0
Arquivo: main.cpp Projeto: CCJY/coliru
int main (){
    Animal *animal = new Animal;
    Cat *cat = new Cat;
    
    animal->eat(); // outputs: "I'm eating generic food."
    cat->eat();    // outputs: "I'm eating a rat."
    
    cout << endl;
    
    func(animal); // outputs: "I'm eating generic food."
    func(cat);    // outputs: 
    
}
Exemplo n.º 5
0
int main()
{
	Animal *aptr; 
	Animal a; 
	Cat c; 
	char choice;
	cout << "Do you want a cat or any animal" << endl; 
	cin >> choice; 
	if (choice == 'a')
	{
		aptr = &a;//new Animal;
	}
	else
	{
		aptr = &c;//new Cat;
	}
	int dchoice = 0; 
	while (dchoice!= -1)
	{
		cout << "Do you want to feed your pet (1), play with it (2), printinfo(3) or quit(-1)" << endl;
		cin >> dchoice; 
		switch (dchoice)
		{
		case 1:
			aptr->eat(); 
			break; 
		case 2:
			aptr->sound(); 
			break; 
		case 3:
			aptr->printinfo(); 
			break; 
		default:
			break;
		}
	}
//	delete aptr;
	return 0;
}
Exemplo n.º 6
0
int main(){

	Animal an;
	an.eat();
	// cannot be accessed
//	an.breathe();

	Fish fh;
	// fh.breathe();
	// fh.sleep();

	cout << "test Animal *pAn" << endl;
	Animal *pAn;
	pAn = &fh;
	// call for Animal's breathe()
	// since Animal's object and Fish' object
	// have the same start address
	// However, if we virtual the Animal's breathe()
	// then this will call Fish's breathe()
	fn(pAn);

	// pointers vs reference
	int a = 8;
	// ref: MUST initialize &b
	// This is NOT get address of a
	// It is a alias name, it does have address
	// It can be used to param transfer
	int &b = a;
	b = 5;
	cout << "b = " << b << endl;

	// This is *pC point to c's address
	int c = 9;
	int *pC = &c;
	cout << "*pC = " << *pC << endl;

	return 0;
}