Passing Objects from one Class to another

Hi, I'm newish to C++ (9 weeks).

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}


Thanks any help appreciated.
Last edited on
> //********* problems here ******************
I'm sorry, but my mind reading cat is in another castle.
¿what's your problem?
Last edited on
Haha ok let me try again...


If it is even possible... I am trying pass an instance of class A from class A to class B, in order to let class B use class A's functions.

The reason for this is I must create a Menu class(class B). Within this class is a switch case statement that runs the class B functions.

Project requirements limit me from simply running the switch case statement in main ()
... that's still not very clear.

Do you mean something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}
Last edited on
Do you mean you want to make A a parent class ?

to make A the parent class of B you have to add ": public A" when you declare class B, something like this :

class B : public A{
...
};
Yes TwilightSpectre this looks promising, I will try to model my program on this and see if it works.

Noobprogrammer I have still not learnt inheritance, if all else fails I will research today.

thanks for the help. I will post my program when complete to make it more clear.
Do you want to pass a single Contacts object, or the whole vector:
menuClassObject.askChoice(myPhoneBook);
The entire vector..
I've made lots of progress today.

Ive figurered out how to pass objects as parameters and on the home straight now.

for anyone want to know how to do it..


in a project of multiple files, each .h file must include the others that it requires access to.

e.g. ... this example shows Edit.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef EDIT_H_INCLUDED
#define EDIT_H_INCLUDED

#include "Contacts.h"
#include "Find.h"
#include <iostream>
#include <string>
#include <vector>

class Contacts;
class Find;
class contactsClassObject;
class findClassObject;


and then in the Edit.cpp file....

 
#include 'Edit.h' 


In any function declarations include the ClassA as the argument type:

e.g....
 
void askChoice(Contacts, vector<Contacts>&, Find); //accepts the other classes object 


then in the function calls simply do this......

 
menuClassObject.askChoice(contactClassObject, myPhoneBook, findClassObject);


Topic archived. No new replies allowed.