Thread: Setting up a Class to hold Objects with multiple attributes

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Setting up a Class to hold Objects with multiple attributes

    Hello, I am a C++ newbie. I am working on a project to create a phone book. The entries of the phone book have to be set up as objects of a class. Each object will have three attributes - name, number, and a note. I am having a really hard time understanding the nature of a class in C++. The attached code is how I have set up my class and includes comments to explain what I hope to accomplish in each line of code. I am getting error messages and don't understand why. Can you help?

    Code:
    string x, y, z;  // Global variables
    
    
    // Class
    
    
    class contactList
    {
    	public:
    		contactList();  //Establish a generic object.
    
    
    		contactList(string name[100], string number[100], string note[100]);
    			string name[i] = "";    // Automatically set up the name field to empty.
    			string number[i] = "";  //  Same for number field
    			string note[i] = "";    // Same for note field - Note is an option field, so some
    					                // entries may not have note data.
    
    
    		// These functions will allow the main program to change the variables
    		void setName(string x)  // When called, this function will take a string x that is
    		{						// provided in one of the functions and assign to name field
    			name[i] = x;		// of the current entry.
    		}
    		void setNumber(string y)	// Same as above, but for the number field.
    		{
    			number[i] = y;
    		}
    		void setNote(string z)		// Same as above, but for the note field.
    		{
    			note[i] = z;
    		}
    		// These functions will allow us to retrieve the name of a variable
    		string getName()	// When called, this function will retrieve the name of the
    		{					// current entry for display.
    			return name[i];
    		}
    		string getNumber()	// Same as above, but for the number field.
    		{
    			return number[i];
    		}
    		string getNote()	// Same as above, but for the note field.
    		{
    			return note[i];
    		}
    
    
    
    
    	private:                        //Placing the variables here will make them more secure.
    		string name[], number[], note[];
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, the idea is to first define a class to model a single contact, e.g.,
    Code:
    class Contact
    {
    public:
        // ...
    private:
        std::string name;
        std::string number;
        std::string note;
    };
    Then you define a class to model a list of contacts, e.g.,
    Code:
    class ContactList
    {
    public:
        // ...
    private:
        std::vector<Contact> contacts;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    There are several grammatical issues. One thing is that the second constructor should use
    Code:
    {}
    around its definition, just like any other function. Also you have to define the size of arrays at design time; neither of these will work:
    Code:
    int x[];
    Code:
    int x[i];
    Only something like this will:

    Code:
    int x[5];
    You also can't pass or return arrays - only pointers to them. Like this:

    Code:
    int x[2] = { 1, 2 };
    switch(x);
    Code:
    void switch(int* x)
    {
         int temp = *x;
         *x = *(x + 1);
         *(x + 1) = temp;
    }
    The function body used some syntax that I don't remember clearly anymore, but the function declaration and its call should be correct. But even so, go with what laserlight said.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Thank you laserlight, jsrig88. My course instructor tried to help me, but I am still having a problem. In the code below, I am trying to set up a first class for telephone book entries and a second class for the phone book. The phone book will fill with entries. This arrangement is what my instructor advised. I am still getting an error in the 7th line (name = name1).

    Can you help me with two things? First, do you understand why I am getting the error? Second, does this code look right to you? I am pasting in code that sets up the entry class, the phone book class, and the public function within the phone book class that reads data from a text file and creates one hundred entries in a phone book. My instructor is extremely nice, but I just don't understand how this code is going to accomplish what I want it to.


    Code:
    class entry {private:
    	string name1, number1, note1;
    public:
    	entry();
    	entry(string name, string number, string note){
    		name = name1;
    		number = number1;
    		note = note1;
    	}
    	void setName(string x){name1 = x;}
    	void setNumber(string y){number1 = y;}
    	void setNote(string z){note1 = z;}
    	string getName()   {return name1;}
    	string getNumber() {return number1;}
    	string getNote()   {return note1;}
    };
    class phoneBook {
    	int i, phoneBookCheck, num_entries;
    	public:
    		entry entryItem[100];
    	void readPhoneBook(){
    		phoneBookCheck = 0;
    		fstream fin;
    		fin.open("entries.txt");
    		if (fin.fail()){
    			cout << "Could not find an existing Phone book." << endl;
    			phoneBookCheck = 1;
    			return;}
    		for (i = 0; !fin.eof(); i++){
    			fin >> currentname;
    			entryItem[i].setName(currentname);
    			fin >> currentnumber;
    			entryItem[i].setNumber(currentnumber);
    			fin >> currentnote;
    			entryItem[i].setNote(currentnote);}
    		num_entries = i - 1;
    		}

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Classes 101.

    Code:
    class entry {private:
    	string name1, number1, note1;
    public:
    	entry();
    	entry(string name, string number, string note){
    		name = name1;
    		number = number1;
    		note = note1;
    	}
    Your member variables are the (poorly-named) name1, number1, and note1. Your constructor's argument names are name, number, and note. Does it make any sense in your constructor to be writing the values of your uninitialized member variables to the variables being passed into the constructor?

    Code:
            for (i = 0; !fin.eof(); i++){
    Don't use eof() in reading files. See the FAQ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  2. setting file attributes through a C program
    By rohan_ak1 in forum C Programming
    Replies: 1
    Last Post: 05-23-2008, 08:39 AM
  3. Attributes as objects
    By drrcknlsn in forum C++ Programming
    Replies: 15
    Last Post: 01-15-2008, 08:25 PM
  4. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  5. Setting File Attributes
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 11:54 PM

Tags for this Thread