Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming: Class Creation Program Overview The purpose of this assignment is to give you some practice with creating your own classes. This program serves

C++ Programming: Class Creation Program

Overview

The purpose of this assignment is to give you some practice with creating your own classes. This program serves as the basis for all the other programming assignments in this class and your future Computer Science classes.

Reminders:

  • Do not put all your code into a single file. It will cost you points.
  • Each Class you create requires two files. The first one is .h and the second one is .cpp. If each Class is not in this format, it will cost you points.
  • Each class has one or more constructors. Many students make a mistake with their Class constructors (you will be learning about them) and use code that looks like this:
    (std::string k) : setName(k) {}
    • The code in bold and yellow belongs between the {}, not in the declaration line of the constructor. There is a loophole in C++ that allows you to put code after the constructor declaration, but in CSIS 112, always put the code between the {}, or it will cost you points.
      Use the example below for the correct format.
      (std::string k)
      {
      setName(k);
      }
    • You will find examples of code after the constructor declaration and a colon. The only purpose we put code after the constructor declaration in this class is when we get into Inheritance. We will not use Inheritance until the last few weeks of the course.
  • Unless instructed to do so, there is no user input or output done inside a class.
  • Remember to review the checklist at the end of the instructions.

 

Instructions

Class:

Name: Circle

Purpose: To accept and store the data for a circle and to provide functions that will manipulate the data.

Constructor:

  • Zero-argument constructor that initializes the data members to 0.

Data Members:

  • radius - int
    Must be 1.0 or greater.
  • colorFill - string
    Cannot be blank.

Functions:

  • calcDiameter() - double
    Calculates and returns the diameter of the circle.
    The diameter is a double.
  • calcArea() - double
    Calculates and returns the area of the circle.
    The area is a double.
  • calcCircumference() - double
    Calculates and returns the circumference of the circle.
    The circumference is a double.
  • setRadius() - void
    Accepts a radius value and updates the radius data member.
  • getRadius() - int
    Returns the value stored in the radius data member.
  • setColorFill() - void
    Accepts the fill color value and updates the colorFill data member.
  • getColorFill() - string
    Returns the value stored in the colorFill data member.

Note: Setters and Getters are required even if they are not used.

 

Non-Class Functions:

  • displayCircleInfo() - void
    Creates and displays the formatted output of the book shipment information.

Formulas:

  • Note: The calc functions are returning a double, and the radius is an integer. The simplest way to convert an int to a double is to multiply the radius by 1.0.
    Example:
    double myDiameter;
    myDiameter = (getRadius * 1.0) * 2;
  • Area = Pi * radius squared
    For Pi, use the value of 3.14 so your results will match mine. You can make this a const using the following line of code in the Circle.cpp file above the constructor
    const double PI = 3.14;
    For radius squared, use the pow() function instead of getRadius() * getRadius()
    Example: pow(4, 2) is 4 squared where 4 is the value of the radius, and 2 is the power to raise the radius to, in this case, squared. Use the getRadius() function to get the radius value.
  • Circumference = 2 * PI * radius
  • Diameter = 2 * radius

The class should use appropriate protection levels for the member data and functions. Data members must be in the private: section. It must also follow "principles of minimalization": that is, no data member should be part of a class unless most functions of the object need it. A general rule of thumb is that "if you can easily calculate it, don't store it."

Program Flow:

  • Start a loop that goes until the user enters -1 for the radius.
  • Create the Circle object using the zero argument constructor.
  • Prompt the user for the radius. If the user enters -1, exit the loop, otherwise, update the Circle object with the value entered.
  • Prompt the user for the fill color and update the Circle object with the value entered.
  • Display the Circle information using a separate function. This function will call the Circle object functions defined in the class.
  • Pause the display so the user can read the displayed information.

 

Your program should allow the user to enter new circle radius until the user enters -1 for the radius. Be sure to include appropriate error checking. Does it make sense to enter "abc" as the radius of a circle? No. Therefore, you should ensure the user enters numeric data for the side. Negative numbers (other than the -1 to exit) should also be prevented. Blanks are not allowed for the fill color.

 

use a function named displayCircleInfo() in the file that contains main(). The purpose of this function will be to display the circle information as shown above. It will get all the data needed from the circle class. This URL will show you an example of creating and using a function. https://cplusplus.com/doc/tutorial/functions/

 

To format the data shown above inside the box with the dot border. The setw() function will define the width of each column of data and the left or right commands will defined the column data as left or right justified.

Be sure to test your program with several different positive radius values.

Style:

  • Your lab should be constructed such that separate files are used: Circle.h (your class declaration file), Circle.cpp (your class implementation file), and CircleDriver.cpp (the file that contains main() and any other functions that are not part of the class).

The purpose of having separate files is for code reusability. If other developers want to use your class in their programs, they don't need main() as well. They should only have to "#include" your class header file. As such, two separate files for the class are needed to hide the implementation details from other developers. You want other developers to know how to use your class (i.e., what functions are available and how they are called -- this is called the "interface" of the class), but not how they are implemented. This cannot be accomplished if both the interface and the implementation code are in the same file. When you distribute your class to other developers, the implementation (.cpp) file gets compiled, but the interface (.h) doesn't. That way, the developer can use your class, but they can't see or change your code in your class functions.

  • Never use "using namespace std;" in a header (.h) file.
  • All functions that are to return a value, "get", "calc", etc., should be declared as constant. Example: string myProcess() const;Any function that does not change the data in the data members should be declared as constant. This is a security measure that prevents anyone from accidentally writing code in a function that changes underlying data when the purpose of the function is only to retrieve the data. In other words, "const" at the end of a function protects your data. The rationale is that certain functions (e.g., those that merely return a data item to the caller or even those that just print the data) should be prevented from ever changing the underlying data. If a function doesn't need "write" access to a data item, it shouldn't be granted access. This adheres to the "Principle of Least Privilege," a security principle that helps protect the integrity of your data.
  • The functions calcDiameter()calcArea(), and calcCircumference() must not have the radius passed in as arguments. Class functions have direct access to all data members in their own class through the getters. When you pass in an argument for the value of the radius, you're not using the value that has already been stored in the corresponding data member. You're using the value you've passed into the functions, which circumvents the whole purpose of storing data in the class.
  • Use good prompting (correct spelling and clear instructions), output labeling, and modularization in your program and class.
  • Be sure to avoid using global variables in your program unless they are constants.
  • Finally, be sure not to include any unnecessary libraries.


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts In Federal Taxation

Authors: Kevin E. Murphy, Mark Higgins, Tonya K. Flesher

19th Edition

978-0324379556, 324379552, 978-1111579876

More Books

Students also viewed these Algorithms questions

Question

understand possible effects of ethnicity;

Answered: 1 week ago

Question

Explain the limitations placed on deductions for medical expenses.

Answered: 1 week ago

Question

9.3 Evaluate methods used to treat eating disorders.

Answered: 1 week ago