CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Vector of Class

  1. #1
    Join Date
    Sep 2016
    Posts
    16

    Vector of Class

    hey, this is my first post here, and dont know if it's related with the forum. if it s not the case m just redirect me without assaulting me
    Here is my problem:

    Contact.h
    Code:
    class Contact{
    	public:  
    		//Constructors
    		Contact(const string& fname, const string&  lname, const string&  phone);
    		void addContact(vector<Contact>&);
                    void setFirstName(string fname);
    		void setLastName(string lname);
    		void setPhone(string phone);
                   string getFirstName();
    		string getLastName();
    		string getPhone();
                    friend class ContactList;
    private:
    		//Variables
    		string fname;
    		string lname;
    		string phone;
    }
    ContactList.h
    Code:
    #include "Contact.h"
    #include <vector>
    
    using namespace std;
    
    class ContactList{
    
    private:
    		static vector <Contact> myList;
    }
    Contact.cpp
    Code:
    #include "ContactList.h"
    #include <iostream>
                Contact::Contact(const string& myFname, const string& myLname, const string& myPhone){
    	fname = myFname;
    	lname = myLname;
    	phone = myPhone;
    }
    void Contact::addContact(vector<Contact>&myList){
    			string fname;
    			string lname;
    			string phone;
                            cout<<"\tFirst Name :> ";
    					getline(cin,fname);
    			cout<<"\tLast Name :> ";
    					getline(cin,lname);
    			cout<<"\tPhone # :> ";
    					getline(cin,phone);
                            Contact newContact(fname, lname, phone, address, city, state);
    			myList.push_back(newContact);
    }
    UseContact.cpp
    Code:
    #include "ContactList.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(){	
    	vector<Contact>v;
           ContactList myContact1(fname, lname, phone);
           cout<<"\t\t----- Add Contact -----"<<endl;	
    		myContact1.addContact(v);
    }
    I don't know how to make it work, Which header should i call.
    Contact.h is include in ContactList.h
    However, i Wasn't;t able to determine how to list all my contacts.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Vector of Class

    What is the purpose of class ContactList - as it only contains one private static member with no constructors etc? What are you trying to achieve? Is this homework - if so what is the assignment?


    Does addContact() belong in class Contact or should it be in class ContactList and take a parameter of Contact which then adds to the myList vector? Why is myList static?
    Last edited by 2kaud; September 27th, 2016 at 03:10 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2016
    Posts
    16

    Re: Vector of Class

    Hi,
    thank you for answering.
    I have to implement a new Header that contains a vector of Contact.
    The vector ContactList should contain all my contacts.
    I don't know if addContact should be in the ContactList class or Contact class. I m not sure which one is more effective.
    I implemented that function in Contact class, and it is not recognized.

  4. #4
    Join Date
    Sep 2016
    Posts
    16

    Re: Vector of Class

    i have a constructor for the ContactList class. i implemented it as public:
    ContactList(Contact);

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Vector of Class

    The vector ContactList should contain all my contacts.
    I don't know if addContact should be in the ContactList class or Contact class. I m not sure which one is more effective.
    I would consider that addContact goes in contactList. I don't know what your exercise asks, but consider
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    class Contact {
    public:
    	Contact(const string& cfname, const string&  clname, const string&  cphone) : fname(cfname), lname(clname), phone(cphone) {};
    	Contact() {};
    
    	friend ostream& operator<<(ostream& os, const Contact& con);
    
    private:
    	string fname;
    	string lname;
    	string phone;
    };
    
    ostream& operator<<(ostream& os, const Contact& con)
    {
    	return os << con.fname << " " << con.lname << " " << con.phone;
    }
    
    
    class ContactList {
    public:
    	void addContact(const Contact& cont) {
    		conList.push_back(cont);
    	}
    
    	void addContact(const string& fname, const string& lname, const string& phone) {
    		conList.emplace_back(fname, lname, phone);
    	}
    
    	void display() const
    	{
    		for (const auto& c : conList)
    			cout << c << endl;
    	}
    
    private:
    	vector<Contact> conList;
    };
    
    int main()
    {
    	ContactList conlst;
    	string ans;
    
    	do {
    		string fname, lname, phone;
    
    		cout << "first name: ";
    		cin >> fname;
    		cout << "last name: ";
    		cin >> lname;
    		cout << "phone: ";
    		cin >> phone;
    		conlst.addContact(fname, lname, phone);
    
    		cout << "Add another contact ? (Y/N) ";
    		cin >> ans;
    
    	} while (ans == "Y" || ans == "y");
    
    	cout << endl;
    	conlst.display();
    }
    which is a 'bare bones' contact list based around a vector.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2016
    Posts
    1

    Re: Vector of Class

    what are you trying to find

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Vector of Class

    Quote Originally Posted by saraedward123 View Post
    what are you trying to find
    The answer to life, the universe and everything.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured