question abot classes

This is a discussion on question abot classes within the C++ Programming forums, part of the General Programming Boards category; Hi, i am starting to play around classes and oop and i have a very basic question that i cannot ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    question abot classes

    Hi,

    i am starting to play around classes and oop and i have a very basic question that i cannot grasp. so the situation is:

    I have a class X and several functions operating on it (are a part of that class). Now, as far as i understood, i can pass a function to a class by saying X::func() but now this function is a part of my class X and if i need to pass it down to another class i need to make this other class a part of my X class, is that right ? If yes then how would one share a function between two or more different unrelated classes ?

    thnx

    baxy


    PS.

    did i completely misunderstand the whole thing ??

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    You are confused about the difference between 'has a' and 'is a' relationships between classes.
    For example, you have a class called Animal and another called Human.
    A human is an animal.
    So, you derive human from animal and implement/override necessary member functions.
    This is called inheritance.

    You can also have composition, in which a object of type..say..Brain may literraly be a member of a human object.

    You can have 'nested' classes, which for which I can not find a good analogy here....
    (Humans do not have iterators )
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    ok so if i have a method that formats some private data structures , let say a simple array . and i want to use the same function on a compleatly unrelated class to do the same thing on a private array in that class how would i do this. the rreason why i am aking this question is because to me oop right now looks so un-flexible in comparison to simple procedural design (nicer and more efficient but extreamly un-flxible ). so is something like that possible and how would i do it? example:

    Code:
     
    class X{
          Constr(int i=0 )throw( ...bla...);
          ~Constr()throw(...bla...);
    
         private:  
         int *array; 
          
    
    };
    
    
    
    class Y{
          Constr(int i=0 )throw( ...bla...);
         ~Constr()throw(...bla...);
    
    private: 
          int *array;
          
    
    };
    
    modif(){
      // it modifies arrays
    }
    how would i make the function modif work on both arrays in classes X and Y ?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by baxy View Post
    ok so if i have a method that formats some private data structures , let say a simple array . and i want to use the same function on a compleatly unrelated class to do the same thing on a private array in that class how would i do this.
    It sounds like what you want is a superclass. Then you can put the similar function into the superclass or if it changes based on the subclasses, let them each have their own version. Going on your example here is one that should give some ideas

    Code:
    class Sup {
    protected:
        static const int lim_ = 10;
        int array_[lim_];
    public:
        virtual void modif() = 0;
        void fill(int i) {
            for (int j=0; j < lim_; j++) {
                array_[j] = i;
            }
        }
        
        friend std::ostream & ::print(std::ostream &, const Sup &);
    };
    
    std::ostream &print(std::ostream &os, const Sup &s)
    {
        using std::cout;
        cout << s.array_[0];
        for (int i = 1; i < s.lim_; i++) {
            cout << " " << s.array_[i];
        }
        return os;
    }
            
    class X : public Sup {
    private:
        const int modifchar_ = 42;
    public:
        X(int i=0) {
            fill(i);
        }
        ~X() { }
    
        virtual void modif() {
            for (int i=0; i < lim_; i++) {
                array_[i] = modifchar_;
            }
        }
    };
    
    class Y : public Sup {
    private:
        const int modifchar_ = 97;
    public:
        Y(int i=0) {
            fill(i);
        }
        ~Y() { }
    
        virtual void modif() {
            for (int i=0; i < lim_; i++) {
                array_[i] = modifchar_;
            }
        }
    };
    Demonstration using the classes

    Code:
    int main()
    
    {
    
        X x;
        Y y;
        x.fill(10);
        y.fill(20);
        print(cout, x) << endl;
        print(cout, y) << endl;
        x.modif();
        y.modif();
        print(cout, x) << endl;
        print(cout, y) << endl;    
        return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes Question.
    By Xam Enyap in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2013, 10:28 PM
  2. Question about classes, and which way is best
    By Swarvy in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 03:31 PM
  3. Abot Arrary Sorting
    By FeNCinGeR in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2006, 09:39 AM
  4. Question on use of classes
    By Flyer in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2003, 09:23 AM
  5. Question on Classes
    By phil001 in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2002, 11:01 AM

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