Welcome to CAVI, the Cisco Academy for the Vision Impaired. Course Fees Linux Wiki HTML Wiki Documentation Index PmWiki FAQ |
Chapter66.1.1Image 1: This image shows a picture of two diagrams defining a subclass. These have been written out below. Single Inheritance: This diagram shows a large square orange box called SuperClass. There is a smaller blue box called SubClass positioned below the SuperClass box with an arrow pointing towards the SuperClass box. Multiple Inheritance: This diagram shows two large square orange boxes called SuperClassA and SuperClassB. There is a smaller blue box called SubClass positioned below the SuperClassA and SuperClassB boxes with an arrow pointing towards the SuperClassA and SuperClassB boxes. 6.1.2Image 1: This image shows an example of defining a subclass in C++. The example code is written out below. #include <iostream> using namespace std; class Super { private: int storage; public: void put(int val) {storage = val; } int get(void) {return storage; } }; int main(void) { Super object; object.put(100); object.put(object.get() + 1); cout<<object.get()<<endl; return 0; } 6.1.3Image 1: This image shows another example of defining a subclass. The code is written out below. class Y : {visibility specifier} X {....}; 6.1.4Image 1: This image shows another example of defining a subclass. The code is written out below. class Sub: Super { }; int main(void) { Sub object; object.put(100); object.put(object.get() + 1); cout<<object.get()<<endl; return 0; } 6.1.5Image 1: This image shows another example of defing a simple subclass. The code is written out below. #include <iostream> using namespace std; class Super { private: int storage; public: void put(int val) {storage = val; } int get(void) {return storage; } }; class Sub : public Super { }; int main(void) { Sub object; object.put(100); object.put(object.get() + 1); cout<<object.get()<<endl; return 0; } 6.1.6Image 1: This image shows another example of defining a sub class. The code is written out below. #include <iostream> using namespace std; class Super { protected: int storage; public: void put(int val) {storage = val; } int get(void) {return storage; } }; class Sub : public Super { public: void print(void) {cout<<”storage = ”<<storage<<endl; } }; int main(void) { Sub object; object.put(100); object.put(object.get() + 1); object.print(); return 0; } 6.1.7Image 1: This image shows a table used for defining sub classes. The table has been written out below.
6.1.8Image 1: This image shows another example of defining a subclass. The code is written out below. #include <iostream> using namespace std; class SuperA { protected: int storage; public: void put(int val) {storage = val; } int get(void) {return storage; } }; class SuperB { protected: int safe; public: void insert(int val) {safe = val; } int takeout(void) {return safe; } }; class Sub : public SuperA, public SuperB { public: void print(void) { cout<<”storage = “<<storage<<endl; cout<<”Safe =”<<safe<<endl; } }; int main(void) { sub object; object.put(1); object.insert(2); object.put(object.get() + object.takeout()); object.insert(object.get() + object.takeout()); object.print(); return 0; } 6.2.1Image 1: This image shows an example of simple type compatibility in C++. The example code is written out below. #include <iostream> using namespace std; class Cat { public: void MakeSound(void) {cout<<”Meow! Meow!”<<endl; } }; class Dog { public: void MakeSound(void) {cout<<”Woof! Woof!”<<endl; } }; int main(void) { Cat *a_cat = new Cat(); Dog *a_dog = new dog(); a_cat -> MakeSound(); a_dog -> MakeSound(); return 0; } 6.2.2Image 1: This image shows an example of a more complex case of type compatibility. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void Run(void) {cout<<Name<<”: i’m running”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Woof! Woof!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Meow! Meow!”<<endl; } }; int main(void) { Pet a_pet(“pet”); Cat a_cat(“Tom”); Dog a_dog(“Spike”); A_pet.Run(); A_dog.Run(); a_dog.MakeSound(); A_cat.Run(); a_cat.MakeSound(); Return 0; } 6.2.3Image 1: This image shows another example of a complex case of type compatibility written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void Run(void) {cout<<Name<<”: i’m running”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Woof! Woof!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Meow! Meow!”<<endl; } }; int main(void) { Pet *a_pet1 = new Cat(“Tom”); Pet *a_pet1 = new Dog(“Spike”); a_pet1 -> Run(); // ‘a_pet1 -> Makesound(); ‘is not allowed here! a_pet2 -> Run(); // ‘a_pet2 -> Makesound(); ‘is not allowed here! return 0; } 6.2.4Image 1: This image shows an example of the static cast operator in C++. The code is written out below. Static_cast<target_type>(an_expression) 6.2.5Image 1: This image shows an example of type compatibility, this time with static cast operators included. The code has been written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void Run(void) {cout<<Name<<”: i’m running”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Woof! Woof!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Meow! Meow!”<<endl; } }; int main(void) { Pet *a_pet1 = new Cat(“Tom”); Pet *a_pet1 = new Dog(“Spike”); a_pet1 -> Run(); static_cast<Cat *>(a_pet1) -> MakeSound(); a_pet2 -> Run(); static_cast<Dog *>(a_pet2) -> MakeSound(); return 0; }; 6.2.6Image 1: This image shows another example of type compatability with static cast operators. However this time the static cast operator has been abused. The code has been written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void Run(void) {cout<<Name<<”: i’m running”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Woof! Woof!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Meow! Meow!”<<endl; } }; int main(void) { Pet *a_pet1 = new Cat(“Tom”); Pet *a_pet1 = new Dog(“Spike”); a_pet1 -> Run(); static_cast<Cat *>(a_pet2) -> MakeSound(); a_pet2 -> Run(); static_cast<Dog *>(a_pet1) -> MakeSound(); return 0; }; 6.2.7Image 1: This image shows one final example of type compatibility. The code has been written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void Run(void) {cout<<Name<<”: i’m running”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {}; void MakeSound(void) {cout<<Name<<”: Meow! Meow!”<<endl; } }; class Persian : public Cat { public: Persian(string n) : Cat(n) {}; }; int main(void) { Pet *a_pet1; Persian *a_persian; a_pet = a_persian = new Persian(“Mr. Bigglesworth”); a_persian -> Makesound(); static_cast<Persian *>(a_pet) -> MakeSound(); return 0; }; 6.3.1Image 1: This image shows an example of overriding a method in a subclass in C++. The example code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void MakeSound(void) {cout<<Name<<”the pet says: Shh! Shh!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the cay says: Meow! Meow!”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the dog says: Woof! Woof!”<<endl; } }; int main(void) { Cat *a_cat; Dog *a_dog; Pet *a_pet1 = new Cat(“Kitty”); Pet *a_pet1 = new Dog(“Doggie”); a_cat -> MakeSound(); static_cast<Pet *>(a_Cat) -> MakeSound(); a_dog -> MakeSound(); static_cast<Pet *>(a_dog) -> MakeSound(); return 0; } 6.3.2Image 1: This image shows another example of overriding a method in a subclass. The code is written out below. #include <iostream> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } void MakeSound(void) {cout<<Name<<”the pet says: Shh! Shh!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the cay says: Meow! Meow!”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the dog says: Woof! Woof!”<<endl; } }; int main(void) { Pet *a_pet1, *a_pet2; Cat *a_cat; Dog *a_dog; Pet *a_pet1 = new Cat(“Kitty”); Pet *a_pet2 = new Dog(“Doggie”); a_pet1 -> Makesound(); a_cat -> Makesound(); a_pet2 -> Makesound(); a_dog -> Makesound(); return 0; } 6.3.3Image 1: This image shows another example of overriding a method in a subclass, this time with polymorphism included. The code is written out below. #include <iostream> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; } virtual void MakeSound(void) {cout<<Name<<”the pet says: Shh! Shh!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the cay says: Meow! Meow!”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the dog says: Woof! Woof!”<<endl; } }; int main(void) { Pet *a_pet1, *a_pet2; Cat *a_cat; Dog *a_dog; Pet *a_pet1 = new Cat(“Kitty”); Pet *a_pet2 = new Dog(“Doggie”); a_pet1 -> Makesound(); a_cat -> Makesound(); static_cast<pet *>(a_cat) -> Makesound(); a_pet2 -> Makesound(); a_dog -> Makesound(); static_cast<pet *>(a_dog) -> Makesound(); return 0; } 6.3.4Image 1: This image shows another example of overriding a method in a subclass with polymorphism included. The code is written out below. #include <iostream> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n; MakeSound();} virtual void MakeSound(void) {cout<<Name<<”the pet says: Shh! Shh!”<<endl; } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the cay says: Meow! Meow!”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the dog says: Woof! Woof!”<<endl; } }; int main(void) { Cat *a_cat; Dog *a_dog; a_cat = new Cat(“Kitty”); a_dog = new Dog(“Doggie”); return 0; } 6.3.5Image 1: This image shows another example of overriding a method in a subclass with polymorphism included. The code is written out below. #include <iostream> using namespace std; class Pet { protected: string Name; public: Pet(string n) {Name = n;} virtual void MakeSound(void) {cout<<Name<<”the pet says: Shh! Shh!”<<endl; } void WakeUp(void) {MakeSound(); } }; class Cat : public Pet { public: Cat(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the cay says: Meow! Meow!”<<endl; } }; class Dog : public Pet { public: Dog(string n) : Pet(n) {} void MakeSound(void) {cout<<Name<<”the dog says: Woof! Woof!”<<endl; } }; int main(void) { Cat *a_cat; Dog *a_dog; a_cat = new Cat(“Kitty”); a_cat -> WakeUP(); a_dog = new Dog(“Doggie”); a_dog -> WakeUP(); return 0; } 6.4.1Image 1: This image shows an example of passing an object as a function parameter in C++. The example code has been written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: void NameMe(string name) {this -> name = name; } void MakeSound(void) {cout<<name<<” says: no comments”<<endl; } }; void PlayWithPetByPointer(string name, Pet *pet) { pet -> NameMe(name); pet -> MakeSound(); } void PlayWithPetByReference(string name, Pet &pet) { pet.NameMe(name); pet.Makesound(); } int main(void) { Pet *P1 = new Pet; Pet p2; PlayWithPetByPointer(“anonymous”, p1); PlayWithPetByReference(“no_name”, p2); PlayWithPetByPointer(“no_name”, &p2); PlayWithPetByReference(“anonymous”, *p1); return 0; } 6.4.2Image 1: This image shows an example of passing an object by a value in C++. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: void NameMe(string name) {this -> name = name; } void MakeSound(void) {cout<<name<<” says: no comments”<<endl; } }; void NamePetByValue(string name, Pet pet) { pet.NameMe(name); } void NamePetByPointer(string name, Pet *pet) { pet -> NameMe(name); } void NamePetByRefrence(string name, Pet &pet) { pet.NameMe(name); } int main(void) { Pet pet; Pet.NameMe(“no_Name”); NamePetByValue(“Alpha”, pet); Pet.MakeSound(); NamePetByPointer(“Beta”, &pet); Pet.MakeSound(); NamePetByRefrence(“Gamma”, Pet); Pet.MakeSound(); return 0; } 6.4.3Image 1: This image shows an example of passing an object of a subclass by reference in C++. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string name) {this -> name = name; } void MakeSound(void) {cout<<name<<” is silent :(“<<endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) {cout<<Name<<”says: Woof!”<<endl; } }; class GermanSheperd : public Dog { public: GermanSheperd(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Wuff!”<<endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Guau!”<<endl; } }; void PlayWithPet(Pet &pet) { pet.MakeSound(); } int main(void) { Pet pet(“creature”); Dog dog(“Dog”); GermanSheperd gs(“Hound”); MastinEspanol mes(“Perro”); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } 6.4.4Image 1: This image shows another example of passing an object of a subclass by pointer in C++. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string name) {this -> name = name; } void MakeSound(void) {cout<<name<<” is silent :(“<<endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) {cout<<Name<<”says: Woof!”<<endl; } }; class GermanSheperd : public Dog { public: GermanSheperd(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Wuff!”<<endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Guau!”<<endl; } }; void PlayWithPet(Pet &pet) { pet.MakeSound(); } int main(void) { Pet *pet = new Pet(“creature”); Dog *dog = new Dog(“Dog”); GermanSheperd *gs = new GermanShepherd(“Hund”); MastinEspanol *mes = new MastinEspanol(“Perro”); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } 6.4.5Image 1: This image shows an example of the dynamic_cast operator applied to a pointer in C++. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string name) : name = (name) {} void MakeSound(void) {cout<<name<<” is silent :(“<<endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) {cout<<Name<<”says: Woof!”<<endl; } }; class GermanSheperd : public Dog { public: GermanSheperd(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Wuff!”<<endl; } void Laufen(void) {cout<<name<<”runs (gs)!”<<endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Guau!”<<endl; } void Ejecutar(void) {cout<<name<<” runs (mes)!”<<endl; } }; void playWithPet(Pet *pet) { GermanSheperd *gs; MastinEspanol *mes; pet -> MakeSound(); if(gs = dynamic_cast<GermanSheperd *>(pet)) gs -> Laufen(); if(mes = dynamic_cast<MastinEspanol *>(pet)) mes -> Ejecutar(); } int main(void) { Pet *pet = new Pet(“creature”); Dog *dog = new Dog(“Dog”) GermanShepherd *gs = new GermanShepherd(“Hund”); MastinEspanol *mes = new MastinEspanol(“Perro”); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } 6.4.6Image 1: This image shows another example of the dynamic cast operator that has been applied to a reference. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string name) : name = (name) {} void MakeSound(void) {cout<<name<<” is silent :(“<<endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) {cout<<Name<<”says: Woof!”<<endl; } }; class GermanSheperd : public Dog { public: GermanSheperd(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Wuff!”<<endl; } void Laufen(void) {cout<<name<<”runs (gs)!”<<endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Guau!”<<endl; } void Ejecutar(void) {cout<<name<<” runs (mes)!”<<endl; } }; void playWithPet(Pet &pet) { pet.MakeSound(); dynamic_cast<GermanShepherd &>(pet).Laufen(); dynamic_cast<MastinEspanol &>(pet).Ejecutar(); } int main(void) { Pet pet(“creature”); Dog dog(“Dog”); GermanShepherd gs(“Hund”); MastinEspanol mes(“Perro”); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } 6.4.7Image 1: This image shows another example of the dynamic cast operator applied to a reference. The code is written out below. #include <iostream> #include <string> using namespace std; class Pet { protected: string Name; public: Pet(string name) : name = (name) {} void MakeSound(void) {cout<<name<<” is silent :(“<<endl; } }; class Dog : public Pet { public: Dog(string name) : Pet(name) {} void MakeSound(void) {cout<<Name<<”says: Woof!”<<endl; } }; class GermanSheperd : public Dog { public: GermanSheperd(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Wuff!”<<endl; } void Laufen(void) {cout<<name<<”runs (gs)!”<<endl; } }; class MastinEspanol : public Dog { public: MastinEspanol(string name) : Dog(name) {} void MakeSound(void) {cout<<Name<<”says: Guau!”<<endl; } void Ejecutar(void) {cout<<name<<” runs (mes)!”<<endl; } }; void playWithPet(Pet &pet) { pet.MakeSound(); try { dynamic_cast<GermanShepherd &>(pet).Laufen(); }catch(...) {} try { dynamic_cast<MastinEspanol &>(pet).Ejecutar(); }catch(...) {} } int main(void) { Pet pet(“creature”); Dog dog(“Dog”); GermanShepherd gs(“Hund”); MastinEspanol mes(“Perro”); PlayWithPet(pet); PlayWithPet(dog); PlayWithPet(gs); PlayWithPet(mes); return 0; } 6.5.1Image 1: This image shows an example of a copying constructor in C++. The example code has been written out below. #include <iostream> using namespace std; class Class { int data; public: Class(int value) :data(value) {} void increment(void) { data++; } int value(void) {return data;} }; int main(void) { Class o1(123); Class o2 = o1; Class o3(o2); o1.increment(); cout<<01.value()<<endl; cout<<02.value()<<endl; cout<<03.value()<<endl; return 0; } 6.5.2Image 1: This image shows another example of copying constructors. The code is written out below. #include <iostream> using namespace std; class Class { int *data; public: Class(int value) { data = new unit; *data = value; } void increment(void) {(*data)++;} int value(void) {return *data; } }; int main(void) { Class o1(123); Class o2 = o1; Class o3(o2); o1.increment(); cout<<01.value()<<endl; cout<<02.value()<<endl; cout<<03.value()<<endl; return 0; } 6.5.3Image 1: This image shows another example of a copying constructor. The code has been written out below. #include <iostream> using namespace std; class Class { int *data; public: Class(int value) { data = new int; *data = value; } void increment(void) {(*data)++;} int value(void) {return *data; } }; int main(void) { Class o1(123); Class o2(o1.value()); Class o3(o2.value()); o1.increment(); cout<<01.value()<<endl; cout<<02.value()<<endl; cout<<03.value()<<endl; return 0; } 6.5.4Image 1: This image shows another example of a copying constructor. The code has been written out below.
using namespace std; class Class { int *data; public: Class(int value) { data = new int; *data = value; } Class(Class &Source) { Data = new int; *data = source.value(); } void increment(void) { (*data)++;} int value(void) {return *data; } }; int main(void) { Class o1(123); Class o2 = o1; Class o3(o2); o1.increment(); cout<<01.value()<<endl; cout<<02.value()<<endl; cout<<03.value()<<endl; return 0; } 6.5.5Image 1: This image shows another example of a copying constructor. The code has been written out below. #include <iostream> using namespace std; class Dummy { public: Dummy(int value) {} Dummy(Dummy &source) { cout<<”Hi from the copy constructor!”<<endl; } }; void DoSomething(Dummy ob) { cout<<”i’m here!”<<endl; } int main(void) { Dummy o1(123); DoSomething(o1); return 0; } 6.5.6Image 1: This image shows another example of a copy constructor. The code is written out below. #include <iostream> using namespace std; class Dummy { private: Dummy(Dummy &source) {} Public: Dummy(int value) {} }; void DoSomething(Dummy ob) { cout<<”i’m here!”<<endl; } int main(void) { Dummy o1(123); Dummy o2 = o1; DoSomething(o1); return 0; } 6.5.7Image 1: This image shows an example of a default constructor in C++. The code has been written out below. #include <iostream> using namespace std; class NoConstructorAtAll { public: int i; float f; void Display(void) {cout<<”i=”<<i<<”,f=”<<f<<endl; } }; int main(void) { NoConstructorsAtAll o1; NoConstructorsAtAll *o2; o2 = new NoConstructorsAtAll; o1.Display(); o2 -> Display(); return 0; } 6.5.8Image 1: This image shows another example of a default constructor. The code is written out below. #include <iostream> using namespace std; class WithConstructor { public: int i; float f; WithConstructor(int a, float b) : i(a), f(b) {} void display(void) {cout<<”i=”<<i<<”,f=”<<f<<endl; } }; int main(void) { WithConstructor o1; WithConstructor o2; o2 = new WithConstructor; o1.Display(); 02 -> Display(); return 0; }; 6.5.9Image 1: This image shows another example of a default constructor. The code has been written out below. #include <iostream> using namespace std; class WithConstructor { public: int i; float f; WithConstructor(int a = 0, float b = 0) : i(a), f(b) {} void display(void) {cout<<”i=”<<i<<”,f=”<<f<<endl; } }; int main(void) { WithConstructor o1; WithConstructor *o2; o2 = new WithConstructor; o1.Display(); 02 -> Display(); return 0; }; 6.5.10Image 1: This image shows an example of compositions vs constructors in C++. The code is written out below. #include <iostream> using namespace std; class A { public: void Do(void) {cout<<”A is doing something”<<endl; } }; Class B { Public: void Do(void) {cout<<”B is doing something”<<endl; } }; Class Compo { public: A f1; B f2; }; int main(void) { Compo co; co.f1.Do(); co.f2.Do(); return 0; } 6.5.11Image 1: This image shows another example of compositions vs constructors. The code is written out below. #include <iostream> using namespace std; class A { public: A(A &src) {cout<<”copying A...”<<endl; } A(void) { } void Do(void) {cout<<”A is doing something”<<endl; } }; Class B { Public: B(B &src) {cout<<”copying B...”<<endl; } B(void) { } void Do(void) {cout<<”B is doing something”<<endl; } }; Class Compo { public: Compo(void) { }; A f1; B f2; }; int main(void) { Compo co1; Compo co2 = co1; co.f1.Do(); co.f2.Do(); return 0; } 6.5.12Image 1: This image shows another example of compositions vs constructors. The code is written out below. #include <iostream> using namespace std; class A { public: A(A &src) {cout<<”copying A...”<<endl; } A(void) { } void Do(void) {cout<<”A is doing something”<<endl; } }; Class B { Public: B(B &src) {cout<<”copying B...”<<endl; } B(void) { } void Do(void) {cout<<”B is doing something”<<endl; } }; Class Compo { public: Compo(Compo &src) {cout<<”Copying Compo...”<<endl; } Compo(void) { }; A f1; B f2; }; int main(void) { Compo co1; Compo co2 = co1; co.f1.Do(); co.f2.Do(); return 0; } 6.6.1Image 1: This image shows the C++ constant operator , otherwise known as const. This has been written out below. CONST 6.6.2Image 1: This image shows an example of constant variables and their declarations in C++. The code is written out below. const int size1 = 100; int const size2 = 100; 6.6.3Image 1: This image shows an example of constant aggregates and their declarations. The code is written out below. cons tint points[5] = {1,2,4,8,16}; const struct {int key; } data = {10}; 6.6.4Image 1: This image shows an example of constant pointers and their declarations. The code is written out below. int arr[5] = {1, 2, 4, 8, 16}; int * const iptr = arr + 2; char * const cptr = “Why?”; 6.6.5Image 1: This image shows an example of pointers to constants, and their declarations. The code is written out below. int arr[5] = {1, 2, 4, 8, 16}; cons int *iptr = arr + 2; const char *cptr = “Why?”; 6.6.6Image 1: This image shows an example of constant pointers to constants, and their declarations. The code has been written out below. int arr[5] = {1, 2, 4, 8, 16}; const int * const iptr = arr + 2; const char * const cptr = “Why?”; 6.6.7Image 1: This image shows an example of constant function parameters which have been passed by value. The example code is written out below. int fun(const int n) { return n * n; } 6.6.8Image 1: This image shows an example of constant function parameters which have been passed by reference. The code has been written out below. int fun(const int &n) { return n++; } 6.6.9Image 1: This image shows an example of constant function results, and their declarations. The code is written out below. const char *fun(void) { return “Caution!”; } 6.6.10Image 1: This image shows an example of constant class variables. The code is written out below. class Class{ private: const int field; public: Class(int n) : field(n) {}; Class(Class &c) : field(0) {}; Class(void) : field(1) {}; }; 6.6.11Image 1: This image shows an example of constant objects in C++. The code is written out below. class Class { public: int field; Class(int n) : field(n) {}; Class(Class &c) : field(0) {}; Class(void) : field(1) {}; void set(int n) {field = n;} int get(void) {return field;} }; 6.6.12Image 1: This image shows an example of constant member functions in C++. The code is written out below. class Class { public: int field; Class(int n) : field(n) {}; Class(Class &c) : field(0) {}; Class(void) : field(1) {}; void set(int n) {field = n;} int get(void) const {return field;} }; 6.7.1Image 1: This image shows a kitten and a small dog sleeping next to each other, with the kitten looking at the camera lens. The image is titled: "Friend or Foe?". 6.7.2Image 1: This image shows an example of a friend class program written in C++. The code is written out below. #include <iostream> using namespace std; class Class { friend class Friend; private: int field; void print(void) {cout<<”it’s a secret, that field = “<<field<<endl; } }; class Friend { public: void Dolt(Class &c) {c.field = 100; c.print(); } }; int main(void) { Class o; Friend f; f.Dolt(o); return 0; } 6.7.3Image 1: This image shows an example of the updated friend class program. The code is written out below. #include <iostream> using namespace std; class A { friend class B; friend class C; private: int field; protected: void print(void) { cout<<”it’s a secret, that field = “<<field<<endl; } }; class C { public: void Dolt(A &a) {a.print(); } }; Class B { public: void Dolt(A &a, C &c) {a.field = 111; c.Dolt(a); } }; int main(void) { A a; B b; C c; b.Dolt(a,c); return 0; }; 6.7.4Image 1: This image shows an example of a friend functions program. The code is written out below. #include <iostream> using namespace std; class A; class C { public: void dec(A &a); }; class A { friend class B; friend void C::dec(A&); friend void Dolt(A&); private: int field; protected: void print(void) { cout<<”it’s a secret, that field = “<<field<<endl; } }; void C::dec(A &a) {a.field--;} class B { public: void Dolt(A &a) {a.print(); } }; Void Dolt(A &a) { a.field = 99; } int main(void) { A a, B b; C c; Dolt(a); b.Dolt(a); return 0; } |