Yasmeen Y.

asked • 06/29/21

C++ please check if it’s correct or not

The assignment is:

Write a tiny library and submit it in files AboutDog.h and AboutDog.cpp.


This library has the only function: 

void AboutDog(const std::string & dogName, unsigned int description)

The function AboutDog outputs on the screen the following text:


This dog is called *dogName*. It is *list of dog features from description*.


description encodes the dogs properties the following way:

  1. set bit 0 means the dog is big
  2. set bit 1 means the dog is hairy
  3. set bit 2 means the dog is brown
  4. set bit 3 means the dog is smart

In the header file, define macros DOG_BIG, DOG_HAIRY, DOG_BROWN, DOG_SMART. I should be able to call the function like this:

AboutDog( "Spike", DOG_BROWN | DOG_SMART | DOG_HAIRY );

The above call should produce the output like this:

This dog is called Spike. It is hairy brown smart.

The order of features doesn't matter. Punctuation doesn't matter too. If description==0 skip the second sentence.


is this solution correct :

AboutDog.cpp:

#include "AboutDog.h"

using namespace std;


void AboutDog(const std::string & dogName, unsigned int description) {

cout<<"This dog is called "<<dogName<<". ";

if(description != 0) {

cout<<"It is";

if(description & DOG_BIG) {

cout<<" big";

}

if(description & DOG_HAIRY) {

cout<<" hairy";

}

if(description & DOG_BROWN) {

cout<<" brown";

}

if(description & DOG_SMART) {

cout<<" smart";

}

cout<<"."<<endl;

}

}

AboutDog.h:

#include <iostream>


enum dog{DOG_BIG, DOG_HAIRY, DOG_BROWN, DOG_SMART};


1 Expert Answer

By:

Yasmeen Y.

For later questions how do I know he’s asking for macros?
Report

06/30/21

Daniel B.

tutor
This is the key sentence: "In the header file, define macros DOG_BIG, DOG_HAIRY, DOG_BROWN, DOG_SMART." That makes it quite unambiguous that he is asking for macros. On the other hand, this might be just a suggestion and an enumeration type might be acceptable. It depends what he wants you to practice.
Report

06/30/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.