Quiz 4: Standard I - O and String Classes

Computing

Problem Plan: • Declaration of class with the name "Date" o The private access specifier is declared for the required variables o The public access specifier is declared for specified member • The main operation is performed • Implementation of the constructor class • "print1()" function is performed for various months o Switch case operations are performed for various months Program: / / Required header files are included #include #include using namespace std; / / Declare a class Date class Date { / *The required variables are declared in private access specifier* / private: int month; int day; int year; / / The date is declared in public access specifier public: Date(); Date(int month,int day,int year); / / Print1() function to display the output void print1(); }; / / The main operation is performed int main() { / / The class Date is tested here Date currentDate; / / It prints the specified date currentDate.print1(); / / assign return zero return 0; } / / Implementation of the class with the constructor Date::Date() { month = 12; day = 14; year = 2017; } / / the function print1 is performed here void Date::print1() { / / the variable mon is declared in string string mon; / / switch case operation is performed for month switch(month) { / / Case for January month case 1: mon="Jan"; break; / / Case for February month case 2: mon="Feb"; break; / / Case for March month case 3: mon="Mar"; break; / / Case for April month case 4: mon="Apr"; break; / / Case for May month case 5: mon="May"; break; / / Case for June month case 6: mon="Jun"; break; / / Case for July month case 7: mon="Jul"; break; / / Case for August month case 8: mon="Aug"; break; / / Case for September month case 9: mon="Sept"; break; / / Case for October month case 10: mon="Oct"; break; / / Case for November month case 11: mon="Nov"; break; / / Case for December month case 12: mon="Dec"; break; } / / It prints the specified month,day and year cout "The month,day and year is\n"; cout } Sample Output: The month,day and year is Dec-14-2017

Problem Plan: PhoneNo.cpp: • Constructor is created for "PhoneNo()" and the required variables are assigned. • Calling of three parameterized constructor. • The function is declared for areaCode in which it returns the areaCode value • The function is declared for localExchange in which it returns the localExchange value • The function is declared for number in which it returns the number PhoneNo.h: • Declaration of the class "PhoneNo" • Constructor is created for "PhoneNo" • Declare the required member function as public • Declare the required member variables as private Main.cpp: • Main function is created. • Object is declared for the class "PhoneNo" • Function call is made for "printPhoneNo()" Program: PhoneNo.cpp: / / include the required header files #include "PhoneNo.h" / / calling of the constructor PhoneNo::PhoneNo() { / / assign '123' to areaCode areaCode = 123; / / assign 'ABCD' to localExchange localExchange = "ABCD"; / / assign '987654321' to number number = 987654321; } / / calling of the 3 parameter constructor PhoneNo::PhoneNo(int area, string local, int no) { / / assign area to areaCode areaCode = area; / / assign local to localExchange localExchange = local; / / assign no to number number = no; } / / calling the destructor PhoneNo::~PhoneNo() {} / / calling the function 'setAreaCode' void PhoneNo::setAreaCode(int area) { / / assign area to areaCode areaCode = area; } / / calling the function 'setLocalExchange' void PhoneNo::setLocalExchange(string local) { / / assign local to localExchange localExchange = local; } / / calling the function 'setNumber' void PhoneNo::setNumber(int no) { / / assign no to number number = no; } / / calling the function 'getAreaCode' int PhoneNo::getAreaCode() { / / return areaCode return areaCode; } / / calling the function 'getLocalExchage' string PhoneNo::getLocalExchage() { / / return localExchange return localExchange; } / / calling the function 'getNumber' int PhoneNo::getNumber() { / / return number return number; } / / calling the function 'getNumber' void PhoneNo::printPhoneNo() { / / print the areaCode cout areaCode " "; / / print localExchange cout localExchange " "; / / print the number cout number endl; } PhoneNo.h: / / include the required header files #ifndef PHONENo_H #define PHONENo_H #include #include using namespace std; / / declaration of the class class PhoneNo { / / access specifier public: / / constructor with no parameters PhoneNo(); / / constructor with three parameters PhoneNo(int area, string local, int no); / / destructor ~PhoneNo(); / / declaration of the function 'setAreaCode()' void setAreaCode(int area); / / declaration of the function 'setLocalExchange()' void setLocalExchange(string local); / / declaration of the function 'setNumber()' void setNumber(int no); / / declaration of the function 'getAreaCode()' int getAreaCode(); / / declaration of the function 'getLocalExchage()' string getLocalExchage(); / / declaration of the function 'getNumber()' int getNumber(); / / declaration of the function 'printPhoneNo()' void printPhoneNo(); / / access specifier private: / * declaration of the variable 'areaCode' with integer datatype * / int areaCode; / * declaration of the variable 'localExchange' with string datatype * / string localExchange; / * declaration of the variable 'number' with integer datatype * / int number; }; #endif TestClasses.cpp: / / required header files are added #include "PhoneNo.h" #include #include using namespace std; / / main function int main() { / / object for the class PhoneNo number; / / display the below content cout "PhoneNumber with code,localexchange and number has been obtained: "; / / calls the function 'printPhoneNo()' number.printPhoneNo(); / / return 0 return 0; } Output: PhoneNumber with code,localexchange and number has been obtained: 123 ABCD 987654321

