answersLogoWhite

0


Best Answer

A "get" or "getter" function is a function that retrieves information. They are called getters because they are often named with a "get_" prefix, such as:

date get_current_date ();

The "get_" prefix is not actually required in C++, it is only required in languages that do not support function overloading. This is because most get functions also have a corresponding set function and you cannot use the same name twice without overloading:

date get_current_date ();

void set_current_date (const date&);

Getters and setters are usually defined as class member functions. However, rather than referring to them informally as getters and setters we refer to them formally as accessors and mutators, respectively. This reflects the fact that a getter typically accesses information without changing it whereas a setter typically mutates or changes information. For example:

class X {

private:

int data;

public:

X (int value): data {value} {} // constructor

X& operator= (int value) { data = value; return *this; } // mutator

int value () const { return data; } // accessor

// ...

};

The accessor is declared const and returns by value. In other words, the caller receives a copy of the information, not the information itself, and the class is left unaffected by the access.

The mutator, on the other hand, is declared non-const because the class representation (the data member) will need to be modified.

Constructors are not mutators because constructors create information, they do not mutate existing information.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

The gets() functions reads input from stdin and writes it to a user-defined buffer. The function has the following declaration in <stdio.h>:

char* gets (char* str); // WARNING! Deprecated since 2011

The function is unsafe because the function won't stop writing to the buffer until a newline or end-of-file is encountered in stdin. If the buffer is too small, the function will overwrite memory beyond the bounds of the buffer.

If available, use the gets_s() function instead. This allows you to specify the length of the buffer and thus prevent buffer overruns:

char* gets_s (char* s, rsize_t n);

Alternatively use the fgets() function passing stdin as the input stream:

char* fgets (char* s, int n, FILE* stream);

POSIX 2008 also provides the getline() function as a safe alternative to gets(). However, this function resizes the buffer dynamically and must be manually released (free'd) when no longer required.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is get function in c plus plus with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the building function in c plus plus?

There is no such term as "building function" in C++.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


Example of binaray operator in c plus plus?

+ is an example, one of many, of a binary operator in C or C++ a = b + c; // for usage example


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What do you call an object function in c plus plus?

method


How do you create functions with multiple parameters in C plus plus?

Random example, function with two parameters: int main (int argc, char **argv) {...}


What are the Example Output of Calculator Program in C plus plus?

example output of c++ calculator


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


What is the function y equals ax2 plus bx plus c?

It is a quadratic function which represents a parabola.


How do you create folder with c plus plus?

Use function mkdir.