int main(){ Shelter sh; Cat c1("Cat1"); Cat c2("Cat2"); Cat c3("Cat3"); Dog d1("Dog1"); Dog d2("Dog2"); Dog d3("Dog3"); sh.enqueue(c1); sh.enqueue(d1); sh.enqueue(d2); sh.enqueue(c2); sh.enqueue(c3); sh.enqueue(d3); Animal ani = sh.dequeueAny(); cout << "DeuqeAny: " << ani.getName() << endl; Cat cc = sh.dequeueCat(); cout << "DeuqeCat: " << cc.getName() << endl; Dog dd = sh.dequeueDog(); cout << "DeuqeDog: " << dd.getName() << endl; }
int main() { Cat * Family[500]; int i; Cat * pCat; for (i = 0; i < 500; i++) { pCat = new Cat; pCat->SetAge(2*i +1); Family[i] = pCat; } /* for (i = 0; i < 500; i++) { cout << "Cat #" << i+1 << ": "; cout << Family[i]->GetAge() << endl; } */ cout<<sizeof(Family[0])<<"\t\tbytes"<<endl; cout<<sizeof(*Family[0])<<"\t\tbytes"<<endl; cout<<sizeof(Family)<<endl; cout<<sizeof(*pCat)<<endl; cout<<sizeof(pCat)<<endl; cout<<sizeof(Cat*)<<endl; cout<<sizeof(Cat)<<endl; cout<<sizeof(int)<<endl; return 0; }
int main(int argc, const char * argv[]) { Dog fido; Cat kitty; fido.talk(); kitty.talk(); return 0; }
int main(void) { Animal *animal = NULL; animal = new Dog; animal->cry(); animal->doWork(); cout << " -----" << endl; Dog * dog = NULL; dog = dynamic_cast<Dog*>(animal); //dynamic_cast是将父类指针转换成子类指针 if (dog != NULL) { cout << "转换成功" << endl; dog->cry(); dog->doWork(); } else { cout << "转换失败" << endl; } Cat *cat = NULL; //想通过dynamic_cast 将animal转换成一个cat指针 cat = dynamic_cast<Cat*>(animal); //通过将animal指针转换成Cat指针 //尝试将一只狗转换成一只猫 if (cat != NULL) { cout << "转换成功" << endl; cat->cry(); cat->doWork(); } else { cout << "转换失败" << endl; } delete animal; return 0; }
// VPTR_NO_NULL-LABEL: define void @_Z13invalid_cast2v void invalid_cast2() { // We've got a pointer to an alloca, so there's no run-time null check needed. // VPTR_NO_NULL-NOT: call void @__ubsan_handle_type_mismatch // VPTR_NO_NULL: call void @__ubsan_handle_dynamic_type_cache_miss Cat cat; cat.speak(); }
int main(int argc , char* argv[]) { if (argc == 1) { printUsage (&cout , argv [0]); return 0; } Cat MyCat (argc , argv); int nPos = isArg (argc , argv); ostream* Out = NULL; ofstream OutFile; if (nPos != -1) { string Path = getFile (argv [nPos]); OutFile.open (Path.c_str () , ios::app); Out = &OutFile; } else { Out = &cout; } MyCat.print (Out); if (nPos != -1) { OutFile.close (); } return 0; }
int mainddddd (int argc, const char * argv[]) { Fish* nemo = new Fish("Nemo"); Fish* dory = new Fish("Dory"); Cat* kitty = new Cat("Kitty"); Dog* spot = new Dog("Spot"); Beagle* max = new Beagle("Max"); /* Let's print out their info with the function I declared at the top! Notice how the function takes Animals, yet we're passing in Dogs, Fish, Cats, etc. printAnimalInfo(nemo); printAnimalInfo(dory); printAnimalInfo(kitty); printAnimalInfo(spot); printAnimalInfo(max); */ //The animals are casuing mischief! spot->chaseCat(kitty); kitty->eatAFish(nemo); kitty->eatAFish(dory); return 0; }
int main () { // Cat and dog instantiations Cat cat_jc("Jean-Claude", 14); Cat cat_jp("Jean-Pierre", 9); Dog dog_h("Helios", 1); // Vector instantiation std::vector<Pet*> pets; // Insert cats and the dog into the vector pets.reserve(3); pets.push_back(&cat_jc); pets.push_back(&cat_jp); pets.push_back(&dog_h); for (std::vector<Pet*>::const_iterator pets_it = pets.begin(); pets_it != pets.end(); ++pets_it) { // Use the dynamic cast Cat *cat = dynamic_cast<Cat*>(*pets_it); Dog *dog = dynamic_cast<Dog*>(*pets_it); if (cat) { cat->pee("outside"); } if (dog) { dog->pee("on the home's room floor"); dog->vomit("on the home's kitchen floor"); } } return 0; }
int main() { Shelter aq; Cat c1("Cat1"); Cat c2("Cat2"); Cat c3("Cat3"); Dog d1("Dog1"); Dog d2("Dog2"); Dog d3("Dog3"); aq.enqueue(d1); aq.enqueue(c1); aq.enqueue(c2); aq.enqueue(c3); aq.enqueue(d2); aq.enqueue(d3); Animal a = aq.dequeueAny(); cout << "Get your pet: " << a.getName() << endl; Cat c = aq.dequeueCat(); cout << "Get your cat: " << c.getName() << endl; Dog d = aq.dequeueDog(); cout << "Get your dog: " << d.getName() << endl; return 0; }
int main() { Cat Tom; Tom.color = "blue"; // eroare: Cat::color este privat Tom.SetColor("blue and white"); // ok cout << Tom.GetColor(); return 0; }
int main() { Cat c; Sound theSound; c.letsDo(&theSound); Dog d; d.letsDo(&theSound); }
void Node::display() { cout << "Name: " << itsCat->getName() << ", "; cout << "age: " << itsCat->getAge() << endl; if(itsNext) itsNext->display(); }
int main() { const Cat c; c.MakeSound(); const Dog d; d.MakeSound(); }
void f11_2(){ Cat frisky; frisky.SetAge(5); frisky.Meow(); cout <<"cat age=" <<frisky.GetAge() <<endl; frisky.Meow(); }
int main(void) { cout << "Test" << endl; Dog dog; dog.bark(); Cat cat; cat.sayMiew(); }
int main() { cout << "Starting program...." << endl; { Cat bob; bob.speak(); } cout << "Stopping program...." << endl; return 0; }
int main() { Cat Frisky; Frisky.SetAge(5); Frisky.Meow(); std::cout << "Frisky is a cat who is "; std::cout << Frisky.GetAge() << "years old.\n"; Frisky.Meow(); return 0; }
int main () { cout << "Hello World" << endl; Animal a; a.speak(); Cat cat; cat.speak(); cat.jump(); return 0; }
int main () { std::cout << "main" << std::endl ; Cat cirmi ( 4 ) ; Dog bodri ( 3 ) ; cirmi.Meow () ; bodri.Bark () ; return 0 ; }
int main(){ cout << sizeof(Animal) << endl; cout << sizeof(Cat) << endl; Cat cat; cat.show(); cat.catfun(); Fish fish; fish.name="test"; fish.age=10; fish.type="a"; fish.show(); fish.fishfun(); }
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: }
int main() { Animal *animal = new Animal; Dog *dog = new Dog; GermanShepard *germanShepard= new GermanShepard; Cat *cat = new Cat; animal->getClass(); dog->getClass(); germanShepard->getClass(); cat->getClass(); whatClassAreYou(animal); whatClassAreYou(dog); whatClassAreYou(germanShepard); whatClassAreYou(cat); Dog spot; GermanShepard max; Cat mini; Animal* ptrDog = &spot; Animal* ptrGShepart = &max; Animal* ptrCat = &mini; ptrDog->getFamily(); ptrDog->getClass(); ptrGShepart->getFamily(); ptrGShepart->getClass(); ptrGShepart->makeSound(); ptrCat->getFamily(); ptrCat->getClass(); ptrCat->makeSound(); Animal* pCat = new Cat; Animal* pDog = new Dog; pCat->makeSound(); pDog->makeSound(); Car* stationWagon = new StationWagon(); stationWagon->getNumWheels(); cin.get(); return 0; }
int main() { Base b; Cat c; Dog d; b.iam(); // "Base" c.iam(); // "Cat" d.iam(); // "Dog" Base *p = &c; p->iam(); // "Cat" p = &d; p->iam(); // "Dog" }
int main() { Bird b; Cat c; Pet* p[] = { &b, &c, }; for(int i = 0; i < sizeof p / sizeof *p; i++) cout << p[i]->type() << " eats " << p[i]->eats()->foodType() << endl; // Can return the exact type: Cat::CatFood* cf = c.eats(); Bird::BirdFood* bf; // Cannot return the exact type: //! bf = b.eats(); // Must downcast: bf = dynamic_cast<Bird::BirdFood*>(b.eats()); } ///:~
int main() { Bird b; Cat c; Pet* p[] = { &b, &c, }; for(int i = 0; i < sizeof p / sizeof *p; i++) cout << p[i]->type() << " je " << p[i]->eats()->foodType() << endl; // Funkcja moze zwrocic dokladny typ: Cat::CatFood* cf = c.eats(); Bird::BirdFood* bf; // Funkcja nie moze zwrocic dokladnego typu: //! bf = b.eats(); // Trzeba rzutowac w dol: bf = dynamic_cast<Bird::BirdFood*>(b.eats()); } ///:~
int main() { const Cat c; c.MakeSound(); const Dog d; d.MakeSound(); //Now use the same Animal pointer for different animals Animal * animal{new Cat}; animal->MakeSound(); delete animal; animal = new Dog; animal->MakeSound(); delete animal; }
//Insert //Orders cats based on their ages //Algorithm: If the cat is younger than you, insert it //before this one. //Otherwise, if last in line, or if the new cat is older than you, //and also younger than next in line, insert it after this one. //Otherwise call insert in the next in line void Node::insert(Node * newNode) { int newAge = newNode->getCat()->getAge(); int thisCatsAge = itsCat->getAge(); if ( newAge < thisCatsAge) { Cat * newCat = newNode->getCat(); Node * oldNode = new Node(itsCat); oldNode->setNext(itsNext); itsNext = oldNode; itsCat = newCat; delete oldNode; oldNode = 0; return; } if(!itsNext) { itsNext = newNode; return; } int nextCatsAge = itsNext->getCat()->getAge(); if ( newAge >= thisCatsAge && newAge < nextCatsAge ) { newNode->setNext(itsNext); itsNext = newNode; return; } itsNext->insert(newNode); }
void shelter::enqueue(Animal &newitem) { if (typeid(newitem)==typeid(Cat)) { Cat *newcat; newcat = new Cat(newitem); newcat->SetOrder(ID++); cat.push(newcat); } else { Dog *newdog; newdog = new Dog(newitem); newdog->SetOrder(ID++); dog.push(newdog); } }
void NyanApp::update() { nyan.update(); if(cinder::app::getElapsedFrames()/45<15){ if((float)(cinder::app::getElapsedFrames()/45) ==((float)cinder::app::getElapsedFrames())/45){ stars[cinder::app::getElapsedFrames()/45] = Star(); } } for(int i=0; i<15; i++){ stars[i].update(); if(stars[i].collide(nyan.getMouth())){ stars[i] = Star(); score++; // eattrack->play(); } } }
void NyanApp::keyUp( KeyEvent event ) { if(event.getChar()==' '){ nyan.setSpace(false); } if(event.getCode()==27){ exit(0); } }