Hi,I got a Class problem for my C++ Class, I am not exactly understand what it m
ID: 673656 • Letter: H
Question
Hi,I got a Class problem for my C++ Class, I am not exactly understand what it means, anyone can help me answer it?
The class Pen represents some kind of writing instrument. Each instance of Pen is comprised of a brand and color (both string) and a boolean which tracks whether the pen's cap is on or off. Like the pen on your desk, you can't write with it until you take the cap off. The point behind this assignment is to realize that not all methods are created equal. In fact, very often, there is a logical sequence to order in which certain methods can be called. It is the class itself that enforces this ordering. Note the various errors that are printed when driver code doesn't use the Pen class correctly, like when it tries to write before taking the cap off. As we learn more complex classes, this kind of sequence to the way methods are called is quite typical. Following the class diagrams shown below, implement the Pen class. Embed your class in a suitable test program that provides the sample dialogue shown below. You may choose to create any number of additional methods, but you are required to implement all of the public methods shown below. You are free to ignore the private parts of the class I suggest below.
Implementation Details Sample Driver with Sample Output Pen Pen p: // brand new pen made! p.setcolor"blue" ) p.setBrand"Bic"); // pen's color set to blue! // pen's brand set to Bic! Pen) void setcolor string color void setBrand string brand) string getcolor string getBrand) p.unCap) p.write( "hel1o": p.reCap(): // pen's cap is now off! // blue Bic is writing hello! // pen's cap is back on! // now let's get some errors. . . p.write( "hello world" ) void unCap void write string message void reCap // you can't write with this pe // pen's cap is now off! // pen's cap is back on! // you can't write with this pe p.unCap ): p.recap reCap bool isCapOn O: p.write( "hello world"; string myColor: string myBrand; bool my capOn:Explanation / Answer
Program:
// penn.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
class Pen
{
std::string mycolor;
std::string myBrand;
std::string message;
bool my_CapOn;
void setColor(string color)
{
mycolor=color;
}
void setBrand(string brand)
{
myBrand=brand;
}
string getColor()
{
return mycolor;
}
string getBrand()
{
return myBrand;
}
void unCap()
{
cout<<"pen cap is off";
}
void write(string message1)
{
message=message1;
cout<<"message written";
cout<<message;
}
void reCap()
{
cout<<"pen cap is on";
}
bool iscapOn()
{
return true;
}
int main()
{
Pen p;
p.setColor("blue");
p.getColor();
p.unCap();
p.write("hello");
p.reCap();
p.write("hello world");
p.unCap();
p.reCap();
p.write("hello world");
return 0;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.