Thread: class composition

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    class composition

    I have created a class like that:
    Code:
    class Visitor
    {
        private:
            int* a;
        public:
            Visitor()
            {
                a=new int[3];
                a[0]=rand()%4 +1;
                a[1]=rand()%10 +1;
                pl++;
                a[2]=pl;
            }
    
    
    };
    and now i want to create another class which takes pointers of type Visitor.How could i do that?And in main how could i pass a pointer of type Visitor to an object from another class?Thank you.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To your question:
    Code:
    class A {};
    class B { public: A* PointerToA;  void foo(A*) {} };
    
    int main()
    {
    	A a;
    	A* pA = new A;
    	B b;
    	b.PointerToA = &a;
    	b.PointerToA = pA;
    	b.foo(&a);
    	b.foo(pA);
    }
    You really shouldn't be using new, though. For arrays, use std::vector or std::array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Composition,private Class member variables and methods
    By sigur47 in forum C++ Programming
    Replies: 13
    Last Post: 01-29-2012, 08:10 PM
  2. help with my class-composition, i am noob
    By evildotaing in forum C++ Programming
    Replies: 7
    Last Post: 12-01-2011, 05:15 AM
  3. Composition
    By Fatima Rizwan in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2009, 09:39 AM
  4. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  5. Replies: 16
    Last Post: 11-10-2007, 03:51 PM