C++ Programming Assignment 2 Circle Class Lab Requirements : Construct a class named Circle that has a floating-point data member named radius . The class should have a zero-argument constructor...

1 answer below »


C++ Programming Assignment 2



Circle Class Lab




Requirements:



Construct a class named

Circle

that has a floating-point data member named
radius. The class should have a zero-argument constructor that initializes this data member to 0. It should have member functions named
calcCircumference() and
calcArea() that calculate the circumference and area of a circle respectively, a member function
setRadius()to set the radius of the circle, a member function
getRadius() to return the radius, and a member function
showData() that displays the circle’s radius, circumference, and area. The formula for the area of a circle is A=πr2. The formula for the circumference of a circle is C=2πr.



The class should use appropriate protection levels for the member data and functions. It should also follow “principles of minimalization”: that is, no member data should be part of a class unless it is needed by most member functions of the object. A general rule of thumb is that “if you can easily calculate it, don’t store it.”



Use your class in a program that creates an instance of a
Circle
(utilizing the zero-argument constructor), prompts a user for a radius, calls the setRadius() function to set the circle’s radius, and then calls showData() to display the circle’s radius, circumference, and area. Your program should allow the user to enter circle dimensions until the user enters -1. 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 that the user enters numeric data for the radius. Negative numbers (other than the -1 to exit) should also be prevented.




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 resusability. 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 be able 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. If both the interface and the implementation code are in the same file, this cannot be accomplished. 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 he or she can't see or change your code in your class functions.




  • Never use “using namespace std;” in a header file. Here is a link that describes why:



https://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file




  • Note that when you follow the rule above and don’t use “using namespace std;” in a header file, you have to remember to prefix certain things with std:: (i.e. cout, cin, endl, string, ostream, and vector). If you fail to prefix these words with std::, you will get a compilation error that is often quite cryptic. Be on the lookout for this situation. It may save you hours of debugging.




  • All "get" functions should be declared as constant. In fact, 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 provides protection of 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 the access. This adheres to the “Principle of Least Privilege,” which is a security principle that helps to protect the integrity of your data.



One other thing that's important to know for future reference: Whenever you make a function constant, it can't call another function unless that function is also constant. It makes sense that if you lock down an object in a function, you don't want to open it up for modifications by calling another function that allows it to be inadvertently altered. If you ever try to call a function that is not constant from a function that is constant, you will get a compilation error.




  • showData() should not refer to the data members directly. Instead, it should call getRadius() to retrieve its value. Think of your getters and setters as middlemen. You should try to avoid accessing your data members directly due to maintenance issues. Pretend that you decided to change the name of your radius variable to be "rad". You really should only have to make that change in two functions: getRadius and setRadius. If you refer to the private data members directly throughout your code, you'll have a nightmare trying to make all the changes necessary. If you always use the getters to retrieve your data, any changes you make to the variable name will only necessitate changes to your get function rather than your whole code base.




  • CalcCircumference, CalcArea, and ShowData should
    not
    have the radius passed in as an argument because radius is a data member of the class. Class functions have
    direct
    access to all data members in their own class. By passing in an argument for the value of the radius, you're not using the value that has already been stored in the data member. You're using the value that you've passed in to the functions, which circumvents the whole purpose of storing radius 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. (Hint: a good constant to create in your program would be the value of pi.)




  • Finally, be sure not to include any unnecessary libraries.





Deliverables:


o Complete the programming assignment described above and submit your completed assignment in accordance with the assignment submission policies.




To give you an idea of the general criteria that will be used for grading, here is a checklist that


you might find helpful:

























































































Compiles and Executes without crashing




No global variables




Appropriate Internal Documentation




Rectangle Class Created:




Circle.h (header file)




Circle.cpp (class file)




CircleDriver.cpp (Driver file)




Data members:




Appropriate data members are declared




Style:




Appropriate Pre-processing and using directives






Constructor:




Zero argument constructor initializes data member to zero




Member functions are correct and used correctly:




calcCircumference()




calcArea()




setRadius()




getRadius()




showData()




Protection:




Member functions and variables are declared with appropriate

protection (i.e. private or public)




Object Creation:




Circle object created correctly




User Interaction:




Prompts user for radius with appropriate error checking




Loops until -1 is entered





Review the submission instructions document prior to uploading your work to Blackboard.



Submit this assignment by 11:59 p.m. (ET) on Monday of Module/Week 2.

Answered Same DayJan 25, 2021

Answer To: C++ Programming Assignment 2 Circle Class Lab Requirements : Construct a class named Circle that...

Neha answered on Jan 27 2021
133 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here