Question

In: Computer Science

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 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. Come up with the 3 most important data members that describe the class/object you want to build and then write 2 constructors, 3 get member functions, 3 set member functions and a toString type method that will print out the value of the three variables.

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 (or preset with {} if you want) and create an object of that type.

Make a toString type method to then display the new object information on the screen. You can try the confusing overload function that’s in the video example, or just concatenate the values together into one string using to_string.

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 '.

Class #2 Save a date

The three variables will be day, year, month. User should be able to set and get those plus just ask for the date from a getDate function and get back something like:

"The Date object is: March 2, 2013"

Add a format parameter to method to getData so it will return a string in one of three or four formats, ie

format 0: Mar 12, 2013

format 1: 12 Mar 2013

format 2: 3-12-2013

format 3: 3/12/13

Also make sure to validate their entry to be legal, ie: days between 1 and 31, years between 1970 and 2070, etc.

Main Function

When your classes are complete, both classes/objects should be created and displayed with main().

Solutions

Expert Solution

SOLUTION -

I have solve the problem in c++ code with comments and screenshot for easy understanding :)

CODE-

SaveADate.h

#include <iostream>

#include <sstream>

using namespace std;

class SaveADate {
  
   // Required member variables
  
   private:
      
       int day;
       int year;
       int month;
      
   // All Required member functions
  
   public:
      
       // Constructor
       SaveADate();
       SaveADate(int d, int y, int m);
      
       // Setters and Getters
      
       void setDay(int d);
       void setYear(int y);
       void setMonth(int m);
      
       int getDay() const;
       int getYear() const;
       int getMonth() const;
      
       // getData function
      
       string getData(int format);
};

SaveADate.cpp

#include "SaveADate.h"

SaveADate::SaveADate() {
   day = 1;
   year = 1970;
   month = 1;
}

SaveADate::SaveADate(int d, int y, int m) {
  
   // Calling set functions which also make validation
   setDay(d);
   setYear(y);
   setMonth(m);
}

void SaveADate::setDay(int d) {
  
   if(d >=1 && d <= 31)
       day = d;
   else
       day = 1;
}

void SaveADate::setYear(int y) {
  
   if(y >= 1970 && y <= 2070)
       year = y;
   else
       year = 1970;
}

void SaveADate::setMonth(int m) {
  
   if(m >= 1 && m <= 12)
       month = m;
   else
       month = 1;
}

int SaveADate::getDay() const {
   return day;
}
int SaveADate::getYear() const {
   return year;
}
int SaveADate::getMonth() const {
   return month;
}

string SaveADate::getData(int format) {
  
   // Getting respective month
  
   string mon = "";
   if(month == 1)
       mon = "Jan";
   else if(month == 2)
       mon = "Feb";
   else if(month == 3)
       mon = "Mar";
   else if(month == 4)
       mon = "April";
   else if(month == 5)
       mon = "May";
   else if(month == 6)
       mon = "June";
   else if(month == 7)
       mon = "July";
   else if(month == 8)
       mon = "Aug";
   else if(month == 9)
       mon = "Sep";
   else if(month == 10)
       mon = "Oct";
   else if(month == 11)
       mon = "Nov";
   else if(month == 12)
       mon = "Dec";

   stringstream str;
  
   if(format == 0)
       str << mon << " " << day << ", " << year;
   else if(format == 1)
       str << day << " " << mon << " " << year;
   else if(format == 2)
       str << month << "-" << day << "-" << year;
   else if(format == 3)
       str << month << "/" << day << "/" << year % 1000;
      
   return str.str();
}

main.cpp

#include "SaveADate.h"

#include <iostream>

using namespace std;

// main function

int main() {
  
   // Creating SaveADate object
  
   SaveADate d;
   d.setDay(12);
   d.setYear(2013);
   d.setMonth(3);
  
   cout << "Showing date according to all formats" << endl;
  
   cout << "format 0: " << d.getData(0) << endl;
   cout << "format 1: " << d.getData(1) << endl;
   cout << "format 2: " << d.getData(2) << endl;
   cout << "format 3: " << d.getData(3) << endl;
}

Sample Output:-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
For this assignment, you will create a hierarchy of five classes to describe various elements of...
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment. Person Variables: String firstName - Holds the person's first name String lastName -...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a...
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a class in C++; 3. Operator overloading with chaining; 4. Preprocessor directives #ifndef, #define, and #endif; 5. this – the pointer to the current object. In this assignment you will implement the Date class and test its functionality. Consider the following class declaration for the class date: class Date { public: Date(); //default constructor; sets m=01, d=01, y =0001 Date(unsigned m, unsigned d, unsigned y);...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
C++ For this assignment, you will create a search benchmark, that is a comparison between the...
C++ For this assignment, you will create a search benchmark, that is a comparison between the Linear and Binary Search algorithm on the same list of names provided in a text file. 1. Ask the user to enter a filename 2. Ask the user to enter a name; this is the search item. 3. Loop until you reach the end of the file and read all the names and store them in an array. Ensure that the array is large...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT