In: Computer Science
C++
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; }
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.
}