Thread: Newbie question about classes.

  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,268
    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, 03: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, 10:17 PM
  5. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM