• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 66
  • Last Modified:

How to print a particular word/string from a file in c++

from the attachmet i need to print the output as

1.       class.User                                                                  

2.       class.User.mv.UserID                                             (mvè Member Variable)

3.       class.User.mv.UserPassword
0
srikar gajawada
Asked:
srikar gajawada
  • 3
1 Solution
 
Fabrice LambertFabrice LambertCommented:
Hi,

I don't see any attachment...
You need to print, on the standard output or on a printer ?

On standard output, you'll need the "<<" operator:
std::string myData{"random text."};
std::cout << myData << std::endl;

Open in new window

To print a class, you'll probably need to overload the "<<" operator (according that make a sens of course).

Without seeing your class design or source code, I can't advice any further.
0
 
srikar gajawadaAuthor Commented:
please find the attachment for more info
User.cpp
0
 
Fabrice LambertFabrice LambertCommented:
You should provide the header also.

From what I see so far:
The constructor must initialize the UserID member, as creating a user without ID make no sens.
The constructor must initialize a default password, again creating a user without password make no sens. In consequence, the parameter-less constructor should not exist.
The setUserId member function shouldn't exist, changing a user ID make no sens.
The user class have an entity semantic, thus should not be copyable neither assignable. This is done by deleting the copy constructor and assignment operator.
Getters should be const qualified.
Parameters that shouldn't be modified should be const qualified.
Do not use the directive "using namespace std". It's sole purpose was maintain old code base when namespaces were introduced in C++ (back in 1990). Today, there is no reason to use it, as it is a potential source of name conflicts and ambiguities.

To print the class, you either call its services to retrieve user datas (but password, as printing it make no sens) or overload the << operator.
Code below with operator overload.

User.h:
#include <iostream>
#include <string>

class User
{
private:
	std::string UserId;
	std::string UserPassword;
public:
	User(std::string const& userID);	    // constructor
	User(User const&) = delete;           // copy constructor deleted.
	~User();						   // destructor
	User& operator=(User const&) = delete;    // assignment operator deleted.
	const std::string& getUserId() const;           // getters
	const std::string& getUserPassword() const;
	void setUserPassword(std::string const& userPassword);  // setter
	friend std::ostream& operator<<(std::ostream& os, User const& user);    // operator << overload
};

Open in new window

User.cpp
#include "User.h"

	// constructor, initialize member via initialisation list.
User::User(std::string const& userID): UserId{userID},
							           UserPassword{"default"}
{
	std::cout << "\n User class constructor was called\n";
}

	// destructor
User::~User()
{
	std::cout << "\n User class destructor was called\n";
}

	// getters (const qualified)
const std::string& User::getUserId() const
{
	return UserId;
}

const std::string& User::getUserPassword() const
{
	return UserPassword;
}

    // setter
void User::setUserPassword(std::string const& userPassword)
{
	this->UserPassword = userPassword;
}

    // operator << overload
std::ostream& operator<<(std::ostream& os, User const& user)
{
    os << user.UserId;
    return os;
}

Open in new window

0
 
Fabrice LambertFabrice LambertCommented:
No answer from asker.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now