at school,I was given the class aName and asked to overload the input operator.the class has declared surname as type of char* and the surname is input from the keyboard and it must not be empty.please help me with the code.the class has declared the operator as its friend

Dani AI

Generated

The original posts correctly flag two separate problems: memory-management with raw char* members, and incorrect stream-operator signatures. correctly warned that copying the pointer only copies the address (not the characters). also noted allocation/copying and that the stream operators should return references. Those fixes are necessary, but a much simpler and safer approach in modern C++ is to use std::string for the members and then implement the I/O operators to validate non-empty surname.

Example (safer, minimal change to behavior):

#include <iostream>
#include <string>
#include <sstream>

struct aName {
    std::string surname;
    std::string firstname;
};

std::istream& operator>>(std::istream& is, aName& n) {
    std::string line;
    if (!std::getline(is, line)) return is;
    std::istringstream iss(line);
    if (!(iss >> n.surname)) { is.setstate(std::ios::failbit); return is; }
    std::getline(iss, n.firstname);
    if (!n.firstname.empty() && n.firstname.front() == ' ') n.firstname.erase(0,1);
    if (n.surname.empty()) is.setstate(std::ios::failbit);
    return is;
}

std::ostream& operator<<(std::ostream& os, const aName& n) {
    os << n.surname;
    if (!n.firstname.empty()) os << ' ' << n.firstname;
    return os;
}

Notes and troubleshooting:

  • Using std::string avoids manual allocation, leaks, and double-delete bugs. If legacy char* is required, implement destructor, copy constructor and copy-assignment (Rule of Three) so deep copies happen and destructors free owned memory. See Rule of Three.
  • Comparing a char* parameter to an empty string literal tests pointer equality, not content. To test content, check for null pointer and then aSurname[0] == '\0' (or use strcmp).
  • Operator semantics: streaming operators should return the stream by reference. For interactive re-prompting, handle the input loop in calling code rather than embedding repeated prompts inside operator>>; alternatively, make operator>> set the stream’s failbit on invalid input so the caller can decide how to recover.

This preserves the requirement that surname not be empty while avoiding raw-pointer pitfalls pointed out by and implementing the corrected operator semantics mentioned by .

Recommended Answers

All 6 Replies

post the class then we can discuss how to implement to >> operator.

post the class then we can discuss how to implement to >> operator.

class aName
{
friend std::istream operator>>(std::istream & input,aName &name);
/*input the name from the standard input stream like the keyboard and the name is never empty*/
friend std::ostream operator<<(std::ostream & output,aName name);
//it displays the name on the standard output like the screen

private:
char*surname,
       *firstname;

public:aName(char *aSurname,char*aFirstname);
void set(char *aSurname,char* afirstname)
{
if(aSurname=="")
  surname="";
else
surname=aSurname;
if(afirstname=="")
firstname="";
else
firstname=afirstname;
};
Member Avatar for Member #248612

Warning! This line

surname=aSurname;

just copies the pointer not the content!!!

>>surname=aSurname;

First you have to allocate space for surname then call strcpy() to copy it

surname = new char[strlen(aSurname)+1];
strcpy(surname, aSurname);

The two friend functions must return a reference to the istream and ostream objects. Here's correction

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#pragma warning( disable: 4996) // only needed for VC++ 2008 compiler

class aName
{
friend std::istream& operator>>(std::istream & input,aName &name);
/*input the name from the standard input stream like the keyboard and the name is never empty*/
friend std::ostream& operator<<(std::ostream & output,aName name);
//it displays the name on the standard output like the screen

private:
char*surname,
    *firstname;

public:
    aName() { surname = NULL; firstname = NULL;}
    aName(char *aSurname,char*aFirstname)
    {
        surname = NULL; firstname = NULL;
        set(aSurname, aFirstname);
    }

    void set(char** name, const char* aName)
    {
        if( *name != NULL)
            delete[] *name;
        *name = NULL;
        if( aName != NULL)
        {
            *name = new char[strlen(aName)+1];
            strcpy(*name, aName);
        }
    }
    void set(const char *aSurname,const char* afirstname)
    {
        set(&surname, aSurname);
        set(&firstname, afirstname);
    }
};

istream& operator>>(istream& in, aName& nm)
{
    // TODO:  complete this function.
    return in;
}

int main()
{
    aName n;
    cin >> n;
}

[edit]Oops! I just noticed I did half your homework:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.