Question

In: Computer Science

C++ Project 1 For this project, we are going to create our own classes and use...

C++ Project 1

For this project, we are going to create our own classes and use them to build objects. For each class you will create an *.h and *.cpp file that contains the class definition and the member functions. There will also be one file called 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them.

You will create 2 new classes for this project, one will be from the list below and one will be a required 'Date' class. Here is the list:

WebSite
Shoes
Beer
Book
Song
Movie
TVShow
Computer
Bike
VideoGame
Car

The process is: take your assigned class from the list such as Book. Add to your project new files called    Book.h and Book.cpp   and create the new class from scratch in those files. I suggest following the simple technique for adding a class discussed in the lecture. Do not overthink. Just come up with the 3 most important data members that describe the class/object you want to build and the write 2 constructors, 3 get member functions, 3 set member functions and a toString method and you are done.

Once you have created a new class, you need to adjust the main() function to allow the user to input the data members for your class and create an object of that type. Use the toString method to then display the new object on the screen. When the project is complete, both classes/objects should be created and displayed with main().

Please do not use the same 3 data types for any class's member variables. That is, do not make them all ' int ' or all ' string '.

As for the Date class, the data members will be int day; int year; string month; Create a Date class with 2 constructors, 3 get member functions, 3 set member functions and a toString method. Adjust the main() function to use the Date class.
When the user wants to build a Date object, on the screen I would input 3 values, one for each member variable. Insert values into the Date object then output a simple message like:

"The Date object is: March 2, 2013"

It is very important that the data input be inserted INTO the object, and pulled back OUT of the object to print the above message.

To ensure you complete each class, use this checklist:

_____  Three global variables (not the same type)

_____  Two constructor methods

_____  Three 'get' methods

_____  Three 'set' methods

_____  One 'toString' method

_____  In the main function you create an object, insert values into the object, and print the object


Good Luck

Upgrade the Date Class

There are three (3) levels of understanding of objects. The first is objects are composed of variables and methods. Variables should be private and simple set/get methods allow us to insert data into and pull data back out of the objects.

The second level of understanding is objects should verify their data! To prevent 'Garbage IN, Garbage OUT' the object should NEVER allow bad data to be assigned to the member (or global) variables. Modify the set methods and constructors of the date class to make SURE the day value is always between 1 and 31, make sure the year value is between 1970 and 2099. If the user tries to assign a bad value, print a simple error message and DO NOT change the value of the variable. Example, the 'setDay' is called with the value 99, a simple message like "Value of day is not in range 1 to 31" should be output and the variable day SHOULD NOT be modified.

The third level of understanding is objects are SMART! Methods can and do generate actions and information that are not explicitly stored in the object. The date class has a string month variable that stores "jan", "feb" ... "dec". Everyone knows month could ALSO be stored as an integer value 1 ("jan") to 12 ("dec"). But we DO NOT need to store BOTH; we can generate the number when needed. Write a method: int getMonthNumber(); that returns the proper value 1-12 based on the string stored in the month variable. It should return the value -1 if the current value in the month variable is NOT valid. DO NOT store the month number in a member variable (local is OK), generate it when the method is called.

Write a method: void printDate( int format ); when it is called the format value will determine how the date is output to the screen using cout:

format 0: Mar 12, 2013
format 1: 12 Mar 2013
format 2: 3-12-2013
format 3: 3/12/13

Of course the actual values stored in the object will be output, this is only an example. Modify the main program to print the date in these additional formats. Optional: Use the int getMonthNumber(); method to check if the user enters a proper value for the month variable.


Solutions

Expert Solution

Book.h

#include<iostream>

using namespace std;

class Book{
  
   public:
       Book();
       Book(string t, int y, double p);
      
       string getTitle();
       int getYear();
       double getPrice();
      
       void setTitle(string t);
       void setYear(int y);
       void setPrice(double p);
       string toString();
   private:
       string title;
       int year;
       double price;
};

Book.cpp

#include"Book.h"
#include<iostream>
#include<string.h>

using namespace std;

Book::Book(){
  
}

Book::Book(string t, int y, double p){
   title = t;
   year = y;
   price = p;
}
      
string Book :: getTitle(){
   return title;
}


int Book :: getYear(){
   return year;
}

