Question

In: Computer Science

C++ Start with your Date class in the Date.cpp file (from Date01B assignment or whatever) Name...

C++

  1. Start with your Date class in the Date.cpp file (from Date01B assignment or whatever)
  2. Name the new Date file Date03.cpp
  3. Add the following constructors to the date class
    1. create a constructor that takes 3 integers
      1. in the order month, day, year
      2. assigns the month, day, year parameters to the corresponding data items.
      3. Use the ‘setter’ to assign the values
      4. Add a line cout << “in constructor with 3 ints\n” in the constructor
    2. a 'default' constructor - no arguments
      1. Add a line cout << “in default constructor \n” in the constructor
      2. using the ‘setters’ assign
        1. 1 to the month
        2. 1 to the day
        3. 1900 to the year
      3. Add a line cout << “in destructor \n”

  1. Create your test plan. This test plan is simple. Just list a way to test each constructor
  2. Upload this test plan document
  3. Code and execute your test plan
    1. Create a function named void testDate03()
    2. The function should be listed after main
    3. Put the code the test of your test plan here in this function
    4. Call this function from mail to test your new functions
    5. I would not necessarily read in a month, day, and year from the keyboard but hard code your numbers
  4. up load the .cpp file
  5. upload the test plan document

discussion topic has been created

/**************************************************
*
*      program name:       Date01
*      Author:            
*      date due:            10/19/20
*      remarks:
*
***************************************************/

/******************************************
*     library includes
******************************************/
#include <iostream>           // needed for cin and cout

/******************************************
*     pre-processor
******************************************/
#define PI 3.14159
using namespace std;

class Date
{
    int month;
    int day;
    int year;
public:
    int getMonth(){
        return month;
    }
    int getDay(){
        return day;
    }
    int getYear(){
        return year;
    }
    void setMonth(int month){
        this->month=month;
    }
    void setDay(int day){
        this->day=day;
    }
    void setYear(int year){
        this->year=year;
    }
    void display();
};

void Date::display(){
    cout<<endl;
    cout<<"month is "<<getMonth()<<endl;
    cout<<"day is "<<getDay()<<endl;
    cout<<"year is "<<getYear()<<endl;
    cout<<endl;
}
/*****************************************
*   main() - the function that executes
*****************************************/
int main()
{
    Date date;
    date.setMonth(10);
    date.setDay(19);
    date.setYear(2020);
    date.display();

    system("pause");
    return 0;
}

Solutions

Expert Solution

Date03.cpp

#include<iostream> // header file used for cout and cin

using namespace std;

void testDate03(); // function prototype/declaration for testDate03

class Date // class Date

{

private: // private access specifiers. declaring 3 variables that will be used in the program, month, day and year

int month;

int day;

int year;

public: // public access specifiers to be used by the function

Date(int month,int day,int year) // parameterised constructor. the parameters are month,day and year

{

setMonth(month); // Inside the constructor we are calling the setters/mutators of each variable. passing the variable data we received as a parameter

setDay(day); // to each of the function.

setYear(year); // printing the message that we are in a constructors with 3 ints

cout<<"In constructor with 3 ints \n";

}

Date() // default constructor. no parameters are used

{

cout<<"In default constructor \n"; // print a message that we are in default constructor

setMonth(1); // call the setters/mutators of all the variables. pass 1 for setMonth, 1 for setDate and 1900 for setYear

setDay(1);

setYear(1900);

}

void setMonth(int month) // setter for month it will take 1 parameter and assign to the global variable month.

{

this->month=month;

}

void setDay(int day) // setter for day it will take 1 parameter and assign to the global variable day.

{

this->day=day;

}

void setYear(int year) // setter for month it will take 1 parameter and assign to the global variable month.

{

this->year=year;

}

~Date() // destructor. It will print a message that we are in the destructor. it will be called when we get out of scope of a class.

{

cout<<"In destructor\n";

}

};

int main() // main()

{

testDate03(); // this will call the testDate03()

}

void testDate03() // definition of testDate03()

{

Date d1(10,25,2018); // object of Date class with name d1. as we can see parameters are passed so the parameterised constructors are called.

Date d2; // another object of Date class. as we can see no parameters are passed so the default constructor is called.

}

Output :

Test plan:

The class that we are using in the program, Date class is having 2 constructors.

1) The parameterised constructor:

