class user { private: int age; string name; .. public: void setAge(int new); }
class MyClass { private: const int number; // deklarace privatni konstantni promenne MyClass():number(10) {} // konstruktor s definici promenne }
class MyClass { private: static const int number; // deklarace } const int MyClass::number = 10; // definice
class Dog : public Animal - co je public u rodiče, je i u potomkaclass Dog : protected Animal - co je public u rodiče, je u potomka jako protectedclass Dog : private Animal - co je public u rodiče, je u potomka jako privateclass Animal { public: void eat() { std::cout << "I'm eating generic food."; } } class Cat : public Animal { public: void eat() { std::cout << "I'm eating a rat."; } } //this can go at the top of the main.cpp file void func(Animal *xyz) { xyz->eat(); } Animal *animal = new Animal; Cat *cat = new Cat; animal->eat(); // outputs: "I'm eating generic food." cat->eat(); // outputs: "I'm eating a rat." func(animal) // outputs: "I'm eating generic food." func(cat) // outputs: "I'm eating generic food."
class Animal { public: virtual void eat() { std::cout << "I'm eating generic food."; } }
class Animal { public: virtual void eat() =0; }
http://www.cplusplus.com/doc/tutorial/polymorphism/
mám obecnou třídu a pak specifické podtřídy. V konstruktoru se inicializuje pro každou specifickou třídu něco jiného. Obecná třída má metodu run. Chci udělat frontu (vector) obecné třídy a při zpracování pak volat metodu run, která zachová podle hodnot nastavených v konstruktoru. Problém: jak to do té fronty dostat?
Když mám objekt obecné třídy, nelze jej přetypovat na různé podtřídy.
http://stackoverflow.com/questions/5313322/c-cast-to-derived-class
class Animal { /* Some virtual members */ } class Dog: public Animal {}; class Cat: public Animal {}; Dog dog; Cat cat; Animal& AnimalRef1 = dog; // Notice no cast required. (Dogs and cats are animals). Animal& AnimalRef2 = cat; Animal* AnimalPtr1 = &dog; Animal* AnimlaPtr2 = &cat; Cat& catRef1 = dynamic_cast<Cat&>(AnimalRef1); // Throws an exception AnimalRef1 is a dog Cat* catPtr1 = dynamic_cast<Cat*>(AnimalPtr1); // Returns NULL AnimalPtr1 is a dog Cat& catRef2 = dynamic_cast<Cat&>(AnimalRed2); // Works Cat* catPtr2 = dynamic_cast<Cat*>(AnimalPtr2); // Works // This on the other hand makes no sense // An animal object is not a cat. Therefore it can not be treated like a Cat. Animal a; Cat& catRef1 = dynamic_cast<Cat&>(a); // Throws an exception Its not a CAT Cat* catPtr1 = dynamic_cast<Cat*>(&a); // Returns NULL Its not a CAT.
Animal animal = cat; // This works. But it slices the cat part out and just // assigns the animal part of the object. Cat bigCat = animal; // Makes no sense. // An animal is not a cat!!!!! Dog bigDog = bigCat; // A cat is not a dog !!!!
std::vector<Animal*> barnYard; barnYard.push_back(&dog); barnYard.push_back(&cat); barnYard.push_back(&duck); barnYard.push_back(&chicken); Dog* dog = dynamic_cast<Dog*>(barnYard[1]); // Note: return NULL as this was the cat.