Newbie question about classes.

This is a discussion on Newbie question about classes. within the C++ Programming forums, part of the General Programming Boards category; Hi all, good to be here. I've only just started learning C++ and am having many associated problems . The ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Newbie question about classes.

    Hi all, good to be here. I've only just started learning C++ and am having many associated problems . The following program should output two 'postcards' one using the default constructor and another that has its sender, receiver and place specified by the user. On running the program the default postcard comes out fine, however the user defined one will only output the receivers name with the place and sender fields left blank. I am using the Borland compiler, any suggestions would be majorly appreciated . Thanks.

    My code is the following with the output listed afterwards:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class Postcard
    { public:
    
    	Postcard();
    	Postcard (string,string,string);
    	void print() const;
    	void personalise(string,string,string);
      private:
      	string receiver;
      	string sender;
      	string place;
    };
    
    Postcard::Postcard()
    {
    	receiver = "Adam";
    	place = "A Place";
    	sender = "John";
    }
    Postcard::Postcard(string x, string y, string z)
    {
    	receiver = x;
    	place = y;
    	sender = z;
    }
    void Postcard::personalise(string a, string b, string c)
    {
    	receiver = a;
    	place = b;
    	sender = c;
    }
    
    void Postcard::print() const
    {
    	cout << "Dear " << receiver << endl;
    	cout << "Having a lovely time at " << place << endl;
    	cout << "Wish you were here" << endl;
    	cout << "From " << sender << endl;
    }
    int main()
    {
    	string x, y, z;
    	Postcard p;
    	p.print();
    	cout << "Please enter the receiver's name, the place and the sender's name seperated by spaces: ";
    	cin >> x, y, z;
    	p.personalise(x,y,z);
    	p.print();
    }
    OUTPUT:
    'Dear Adam
    HAving a lovely time at A Place
    Wish you were here
    From John
    Please enter the receiver's name, the place and the sender's name seperated by spaces:
    Tony London Martin
    Dear Tony
    Having a lovely time at
    Wish you were here
    From'

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Use separate lines.
    Code:
    cin >> x;
    cin >> y;
    cin >> z;
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Thank You!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting to learn C++ Question about classes
    By uraliss in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:31 AM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. Quick Question on Classes
    By gguy85 in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2005, 02:28 AM
  4. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM
  5. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 04:01 PM

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