how to call a function from another class

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
#include <iostream>
#include <string>

using namespace std;


class Main{
public:
	Main();
	void Displaydetail(){ cout << "This is main class ! " << endl;}
};


class Second {
public:
        Main themain;
	void Display(){ 
		cout << "This is the second class ! " << endl;
                cout << "themain.Displaydetail() " << endl;
	}

	
};


int main(){

	Second theSecond;
	Main theMain;
        theSecond.Display();

	system( "pause" );
	return 0;
}


what wrong with my code TT
You need to actually implement your constructors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Main{
public:
	Main(){}
	void Displaydetail(){ cout << "This is main class ! " << endl;}
};


class Second {
public:
	Second(){}
	void Display(){ 
		cout << "This is the second class ! " << endl;
                cout << "themain.Displaydetail() " << endl;
	}

	
};
implement what?
can provide for me c?
i just wan to display . and test my program
One way of doing it would be 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
#include <iostream>
using namespace std;

class Main{
public:
	Main(){}
	void Displaydetail(){ 
              cout << "This is main class ! " << endl;
        }
};


class Second {
private:
	Main themain;
public:
	Second(Main &theMain)
	{
		themain = theMain;
	}
	void Display(){ 
		cout << "This is the second class ! " << endl;
        themain.Displaydetail();
	}

	
};

int main(){

	Main theMain;
	Second theSecond(theMain);
    theSecond.Display();
	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//first class with the function
class_A
{
    public:
         void function_A();
}

//second class where you want to use the function
class_B
{
    class_A A;  //create an object of the first class
     A.function_A();  //call the funtion "function_A()" of that object
}



wont compile, but should give you an idea
Topic archived. No new replies allowed.