set function examples in c++

set function examples in c++

Page 1 of 1

4 Replies - 16204 Views - Last Post: 19 March 2009 - 10:04 AM Rate Topic: -----

#1 Rohitvarne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 09

set function examples in c++

Posted 19 March 2009 - 03:39 AM

set function examples in c++
Is This A Good Question/Topic? 0
  • +

Replies To: set function examples in c++

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: set function examples in c++

Posted 19 March 2009 - 04:18 AM

class sampleClass 
{
  bool isItClass;
public:
  sampleClass ()
  {
	isItClass = true;
  }

  // Get function, gives read access to isItClass
  bool IsThisAClass() const { return isItClass; }

  // Set function, gives write access to isItClass
  void SetClassStatus(bool value) { isItClass = value; }
};

int main()
{
  sampleClass cl;

  //It is a class
  std::cout << std::boolalpha << cl.IsThisAClass() << '\n';

  cl.SetClassStatus(false);

  // maybe not...
  std::cout << std::boolalpha <<  cl.IsThisAClass() << '\n';
}
 


I hope this example will help you to understand that better. :)
Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: set function examples in c++

Posted 19 March 2009 - 09:17 AM

Here is another one:
class object {
	private:
		int x, y;

	public:
		object(): x(0), y(0) {}
		object( int X, int Y ) {
			x = X;
			y = Y;
		}

		void set( const int& X, const int& Y ) {
			x = X;
			y = Y;
		}
};


int main() {

	object a; //a.x and a.y hold the value 0
	object b(100, 17); //b.x is 100 and b.y is 17

	//Set object a's x and y values to a different one
	a.set( 6, 9 ); //x=6 and y=9

	return 0;
}



Having a mutator (such as the "set" function) allows you to hide the implementation. This results to encapsulation, which is very important for best programming practices.

I hope this post was helpful.

This post has been edited by e_barroga: 19 March 2009 - 09:25 AM

Was This Post Helpful? 1

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: set function examples in c++

Posted 19 March 2009 - 09:43 AM

"Acessor methods" (method aka function aka attribute)
Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: set function examples in c++

Posted 19 March 2009 - 10:04 AM

View PostHyper, on 19 Mar, 2009 - 08:43 AM, said:

"Acessor methods" (method aka function aka attribute)


Accesors "get" data.
Mutators "mutate" data (modify).

Mutator:
foo.set(99);



Accessor:
cout << foo.get() << endl;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1