Question

C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names...

C++ PLEASE

Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient.

The header file for this class is given below.

#ifndef LETTER_H
#define LETTER_H

#include
#include

using namespace std;


class Letter
{
public:
   //constructor
   Letter(string from = "John", string to = "Ana");

   void add_line(string line);

   string get_text() const;

private:
   string sender;
   string recipient;
   vector lines;

};

#endif

-Implement the constructor to initialize data members sender and recipient.

-Implement member function add_line(string line) to add a line of text to the body of the letter. [hint: use push_back( ) member function of vector class]

-Implement member function get_text( ) that returns the entire text of the letter. The text has the form:

Dear recipient name:
Blank line
First line of the body
Second line of the body

Last line of the body
Blank line,
Sincerely,
Blank line
Sender name

-Supply a main( ) function that prints the letter.

Dear John:

I am sorry we must part.
I wish you all the best.

Sincerely,

Mary

Construct an object of the Letter class with “John” as recipient and “Mary” as sender and call add_line( ) twice.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

Letter.h

#ifndef LETTER_H
#define LETTER_H

#include<vector>
#include<string>

using namespace std;


class Letter
{
public:
   //constructor
   Letter(string from = "John", string to = "Ana");
   void add_line(string line);
   string get_text() const;

private:
   string sender;
   string recipient;
   vector<string>lines;
};
#endif

Letter.cpp

#include "Letter.h"

Letter::Letter(string from, string to)
{
   this->sender = from;
   this->recipient = to;
}

void Letter::add_line(string line)
{
   this->lines.push_back(line);
}

string Letter::get_text() const
{
   string str = "";
   str += "Dear " + sender;
   str += "\n\n";
   for (int i = 0; i < lines.size(); i++)
       str += lines[i] + "\n";
   str += "\n";
   str += "Sincerely,\n";
   str += recipient;
   return str;
}

Main.cpp

#include"Letter.h"
#include<iostream>

int main()
{
   Letter l("John", "Mary");
   l.add_line("I am sorry we must part.");
   l.add_line("I wish you all the best.");

   cout << l.get_text() << endl;

   system("pause");
   return 0;
}

Output

D:Chegg\C++\Letter class Debug Letter class.exe Dear John - x I am sorry we must part. I wish you all the best. Sincerely, Ma

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Provide the definition of class Message, which models an email message, in a file named Message.hpp...

    Provide the definition of class Message, which models an email message, in a file named Message.hpp and implement all the required member functions (see below) in a file named Message.cpp. A message contains a header and a body, which are separated by the first full line break in the message. The message header has a sender, a recipient, a subject representing the message heading, a Unix timestamp representing the sending time of the message, a character encoding, which you can...

  • Implement the Message class using a header and implementation file named Message.h and Message.cpp respectively. In...

    Implement the Message class using a header and implementation file named Message.h and Message.cpp respectively. In addition to the two Message class files, you must write a driver. The driver must create a Message array that is capable of holding 100 messages (make sure that you don’t exceed the maximum number of Messages). It then must read all Messages from a file named messages.txt (ordered date\n sender\n recipient\n body\n), sort the Messages based on date, find a Message based on...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Create a class hierarchy to be used in a university setting. The classes are as follows:...

    Create a class hierarchy to be used in a university setting. The classes are as follows: The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last name...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

  • C++ Define a class called Text whose objects store lists of words. The class Text will...

    C++ Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dynamic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting of words separated...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT