about private class variables

This is a discussion on about private class variables within the C++ Programming forums, part of the General Programming Boards category; I need to be able to use a private class variable in several cout statements. I need to do something ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    17

    about private class variables

    I need to be able to use a private class variable in several cout statements. I need to do something like this..

    Code:
    cout <<"Hello " <<Person.displayName() <<"how are you today" <<endl;
    but that obviously gives me errors.

    Here is the class, the code isn't perfect and I actually typed it out in wordpad. None of it has been compiled so please disreguard all the obvious errors.
    Code:
    class Persons {
    public:
            void setName(string); 
    	string getName();
    	void displayName();
    private:
             string PersonName;
    };
    
    void Person::setName(string name){
    	PersonName = Name;
    }
    
    string Person::getPName() {
    	return PersonName;
    	}
    void Person::displayPName() {
    	cout <<getName();
    }
    Now, I can use it the way I want but I have to do it in a rather messy way.
    Code:
    cout <<"Hello ";
    Person.displayName();
    cout <<" how are you today?" <<endl;
    Is there a better way to do this so I don't have to break up the cout statments?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Instead of using cout in your class return the String.

    Code:
    string Person:: displayName()
    {
        return PersonName;
    }
    
    .....
    
    cout << Person.displayName();
    What is C++?

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Do you see anything in "your code" that might return the name instead of printing it? Try to use that instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Classes are objects and thus do not print anything to the screen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    17
    cool thanks

    I removed displayname() and just used getname() and it worked great when I plugged it into my compiler.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    (WARNING: Uncompiled code follows)

    Make the class print itself:

    Code:
    class Person {
        string Name;
    public:
        friend ostream & operator<<(ostream & os, const Person & p)
        {
            os << p.Name;
            return os;
        }
    };
    
    ...
    
    Person p;
    
    cout << p << endl;
    Last edited by medievalelks; 04-18-2008 at 12:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-02-2008, 11:45 AM
  2. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 02:57 PM
  3. Declaring SDL_Rect variables in a class?
    By pwnz32 in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 07:12 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21