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() { Dog x; x.setName("Lerothodi"); x.setAge(5); x.setWeight(60); x.setBreed("German Shepherd"); cout << "Name : " << x.getName() << endl; cout << "Age : " << x.getAge() << " years." << endl;; cout << "Weight : " << x.getWeight() << " lb." << endl; cout << "Breed : " << x.getBreed() << endl; cout << "Lifespan: " << x.getLifespan() << endl; cout << endl; Pet y; y.setName("Leeuw"); y.setAge(7); y.setWeight(55); cout << "Name : " << y.getName() << endl; cout << "Age : " << y.getAge() << " years." << endl;; cout << "Weight : " << y.getWeight() << " lb." << endl; cout << "Lifespan: " << y.getLifespan() << endl; return 0; }
int main() { Dog dog; dog.speak(); return 0; }
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() { Dog d = std::string("BArao") ; d.bark(); }
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(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; }
int main(){ Dog d; int i = 9; d.setAge(i); cout << i << endl; }
int main(int argc, const char * argv[]) { Dog fido; Cat kitty; fido.talk(); kitty.talk(); return 0; }
int main() { Dog vdog; Pet vpet; vdog.name = "Tiny"; vdog.breed = "Great Dane"; vpet = vdog; //vpet.breed; is illegal since class Pet has no member named breed Dog *pdog; pdog = new Dog; pdog->name = "Tiny"; pdog->breed = "Great Dane"; Pet *ppet; ppet = pdog; ppet->print(); // These two print the same output: pdog->print(); // name: Tiny breed: Great Dane //The following, which accesses member variables directly //rather than via virtual functions, would produce an error: //cout << "name: " << ppet->name << " breed: " // << ppet->breed << endl; //generates an error message: 'class Pet' has no member //named 'breed' . //See Pitfall section "Not Using Virtual Member Functions" //for more discussion on this. return 0; }
int main() { Cat c; Sound theSound; c.letsDo(&theSound); Dog d; d.letsDo(&theSound); }
int main() { const Cat c; c.MakeSound(); const Dog d; d.MakeSound(); }
int main() { Dog fido; // create a dog fido.speak(); fido.wagTail(); std::cout << "Fido is " << fido.getAge() << " years old\n"; return 0; }
Dog::Dog(Dog &oldDog) { name = new char[17]; strncpy( name, oldDog.getName(), 16 ); name[16] = '\0'; height = oldDog.getHeight(); weight = oldDog.getWeight(); }
int main(void) { cout << "Test" << endl; Dog dog; dog.bark(); Cat cat; cat.sayMiew(); }
int main(void) { Animal zh; Dog dg; zh.sleep1(); dg.sleep(); // dg.sleep1(); //////不可直接访问。 return 0; }
int main(){ Dog dg; //dg.setName("Mark"); // this will give error coz setName is protected in dog class, and is unaccessable dg.setDogName("Mark"); dg.show(); return 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; }
int main(int argc, const char * argv[]) { //Animal a;//抽象类不能实例化 Dog d; d.show(); //抽象类虽然不能实例化,但可以用指针指向子类的对象 Animal* a = new Dog(); a->show(); }
int main() { Dog* d = new Dog(); Animal* a = d; // refer to Dog instance with Animal pointer using namespace std; cout << d->Says(); // always Woof cout << a->Says(); // Woof or ?, depends on virtual return 0; }
int main () { std::cout << "main" << std::endl ; Cat cirmi ( 4 ) ; Dog bodri ( 3 ) ; cirmi.Meow () ; bodri.Bark () ; return 0 ; }
int main() { Dog d; int i=9; d.setAge(i); // Compile error :- ambiguity amongst "void setAge(const int a)" and "void setAge(int &a)" cout<<i<<endl; const string& n = d.getName(); cout<<n<<endl; return 0; }
DogHouse& operator=(const DogHouse& dh) { // Check for self-assignment: if(&dh != this) { houseName = dh.houseName + " assigned"; // Clean up what you're using first: p->detach(); p = dh.p; // Like copy-constructor p->attach(); } cout << "DogHouse operator= : " << *this << endl; return *this; }
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() { std::cout << "DOG:" << std::endl; Dog myDog; myDog.setName("Barkley"); myDog.setWeight(10); myDog.print(); std::cout << "OWNEDDOG:" << std::endl; OwnedDog ownedDog; ownedDog.setName("OwnedBarkley"); ownedDog.setOwner("Ale"); ownedDog.setWeight(20); ownedDog.print(); return 0; }
int main() { using namespace std; cout << ~(-10) << endl; cout << (unsigned int)(-10) << endl; Dog dog; dog.eat = Dog_eat; dog.bark(); dog.eat(); return 0; }
void main () { //calling and instantiation of the classes Dog spots = Dog(); cout<<spots.weight << endl; spots.run(); spots.bark(); cout << spots.weight <<endl; system("PAUSE"); }
void QT1::listItemChanged() { int idx = this->getRepoListSelectedIndex(); if (idx == -1) return; std::vector<Dog> dogs = this->currentDogsInRepoList; if (idx > dogs.size())return; Dog s = dogs[idx]; this->nameEdit->setText(QString::fromStdString(s.getName())); this->breedEdit->setText(QString::fromStdString(s.getBreed())); QString w; w.setNum(s.getAge()); this->ageEdit->setText(w); this->linkEdit->setText(QString::fromStdString(s.getPhotograph())); }
Animal dequeueAny() { if (dogs.empty()) return dequeueCats(); else if (cats.empty()) return dequeueDogs(); Dog dog = dogs.front(); Cat cat = cats.front(); if (dog.isOlderThan(cat)) return dequeueDogs(); else return dequeueCats(); }