OOP beginer help

This is a discussion on OOP beginer help within the C++ Programming forums, part of the General Programming Boards category; hi , i've got a liitle bit of a beginer's problem i have a class dog which i want it ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    OOP beginer help

    hi ,

    i've got a liitle bit of a beginer's problem

    i have a class dog which i want it to inherit one of the three kinds of dogs either rescue or boom or drug
    tried
    class dog() public boom , public drug public rescue

    but it creates all three kinds
    how can i have a dog class which will be able to inherit the three of them but create only on choice ?
    thank you in advance .

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    You are inheriting the methods and variables from the Base class, and thus inherit all methods declared in those classes into the class dog.

    Let me guess you have this:

    Class Rescue

    Class Boom

    Class Drug

    Class Dog : public Rescue

    A better way to do this is create a Base class called Dog. Let the Boom, Drug, and Rescue classes be derived from this so they inherit generic function/variables from Dog. So you have something like this:

    Code:
    class Dog {
        //some methods/variables
    };
    Code:
    class Rescue : public Dog
    {
    };
    
    class Boom : public Dog
    {
    };
    
    class Drug : public Dog
    {
    };
    Then implement the necessary function in the derived classes.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12

    did not understand

    hi
    thanks for the quick reply
    i didn't understand the diffrence

    class dog () : public boom , public rescue , public drug
    {
    //
    }

    is what i have now , i want to be able to inherit one of the other
    classes according to my prefences , the minute a dog is created he constructs all three other classes
    how can i avoid this ?

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    hm, ok can u post your code, im not sure what you mean to do.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    12

    code...

    class Dog : public Boom,public Drug,public Rescue
    {
    protected:
    string name;


    public:
    Dog(string nname);
    virtual ~Dog();
    void print();


    };


    class Boom
    {
    public:
    Boom(){cout <<"is boom"<<endl;};
    virtual ~Boom();

    };


    class Drug
    {
    public:
    Drug(){cout <<"is drug"<<endl;};
    virtual ~Drug();

    };



    class Rescue
    {
    public:
    Rescue(){cout <<"is rescue"<<endl;};
    virtual ~Rescue();

    };


    in the main i have

    Dog** Dogarr;


    it should hold the dogs in the pound either drug...
    but every dog holds one kind

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It sounds like ventolin's code was what you want.

    A Rescue is a dog (I'm assuming it's a rescue dog - consider changing the name to RescueDog to reflect that). A Drug is a dog, but a Drug is not a Rescue. Is that right? If so, then alter your code to match ventolin's:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Dog {
    protected:
      string name;
    
    public:
      Dog(string nname) { name = nname; }
      virtual ~Dog() { }
      virtual void print() {cout << name << endl;}
    };
    
    class Rescue : public Dog
    {
    public:
      Rescue(string nname) : Dog(nname) { }
      virtual ~Rescue() { }
      virtual void print() {cout << name << " is rescue" << endl;}
    };
    
    class Boom : public Dog
    {
    public:
      Boom(string nname) : Dog(nname) { };
      virtual ~Boom() { }
      virtual void print() {cout << name << " is boom" << endl;}
    };
    
    class Drug : public Dog
    {
    public:
      Drug(string nname) : Dog(nname) { }
      virtual ~Drug() { }
      virtual void print() {cout << name << " is drug" << endl;}
    };
    
    int main()
    {
      Dog** dogarr = new Dog*[3];
      dogarr[0] = new Rescue("Fido");
      dogarr[1] = new Boom("Spot");
      dogarr[2] = new Drug("Benji");
    
      for (int j = 0; j < 3; j++)
        dogarr[j]->print();
    
      delete dogarr[0];
      delete dogarr[1];
      delete dogarr[2];
      delete [] dogarr;
    }
    On the other hand, if you want your dog to contain a Rescue, Boom, and Drug, then you'd want a different setup. Try running the program above and understanding why it works if it's what you want.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    12

    thanks

    now i understand i tried it the other way around ...
    class dog : public ....

    anyways i've ran into a diffrent problem now ,
    i have in the main the dogarr**
    now the max it can hold it 10 , now after deleteing let's say number 3 when i have 8 in the arr
    how can i push the remaining 5 back ... ?

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    12

    solve it but ...

    ok i've added a string type and a gettype{return type}
    so i'll know what to copy
    but is there a more elegant way of solving it without adding a variable ?
    i mean can i know what type of dog is it without the added variable ?

    thanks in advance

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>now the max it can hold it 10 , now after deleteing let's say number 3 when i have 8 in the arr
    how can i push the remaining 5 back ... ?

    To shift elements in array to left by one you can do something like this.
    for( i = elementToErase; i < #ofElementInArray; ++i)
    array[i] = array[i+1];

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    12

    i'm not that much of a novice ;)

    That is quite elementary Jbro , but my problem with psushing back is knowing whcih class to associate the
    dogarr[i] = new TYPE for the [i] = [i+1]
    that's why I added the string type , nut is there a more elgant way to do it like sizeof() for int and double ...

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    download jlou's code. Change the code after the for loop in main to look like this:

    Code:
    delete dogarray[i];
     
    dogarray[1] = dogarray[2];
     
    for(i = 0; i < 2; ++i)
      dogarray[i]->print();
     
    delete dogarray[0];
    delete dogarray[1];
    delete [] dogarray;
     
    return 0;
    The point I'm trying to make is that you don't need to know what whether dogarray[i] points to a drug or a rescue or a boom to get the code to work. You can delete them, move them, etc. without knowing. That's part of the power (and mystique) of polymorphism/pointers/OOP.

    Now, if you have some reason to know whether dogarray[i] is pointing to a rescue or a boom or a drug I believe you can use RTTI (Runtime Type Identification).

    The reference I have on RTTI indicates that you can mimic RTTI using an enumerated type in each of the classes and then test that type at runtime--which sounds similar to your use of a string variable and then testing that string at runtime. My reference indicates that having to use RTTI may be an indication of poor design and suggests looking into use of virtual functions, templates, or other types of inheritance to see if there is some other mechanism to do what you want. Furthermore my reference indicates that you need to cast down from the base class pointer to the derived class using the concept of dynamic_cast.

    If this all sounds like it may be applicable to your case, then I suggest looking in your references for RTTI and dynamic_cast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 03:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 11:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 05:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21