Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This assignment tests the concepts of: Classes Proper object oriented design and usage of const namespace Program Objective: The following items class was not writen
- This assignment tests the concepts of:
- Classes
- Proper object oriented design and usage of const
- namespace
Program Objective:
- The following items class was not writen with the best object oriented principals
- put the items class in the DS namespace (will not compile until you do)
- Add as many const keywords to the member and non-member functions of items, but the main driver should not require modification
- Change variables to static if you can
- Mitigate compile warnings
- The output should not change
- No need to add any inline functions
DELIVERABLES:
Description | filename |
---|---|
Your fixed header of items | items.h |
Your fixes implementation of items | items.cpp |
You should not modify main.cpp.
Submitted assignments without the above file named correctly will render your assignment as uncompilable and will be detrimental to your assignment grade.
Example output (from multiple runs with different arguments given):
42 50 530 530 false false true
Files
main.cpp
#include#include #include "items.h" using namespace std; using namespace DS; int main() { items a; items b(50); items c(b); items *p = &b; cout << a << endl; cout << b << endl; a.set("apple"); b.set(530); cout << a << endl; cout << b << endl; cout << boolalpha << a.same(&b) << endl; cout << boolalpha << a.same(&c) << endl; cout << boolalpha << b.same(p) << endl; return 0; }
items.h
#ifndef LAB_CONST_ITEMS_H #define LAB_CONST_ITEMS_H #include#include class items { public: items(); items(int); items(items&); int get() { return current; } void set(int); void set(std::string s); bool same(items*); private: const int everything = 42; int current; }; std::ostream& operator<<(std::ostream& out, items obj); #endif //LAB_CONST_ITEMS_H
items.cpp
#include "items.h" items::items() { set(everything); } items::items(int i) { set(i); } items::items(items& i) { set(i.get()); } bool items::same(items *p) { return this == p; } void items::set(int i) { current = i; } void items::set(std::string s) { if (s.length() > 0) { current = s.at(0); for (int i = 1; i < s.length(); ++i) current += s.at(i); } else current = everything; } std::ostream &operator<<(std::ostream &out, items obj) { out << obj.get(); return out; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started