Help with C++ Strings: Create a Class Function to Return a String

  • Comp Sci
  • Thread starter SEGA
  • Start date
  • Tags
    C++ Strings
In summary, a C++ string is a data type used to store a sequence of characters and can be manipulated using built-in functions. To create a class function that returns a string in C++, you can declare and define it using the scope resolution operator. Parameters can be passed to this function to manipulate the string before returning it. To use the class function in your code, an object of the class must be created and the function can be called. The returned string can also be modified using string manipulation functions in the code.
  • #1
SEGA
2
0
I want to know, how to make a CLASS FUNCTION to return a STRING.
For example, taken that all variables are declared correctly.

string personType::get() const
{
return firstName + " " + lastName;
}
 
Physics news on Phys.org
  • #2
To return a string, you just return a string. What is your real problem?
 
  • #3
The compiler write error-type
 
  • #4
There are many different types of "strings" possible in C++. Which one do you want to use?
 
  • #5


Great question! To create a class function that returns a string, you will first need to declare the function as part of your class. This can be done in the header file of your class by including the function prototype with the return type of "string". For example:

class personType {
private:
string firstName;
string lastName;
public:
string get() const;
};

Next, in the implementation file of your class, you will need to define the function and include the class scope resolution operator "::" to specify that it is a member function of the personType class. The function should also be declared as const to ensure that it does not modify any data members of the class. For example:

string personType::get() const {
return firstName + " " + lastName;
}

This function will return a string consisting of the first and last name of the personType object it is called on. You can modify the function to return any string you want, depending on the purpose of your class. I hope this helps!
 

Related to Help with C++ Strings: Create a Class Function to Return a String

What is a C++ string?

A C++ string is a data type used to store a sequence of characters. It is represented by the "string" class in C++ and can be manipulated using various built-in functions.

How do I create a class function to return a string in C++?

To create a class function that returns a string in C++, you can declare a function of type "string" in the class definition and define it outside the class using the scope resolution operator (::). Inside the function, you can use the "return" keyword to specify the string you want to return.

Can I pass parameters to a class function that returns a string?

Yes, you can pass parameters to a class function that returns a string in C++. These parameters can be used inside the function to manipulate the string before returning it.

How do I use the class function to return a string in my code?

To use the class function to return a string in your code, you first need to create an object of the class. Then, you can call the function using the object and store the returned string in a variable or use it directly in your code.

Can I modify the returned string from the class function?

Yes, you can modify the returned string from the class function in your code. Since the returned string is stored in a variable or used directly, you can use various string manipulation functions to modify it.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
964
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
965
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top