For a project I've been tasked a phone book, using individual classes.
class Contacts
{//contains the data i.e num and name};
class Menu
{//lets the user make a selection i.e add contact, edit contact etc};
In main() I'm trying to send and instance of Contacts over to Menu, to allow the menu to use Contacts functions independent of main().
Why?? I'm trying to keep main tidy and simply call the menu function once.
int main()
{
//creating a vector of type contacts(which is a Class) named myPhoneBook.
vector<Contacts> myPhoneBook;
//instances of each class
Contacts contactClassObject; //using default constructor
Menu menuClassObject; //using default constructor
//function calls
//********* problems here ******************
//I'm trying to pass an object from one class to another
//so that menu class can use contacts class functions
menuClassObject.askChoice(contactClassObject); //calls menu funct.
//***********Any help welcome, Thanks**********
return (0);
}
class A {
public:
// functions you want to call
int getVal() const;
};
class B {
public:
void setA(A a) { m_a = a; }
void doSomething() {
switch(m_a.getVal()) {
// ...
}
}
private:
A m_a;
// or
public:
void doSomething(A a) {
switch(a.getVal()) {
// ...
}
}
};
int main() {
A a;
B b;
b.setA(a);
b.doSomething();
// or
b.doSomething(a);
return 0;
}