Wondering if I can use memeber functions without object calling it.

hi,
Quick question is it possible to active a member function without referring to a object. Instead, I want to refer to the object via a reference parameter to change its private members. My main concern is calling the set_combination, i would like to to use the method to change the combination to my lock.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// File:Lock.h
// apart of the namespace jor_tad
//
// CONSTRUCTORS
// lock()
// Precondition: this constructor is called when you do not provide arguments.
// Postcondtion: this constructor initializes sets your combination to (0,0,0).
//
//
//
// MODIFICATION MEMBER FUNCTIONS
// void set_combination(lock& l)
// Precondition: an object of lock type is an argument.
// Postcondition: the user is asked wether he would like to change the //combination if yes then the combination of the lock has been changed.
//
//
// void set_x(int a_x)
// precondition: None
// Postcondition: the x variable is now changed
//
// void set_y(int b_y)
// precondition: None.
// postcondition:
//
// void set_z(int c_z)
// precondtion: None.
// Postcondition: z has been changed
//
// Value semantics
// you can copy object variables to one another and use the copy constructor.

#ifndef Jor_Tad
#define Jor_Tad
namespace jor_tad
{
	class lock
	{
	public:
		//CONSTRUCTORS
		lock(){ x = 0; y = 0; z = 0; }

		// MODIFICATION MEMBER FUNCTIONS
		void set_combination(lock& Refobject);
		//void set_combination(int new_x, int new_y, int new_z);
		void turn_knob(lock& l);
		void close_lock(lock& l);
		void open_lock(lock& l);
		void set_x();
		void set_y();
		void set_z();

		// CONSTANT MEMBER FUNCTIONS
		void check_lock(const lock& l);
		void current_number(const lock& l);

	private:
		int x, y, z;
	};
}
#endif 



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 //File: Lock.cpp
// if you want to learn how to use this program look at the header file.
//
//
#include "Lock1.3.h"
#include <iostream>
namespace jor_tad
{
	//MODIFICATION MEMBER FUNCTIONS
	void lock::set_combination(lock& Refobject)
	{
		std::cout << "You have a lock with a rotating knob with numbers 0-39 on its edge." << std::endl;
		std::cout << "Your lock by default has been set to (0,0,0)." << std::endl;
		std::cout << "Would you like to set the combination to your lock?" << std::endl;
		std::cout << "If you would like to change the combination press 1 if not press any number" << std::endl;
		int user_input;
		std::cin >> user_input;
		if (user_input == 1)
		{
			std::cout << "What would you like your new combination to be?" << std::endl;
			std::cout << "Please enter your first number" << std::endl;
			int user_input_x;
			std::cin >> user_input_x;
			Refobject.set_x(user_input_x);
			std::cout << "Please enter your second number" << std::endl;
			int user_input_y;
			std::cin >> user_input_y;
			Refobject.set_y(user_input_y);
			std::cout << "Please enter your third number" << std::endl;
			int user_input_z;
			std::cin >> user_input_z;
			Refobject.set_z(user_input_z);
		}
		else
		{
			std::cout << "You have chosen to not change your lock" << std::endl;
		}
		
		void lock::set_x(int a_x)
		{
			x = a_x;
		}

		void lock::set_y(int b_y)
		{
			y = a_y;
		}

		void lock::set_z(int a_z)
		{
			z = a_z;
		}

}




1
2
3
4
5
6
7
8
9
10
#include "Lock1.3.h"
#include <iostream>
using namespace std;
using namespace jor_tad;

int main()
{
	lock a;
	set_combination(a);
}

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Example program
#include <iostream>
#include <string>

	class lock
	{
	public:
		//CONSTRUCTORS
		lock(){ x = 0; y = 0; z = 0; }

		// MODIFICATION MEMBER FUNCTIONS
		void set_combination(lock& Refobject)
		{
		std::cout << "You have a lock with a rotating knob with numbers 0-39 on its edge." << std::endl;
		std::cout << "Your lock by default has been set to (0,0,0)." << std::endl;
		std::cout << "Would you like to set the combination to your lock?" << std::endl;
		std::cout << "If you would like to change the combination press 1 if not press any number" << std::endl;
		int user_input;
		std::cin >> user_input;
		if (user_input == 1)
		{
			std::cout << "What would you like your new combination to be?" << std::endl;
			std::cout << "Please enter your first number" << std::endl;
			int user_input_x;
			std::cin >> user_input_x;
			Refobject.set_x(user_input_x);
			std::cout << "Please enter your second number" << std::endl;
			int user_input_y;
			std::cin >> user_input_y;
			Refobject.set_y(user_input_y);
			std::cout << "Please enter your third number" << std::endl;
			int user_input_z;
			std::cin >> user_input_z;
			Refobject.set_z(user_input_z);
		}
		else
		{
			std::cout << "You have chosen to not change your lock" << std::endl;
		}
		}
		void set_x(int a_x) {x= a_x;}
		void set_y(int b_y) {y= b_y;}
		void set_z(int c_z) {z= c_z;}

		// CONSTANT MEMBER FUNCTIONS
		void check_lock(const lock& l);
		void current_number(const lock& l);

	private:
		int x, y, z;
	};




int main()
{
  lock a;
  set_combination(a);
}

Last edited on
I believe you can call member function via reference but only if the object has already been constructed or "instantiated".
well, in my main program the object has been created but i cannot call the set_combination function.
Quick question is it possible to active a member function without referring to a object.

Yes, if it is a static member function.
Last edited on
Design.... if you can (and want to) call it without an object, maybe it should not be a member but a stand alone function?



Like a friend?
friends work across objects. So object of type a could call a function from type b. It won't let you call the function cold in main with no objects defined. Maybe you can explain better exactly what you need to do?
Ok, I want to be able to call the set_combination method and have the user manually type in the combination for that object's private members (x,y,z).
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
#include <iostream>

namespace jor_tad
{
	class lock
	{
        public:
            lock( int x = 0, int y = 0, int z = 0 ) : x(x), y(y), z(z) {}

            // ...

        private:
            int x, y, z;
	};

	void set_combination( lock& lk ) // non-member function
	{
	    int x, y, z ;
	    std::cout << "please enter your three numbers:\n" ;
	    std::cin >> x >> y >> z ;

	    lk = { x, y, z } ; // x, y and z:  C+11 
            // lk = lock(x,y,z) ; // set x, y ans z legacy C++
	}
}

int main()
{
    jor_tad::lock lk ;
    set_combination(lk) ; // ADL (argument dependant lookup aka koenig lookup)
                          // finds jor_tad::set_combination
}
No friendship nor membership is required in your code:
1
2
3
4
5
6
7
8
9
int main() {
  lock Refobject;

  int user_input_x;
  std::cin >> user_input_x;
  Refobject.set_x( user_input_x ); // valid

  return 0;
}

The set_x() member of your lock is public and therefore anyone can call it.


JLBorges does not use the set_x(), set_y() and set_z(), but a constructor that too is a public member, accessible to everyone.
Topic archived. No new replies allowed.