-2

I want to output the values of the private class members Bankcode and AgentName. How can I do this from within my main() function, or in general, outside of the BOURNE class.

My initial code attempts are below:

#include <iostream>
#include <string>
using namespace std;

class BOURNE
{
    string Bankcode ={"THE SECRET CODE IS 00071712014"} ; /*private by default*/

    string AgentName={"Jason Bourne"};                   /*private by default*/
public:    
    void tryToGetSecretCodeandName(string theName ,string theCode);    //trying to get the private   

    void trytoGetAgentName( string name); // try to get name
};
//***********************defining member function**************************************

void BOURNE::tryToGetSecretCodeandName(string theName, string theCode)  //member defining function
{
   Bankcode=theCode;    //equalling name to the code here 

   AgentName=theName;   //the samething here

   cout<<theCode<<"\n"<<theName; //printing out the values
}
//************************main function*****************************
int main()
{
   BOURNE justAnyObject;  //making an object to the class
   justAnyObject.tryToGetSecretCodeandName();
   return 0;    
}
chrisb2244
  • 2,940
  • 22
  • 44
babylon
  • 21
  • 10
  • What's the question? You can set the values this way, but it won't tell you what they initially were – chrisb2244 Oct 08 '14 at 17:12
  • I don't understand this. Nothing in your code attempts to retrieve the values of the private members. – Neil Kirk Oct 08 '14 at 17:15
  • look, all I need is this line here cout< – babylon Oct 08 '14 at 17:45
  • Can you perhaps elaborate? Maybe show the output you get, and what you're hoping to get? Along with a comparison to `int`, if that behaves differently – chrisb2244 Oct 08 '14 at 17:50
  • it works for integers but all I need is to get this output THE SECRET CODE IS 00071712014 Jason Bourne but the problem is it will not compile not in code blocks and neither in visual studio 2013 , strange , now I got stuck here – babylon Oct 08 '14 at 17:54

1 Answers1

1

Third Answer

Your code has two 'getter' style functions, but neither one takes no arguments. That is, both of your functions require arguments to be passed.

Your main function is calling get...CodeandName(), which has no arguments. As such, you get a compiler error, probably complaining about valid signatures, or arguments passed.

Edited Answer If you only want to get the values, the typical (as far as I am aware) implementation is something like

std::string BOURNE::getCode()
{
    return Bankcode;
}

std::string BOURNE::getName()
{
    return AgentName;
}

int main()
{
    BOURNE myAgent;
    cout<< "The agent's name is : " << myAgent.getName() << endl;
    cout<< "The agent's code is : " << myAgent.getCode() << endl;
}

Original Answer, left in because I feel like it's more useful

I suspect what you're asking is if you could do something like

void BOURNE::tryToGetSecretCodeandName(string theName, string theCode)
{
    if (Bankcode == theCode) {
        cout<< "You correctly guessed the code : " << Bankcode << endl;
    }
    if (AgentName == theName) {
        cout << "You correctly guessed the agent's name : " << AgentName << endl;
    }
}

This will allow you to repeatedly guess at the name, and get output when you're correct.

If you wanted to disable this kind of guessing, then you could consider creating a new class (possibly derived from/based on std::string - but see this question for reasons to be careful!) and implement an operator== function which always returned false.

Community
  • 1
  • 1
chrisb2244
  • 2,940
  • 22
  • 44
  • please all I need to Print the Bankcode and AgentName but it will not compile and it's like I can't get access to them? I don't understand, it works for int but not string or characters , please help – babylon Oct 08 '14 at 17:50
  • Edited to reflect your request - if this is what you wanted say and I'll tidy up. – chrisb2244 Oct 08 '14 at 17:56
  • you know what ? I am studying C++ and I want to understand it logically, I know other ways to show this code and to works but all I want is to understand where is my mistake, and what is wrong with it, Return-Type class-name ::function-name(argument declarations) { function-body } , this should work but , why is not work with me? – babylon Oct 08 '14 at 18:11
  • You have no function with signature `tryToGetSecretCodeandName()`. Your functions (both, now) take arguments. Giving us the compilation errors you're hitting would be helpful if it's not this. – chrisb2244 Oct 08 '14 at 18:14
  • i think it has something with what you just said .no matching function for call to 'BOURNE::tryToGetSecretCodeandName()'| – babylon Oct 08 '14 at 18:18
  • Good stuff... Your problem is (you guessed it!) you have no matching function call to ... . You can fix this by adding a new function (taking no arguments), or alternatively by giving arguments to your function call in your `main()` function. The first is probably clearer given your intentions however - if you only want to print values, then you don't need arguments, right? – chrisb2244 Oct 08 '14 at 18:22
  • I gave arguments to my function in main but it is still expecting something else from me like in the error shows-----------expected primary-expression before 'theName'| – babylon Oct 08 '14 at 18:30
  • It's expecting the word "string" before theName. If you already have the word in place, check your semicolons, especially on the previous line. – chrisb2244 Oct 08 '14 at 23:57