How do i Declare methods in public and write the code in private [closed]
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
private:
vector<int> vector_1;
void function_1(string name)
"code for function one goes here"
public:
void function_1(string name);
;
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
;
int main(void)
MapFunction a;
return 0;
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
;
void MapFunction::AskForInput()
cin >> input;
;
void MapFunction::Output()
cout << input;
;
int main(void)
MapFunction a;
a.AskForInput();
a.Output();
return 0;
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
c++ private-methods public-method
closed as unclear what you're asking by Patrick Mevzek, Aganju, Graham, Shiladitya, gnat Nov 15 '18 at 5:46
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
private:
vector<int> vector_1;
void function_1(string name)
"code for function one goes here"
public:
void function_1(string name);
;
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
;
int main(void)
MapFunction a;
return 0;
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
;
void MapFunction::AskForInput()
cin >> input;
;
void MapFunction::Output()
cout << input;
;
int main(void)
MapFunction a;
a.AskForInput();
a.Output();
return 0;
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
c++ private-methods public-method
closed as unclear what you're asking by Patrick Mevzek, Aganju, Graham, Shiladitya, gnat Nov 15 '18 at 5:46
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20
add a comment |
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
private:
vector<int> vector_1;
void function_1(string name)
"code for function one goes here"
public:
void function_1(string name);
;
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
;
int main(void)
MapFunction a;
return 0;
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
;
void MapFunction::AskForInput()
cin >> input;
;
void MapFunction::Output()
cout << input;
;
int main(void)
MapFunction a;
a.AskForInput();
a.Output();
return 0;
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
c++ private-methods public-method
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
private:
vector<int> vector_1;
void function_1(string name)
"code for function one goes here"
public:
void function_1(string name);
;
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
;
int main(void)
MapFunction a;
return 0;
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
;
void MapFunction::AskForInput()
cin >> input;
;
void MapFunction::Output()
cout << input;
;
int main(void)
MapFunction a;
a.AskForInput();
a.Output();
return 0;
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
c++ private-methods public-method
c++ private-methods public-method
edited Nov 15 '18 at 17:38
herbeir
asked Nov 15 '18 at 2:38
herbeirherbeir
11
11
closed as unclear what you're asking by Patrick Mevzek, Aganju, Graham, Shiladitya, gnat Nov 15 '18 at 5:46
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Patrick Mevzek, Aganju, Graham, Shiladitya, gnat Nov 15 '18 at 5:46
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20
add a comment |
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20
add a comment |
1 Answer
1
active
oldest
votes
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend
keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
;
void outer()
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
void outerFriend()
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend
keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
;
void outer()
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
void outerFriend()
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
add a comment |
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend
keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
;
void outer()
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
void outerFriend()
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
add a comment |
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend
keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
;
void outer()
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
void outerFriend()
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend
keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
;
void outer()
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
void outerFriend()
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
edited Nov 15 '18 at 5:54
answered Nov 15 '18 at 3:17
AconcaguaAconcagua
12.8k32144
12.8k32144
add a comment |
add a comment |
I'm voting to close this question as off-topic because Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it.
– Graham
Nov 15 '18 at 4:20