Problem Plan: Check.cpp • Constructor is created for "Check()" and the required variables are assigned. • Calling of two parameterized constructor such as row and column • Destructor is created for "Check()" • The function "setRow()" is created to assign the row as "r" • The function "setColumn()" is created to assign the row as "c" • "getRow()" function is called to return row value • "getColumn()" function is called to return column value • The function "printCheck()" is created to assign the row and column value Check.h • Declaration of the class "Check" • Constructor is created for "Check" • Declare the required member function as public • Declare the required member variables as private Main.cpp • Main function is created • Object is declared for the class "Check" • Function call is made for "printCheck()" Program: / ********************************************************** * Program to find the Check number * ********************************************************** / Check.cpp: / / include the required header files #include "Check.h" / / calling of the constructor Check::Check() { / / assign '2' to row row = 2; / / assign '3' to column column = 3; } / / calling of the 2 parameter constructor Check::Check(int r, int c) { / / assign r to row row = r; / / assign c to column column = c; } / / calling the destructor Check::~Check() {} / / calling the function 'setRow' void Check::setRow(int r) { / / assign r to row row = r; } / / calling the function 'setColumn' void Check::setColumn(int c) { / / assign c to column column = c; } / / calling the function 'getRow' int Check::getRow() { / / return row return row; } / / calling the function 'getColumn' int Check::getColumn() { / / return column return column; } / / calling the function 'printCheck' void Check::printCheck() { / / print the row cout row " "; / / print column cout column endl; } Check.h: / / include the required header files #ifndef CHECK_H #define CHECK_H #include #include using namespace std; / / declaration of the class class Check { / / access specifier public: / / constructor with no parameters Check(); / / constructor with two parameters Check(int row, int column); / / destructor ~Check(); / / declaration of the function 'setRow()' void setRow(int r); / / declaration of the function 'setColumn()' void setColumn(int c); / / declaration of the function 'getRow()' int getRow(); / / declaration of the function 'getColumn()' int getColumn(); / / declaration of the function 'printCheck()' void printCheck(); / / access specifier private: / *declaration of the variable 'row' with integer datatype* / int row; / *declaration of the variable 'column' with integer datatype* / int column; }; #endif Source.cpp: / / required header files are added #include "Check.h" #include #include using namespace std; / / main function int main() { / / object for the class Check number; / / display the below content cout "row and column is given as: "; / / calls the function 'printCheck()' number.printCheck(); / / system("pause"); return 0; } Sample Output: row and column is given as: 2 3