double Book::getPrice(){
   return price;
}

void Book:: setTitle(string t){
   title = t;  
}

void Book :: setYear(int y){
   year = y;
}

void Book :: setPrice(double p){
   price = p;
}

string Book::toString(){
   string str = "";
   str.append("Title: ");
   str.append(this->title);
   str.append(", Price: ");
   str.append(to_string(this->price));
   str.append(", Publication year: ");
   str.append(to_string(this->year));
  
   return str;
}

Date.h

#include<iostream>
using namespace std;

class Date{
   public:
       Date();
       Date(int d, string m, int y);
      
       int getDay();
       string getMonth();
       int getYear();
      
       void setDay(int);
       void setMonth(string);
       void setYear(int);
      
       string toString();
      
   private:
       int day;
       string month;
       int year;
};

Date.cpp

#include"Date.h"
#include<iostream>

Date::Date(){
  
}

Date::Date(int d, string m, int y){
   day = d;
   month = m;
   year = y;
}

      
int Date:: getDay(){
   return day;
}

string Date :: getMonth(){
   return month;
}

int Date :: getYear(){
   return year;
}
      
void Date :: setDay(int d){
   day = d;
}

void Date::setMonth(string m){
   month = m;
}

void Date::setYear(int y){
   year = y;
}
      
string Date::toString(){
  
   string msg = "";
   msg.append("The Date object is: ");
   msg.append(month);
   msg.append(" ");
   //msg.append(to_string(day));
   msg.append(", ");
   //msg.append(to_string(year));
  
   return msg;
  
}

Main.cpp

#include"Date.cpp"
#include"Book.cpp"
#include<iostream>

using namespace std;

int main(){
  
   string bname, month;
   int year, date;
   double price;
  
   cout<<"Enter book name: ";
   getline(cin, bname);
  
   cout<<"Enter book year of publication: ";
   cin>>year;
  
   cout<<"Enter price: ";
   cin>>price;
  
   Book book(bname, year, price);
  
   cout<<book.toString()<<endl;
  
   cout<<"\n--------------------------------------\nEnter date: ";
   cin>>date;
  
   cout<<"Enter month: ";
   cin>>month;
  
   cout<<"Enter year: ";
   cin>>year;
  
   Date dateObj(date, month, year);
  
   cout<<dateObj.toString();
  
  
   return 0;
}


Related Solutions

Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
IN PYTHON INF 120 – Project #6.5 In project 6.5 we are going to create a...
IN PYTHON INF 120 – Project #6.5 In project 6.5 we are going to create a program and test cases to test each path/branch through the program. Our program is going to get a word from the user and check if it is a palindrome. If it is it will output to the user that it is a palindrome. A palindrome is where the word is the same forwards as backwards. For example racecar.If the word isn’t a palindrome then...
Create a Snake game project in C++ using all these which are given below. 1. Classes...
Create a Snake game project in C++ using all these which are given below. 1. Classes 2. structures 3. files ( MULTFILLING AND FILE HANDLING ) 4. loops 5. conditions 6. switch 7. functions 8. array Note: Also upload the zip file.
Programming Project #1 – Day Planner In this project we will develop classes to implement a...
Programming Project #1 – Day Planner In this project we will develop classes to implement a Day Planner program. Be sure to develop the code in a step by step manner, finish phase 1 before moving on to phase 2. Phase 1 The class Appointment is essentially a record; an object built from this class will represent an Appointment in a Day Planner . It will contain 5 fields: month (3 character String), day (int), hour (int), minute (int), and...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We will be creating a base class for our game character. The base class will be LifeForm. We will have derived classes of Human, Dragon, and Unicorn. LifeForm will have the following attributes: hitPoints – range 0 to 100 strength – range 0 to 18 You will need to provide a default constructor that initializes these attributes as follows: strength – 15 hitPoints – 100...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes (Session and Problems) and one driver class and ensure the user inputs cannot cause the system to fail by using exception handling. Overview from Project . Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+,-, *, /), the range of the factors to be used in the problems, and the number of problems...
C++In Object Oriented Programming, classes represent abstractionsof real things in our programs. We must...
C++In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition. The class must have at least three properties/data fields and three functions/behaviors (constructors and get/set functions don’t count). Do not pick examples from the textbook and do not redefine...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT