Search Results

Search found 29 results on 2 pages for 'wrongusername'.

Page 2/2 | < Previous Page | 1 2 

  • Getting functions of inherited functions to be called

    - by wrongusername
    Let's say I have a base class Animal from which a class Cow inherits, and a Barn class containing an Animal vector, and let's say the Animal class has a virtual function scream(), which Cow overrides. With the following code: Animal.h #ifndef _ANIMAL_H #define _ANIMAL_H #include <iostream> using namespace std; class Animal { public: Animal() {}; virtual void scream() {cout << "aaaAAAAAAAAAAGHHHHHHHHHH!!! ahhh..." << endl;} }; #endif /* _ANIMAL_H */ Cow.h #ifndef _COW_H #define _COW_H #include "Animal.h" class Cow: public Animal { public: Cow() {} void scream() {cout << "MOOooooOOOOOOOO!!!" << endl;} }; #endif /* _COW_H */ Barn.h #ifndef _BARN_H #define _BARN_H #include "Animal.h" #include <vector> class Barn { std::vector<Animal> animals; public: Barn() {} void insertAnimal(Animal animal) {animals.push_back(animal);} void tortureAnimals() { for(int a = 0; a < animals.size(); a++) animals[a].scream(); } }; #endif /* _BARN_H */ and finally main.cpp #include <stdlib.h> #include "Barn.h" #include "Cow.h" #include "Chicken.h" /* * */ int main(int argc, char** argv) { Barn barn; barn.insertAnimal(Cow()); barn.tortureAnimals(); return (EXIT_SUCCESS); } I get this output: aaaAAAAAAAAAAGHHHHHHHHHH!!! ahhh... How should I code this to get MOOooooOOOOOOOO!!! (and whatever other classes inheriting Animal wants scream() to be) instead?

    Read the article

  • LinkedList insert tied to inserted object

    - by wrongusername
    I have code that looks like this: public class Polynomial { List<Term> term = new LinkedList<Term>(); and it seems that whenever I do something like term.add(anotherTerm), with anotherTerm being... another Term object, it seems anotherTerm is referencing the same thing as what I've just inserted into term so that whenever I try to change anotherTerm, term.get(2) (let's say) get's changed too. How can I prevent this from happening?

    Read the article

  • Consistency in placing operator functions

    - by wrongusername
    I have a class like this: class A { ...private functions, variables, etc... public: ...some public functions and variables... A operator * (double); A operator / (double); A operator * (A); ...and lots of other operators } However, I want to also be able to do stuff like 2 * A instead of only being allowed to do A * 2, and so I would need functions like these outside of the class: A operator * (double, A); A operator / (double, A); ...etc... Should I put all these operators outside of the class for consistency, or should I keep half inside and half outside?

    Read the article

  • Displaying text in correct column

    - by wrongusername
    After getting a helpful answer here, I have run into yet another problem: displaying two or more strings in the column I want it to be displayed in. For an example of the problem I have, I want this output: Come here! where? not here! but instead get Come here! where? not here! when I use the code cout << left << setw(30) << "Come here!" << " where? " << setw(20) << "not here!" << endl; I made sure (I think) that the width of both columns could contain the two strings, but no matter how large I set the width of the columns to be, the error is still there.

    Read the article

< Previous Page | 1 2