Date(int month,int day,int year) // parameterised constructor. the parameters are month,day and year

{

setMonth(month); // Inside the constructor we are calling the setters/mutators of each variable. passing the variable data we received as a parameter

setDay(day); // to each of the function.

setYear(year); // printing the message that we are in a constructors with 3 ints

cout<<"In constructor with 3 ints \n";

}

The constructor is having parameters that is why it is a parameterised constructor.

2) default constructor:

Date() // default constructor. no parameters are used

{

cout<<"In default constructor \n"; // print a message that we are in default constructor

setMonth(1); // call the setters/mutators of all the variables. pass 1 for setMonth, 1 for setDate and 1900 for setYear

setDay(1);

setYear(1900);

}

As there are no parameters in the constructor that is why this is a default constructor.

This class may have more than 1 parameters but when the object of the class is created. The object will contain only 1 constructor and only one will be called.

So as there are 2 constructors in the class. Our test plan includes 2 objects must be created. One object will invoke the parameterised constructor and another object will invoke the default constructor.

So our first object will be:

Date d1(10,25,2018);

Here we are creating an object of class Date with name d1. and when we pass parameters to the objects it will invoke the parameterised constructor. so we are passing 10 as month, 25 as day and 2018 as the year to the constructor.

2nd object will be:

Date d2;

here we are dicrectly creating another object of class Date with no parameters so it will call the default constructor.

these 2 objects will be created in the functions testDate03().

============================================================================

Description:

Constructor:

constructor is a function. whose name and class name are same. constructors are used to initialize values. constructors do not have a return type. constructors are always public. constructors are called when an object is created.

types of constructors used in our program:

1) default constructors: constructors with no parameters

Date()

2) parameterized constructors: constructors with parameters

Date(int month,int day,int year)

_________________________________________

Destructor: A function used to release the memory of the variables used in the function is a destructor. A destructor is called when a function which created object goes out of scope. In our program when testDate03 goes out of scope.

_____________________________________________

what is a function?

A piece of code that is stored independently from other functions or main(), and it can be called from any functions.

function in a program can be denoted in 3 ways.

1) function prototype: Before the definition of the function, we need to tell the compiler, what are the functions that we are using in the program, what are its parameter types and return type. this is done before the function definition but it is a good practice to provide function prototype/declaration at the start of the program.

void testDate03();

2) Function call: whenever a function is called for execution from the same function or different function, it is function call.

testDate03();

3) function definition: whenever we provide a body to the function or tell what the function will do then it is function definition.

void testDate03() // definition of testDate03()

{

Date d1(10,25,2018); // object of Date class with name d1. as we can see parameters are passed so the parameterised constructors are called.

Date d2; // another object of Date class. as we can see no parameters are passed so the default constructor is called.

}


Related Solutions

Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
/* * File: ListDriver.java * Assignment: Lab03 * Author: * Date: * Course: * Instructor: *...
/* * File: ListDriver.java * Assignment: Lab03 * Author: * Date: * Course: * Instructor: * * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use of "ListArrayBased" the project driver * class will populate the list, then invoke various operations * of "ListArrayBased". */ import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a...
b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java. c) Write separate methods to perform...
b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java. c) Write separate methods to perform arithmetic operations of sum, average, product, max and min of three integer numbers. These 5 methods should have three integer parameters to take the three integer values. They should be valuereturn methods, and return the arithmetic result in proper data type when they are invoked (called). The average method should return a double value, and all the other methods should return integer values. d)...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Name: Jessica Villasenor Date: June 14, 2020 Class: Principles of Microeconomics Professor: Priti Verma Assignment #4...
Name: Jessica Villasenor Date: June 14, 2020 Class: Principles of Microeconomics Professor: Priti Verma Assignment #4 1. Explain each of the following statements using supply-and-demand diagrams. a. “When a cold snap hits Florida, the price of orange juice rises in supermarkets throughout the country.” b. “When the weather turns warm in New England every summer, the price of hotel rooms in Caribbean resorts plummets.” c. “When a war breaks out in the Middle East, the price of gasoline rises and...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario:...
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario: Please write a few lines describing your scenario and the four variables (in addition to income) you have selected. Use Table 1 to report the variables selected for this assignment. Note: The information for the required variable, “Income,” has already been completed and can be used as a guide for completing information on the remaining variables. Table 1. Variables Selected for the Analysis Variable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT