Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a string function using object does not return any value


I am trying to practice the Encapsulation in objects and classes so I am using set and get methods. The string value being used by those two methods is user-inputted. After setting the value in the set method, I tried calling the get method to print its return value in the print method but it seems that the returned value just disappears.

To run down my program, first, I created the Employee Class with its respective attributes and methods as shown below:

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

class Employee
{
  public:
    // Variable Declaration
    string userName;
    string inputName;

    // Function Declaration
    void enterName();
    void setName(string inputName);
    string getName();
    void printName();
};

All the functions are then defined including the main function.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int main() 
{ 
    // Create object of Employee class
    Employee myEmp;

    // Call input function
    myEmp.enterName();

    return 0;
}

void Employee::enterName()
{
    // Get name
    cout << "Enter name: ";
    getline(cin, inputName);

    // Pass inputted name to setName function
    setName(inputName);

    // Calling print function
    printName();
}

// Setter Function
void Employee::setName(string inputName) 
{
    // Copy user-inputted name to a new variable 
    userName = inputName;
}

// Getter Function
string Employee::getName()
{

    return userName;

}

// Print Function
void Employee::printName()
{
    
    Employee myObj;
    cout <<  "The name " + myObj.getName() + " is printed." << endl;  
    
}

In this part, myObj.getName() does not properly print nor retrieve the returned value of the get method.

cout <<  "The name " + myObj.getName() + " is printed." << endl;

My question is how can I print the returned value of a method using Object-Oriented Programming, specifically using Encapsulation. However, feel free to educate me with more efficient ways as long as I can use OOP.

>Solution :

printName() is printing the name of a new blank Employee object, not the name of the object it is called on. Drop the local myObj variable, use this instead, like all of your other class methods are using, eg

void Employee::printName()
{
    cout << "The name " + /*this->*/getName() + " is printed." << endl;
} 

Or

void Employee::printName()
{
    cout << "The name " + /*this->*/userName + " is printed." << endl;
} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading