classes and members

This is a discussion on classes and members within the C++ Programming forums, part of the General Programming Boards category; im going to explain this to you all as clear as possible, forgive me if it looks like garbage.English is ...

  1. #1
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25

    classes and members

    im going to explain this to you all as clear as possible, forgive me if it looks like garbage.English is not my native langauge.

    ok, i have a class called Client. Now client has many data members (like Computer serial,color) etc. Now i want 1 client to be able to book in more than one computer (unlimited would be nice) Anyway how do i get a client to have unlimited computers booked in?? If you dont understand please just ask me.

    Thanks in advance.


    in other words to declare data memebers in a single object dinamically
    Last edited by Marlon; 08-04-2005 at 04:30 AM. Reason: to make it clearer
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    class bookings
    {
    //you fill this out
    };
    
    class client
    {
    public:
       // your funcs
       // funcs to deal with the vector of bookings
    private:
       std::vector<bookings> _mybookings;
       // rest of your members
    };
    You mean something like that?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    I'll google vectors to see what i can come up with, i dont know about vectors, maybe im in too deep
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You need a container that can dynamically grow and shrink. While Arrays ( even if dynamically allocated ) always have a static size, linked lists are the first example class that will hold a variable and varying number of entries.

    The standard library has some classes to handle this. vector and list come to mind.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'll google vectors to see what i can come up with, i dont know about vectors, maybe im in too deep
    You can use vectors just like arrays--except they have no fixed size. The basics are very easy to learn. Here's some of the basics using ints as the elements of the vector:
    Code:
    #include <iostream>
    #include <vector> /***/
    
    using namespace std;
    
    int main()
    {
    	vector<int> numbers(3); //declare a vector of ints with size 3
    	numbers[0] = 10;
    	numbers[1] = 20;
    	
    	cout<<numbers.size()<<endl; //3
    	
            numbers[2] = 30;
    
    	for(int i = 0; i<3; i++)
    	{
    		cout<<numbers[i]<<endl;
    	}
    	
    	numbers.push_back(40); //used to expand the size of the vector
    	cout<<numbers[3]<<endl;
    
    	cout<<numbers.size()<<endl; //4
    	cout<<numbers.capacity()<<endl; //6
    
    	return 0;
    }
    push_back() :
    Adds the argument to the end of the vector, expands the vector if necessary.

    size() :
    If you declare the size of a vector, size() will return that number--even if there are fewer elements. Otherwise, size() will return the actual number of elements in the vector.

    capacity():
    Internally, it is inefficient to expand a vector's size by just 1 every time, so a vector automatically expands its size to something bigger. capacity() gives you the room available before the vector will have to be resized again.
    I don't think you'll need anything more than that to write your program.
    Last edited by 7stud; 08-04-2005 at 10:47 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You don't even need to concern yourself with capacity. It is only useful for strict memory management, and can be easily confused with size. Just make sure you don't access elements beyond the size, and use push_back to add elements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  2. alignment (classes, structs, members)
    By Raven Arkadon in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2006, 06:51 AM
  3. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 05:58 PM
  4. inheritance from classes own members?
    By atapi103 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 10:19 PM
  5. Help with classes & strings members
    By GrNxxDaY in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2002, 08:29 PM

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