Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain how and where the following code meets the 13 criteria. 1.Create a progr

ID: 3835106 • Letter: E

Question

Explain how and where the following code meets the 13 criteria.

1.Create a program that interacts with the user.

2.Use at least 3 objects (instances of different classes)

3.Use at least one example of inheritance

4.Use at least one pointer

5.Use at least one pass by reference

6.Use at least one overloaded function

7.Use separate files for class definition, class function definition, cpp file that has main

8.Use at least 2 loops

9.Use at least 2 selection statements

10.Include at least one array

11.Include a write to a file

12.Include a read from file

13. Include data validation.

File name: classes.h

#ifndef CLASSES_H

#define CLASSES_H

//Base class

class baseShape

{

protected:

int shapeWidth, shapeHeight;

//Public functions of the class

public:

//Class constructor

baseShape( int checkVariable, int h);

//Function to find the area of the shape

int findArea();

};

//Derived class inherited from base class baseShape

class derivedRectangle: public baseShape

{

public:

//Class constructor

derivedRectangle( int checkVariable, int h);

//derived redefinition of the function

int findArea ();

};

//Other derived class inherited from baseShape

class derivedTriangle: public baseShape{

public:

derivedTriangle( int checkVariable, int h);

//redefinition of findArea function

//function overloadin can be used here

int findArea ();

};

#endif

File Name: classes.cpp

#include "classes.h"

#include <iostream>

using namespace std;

baseShape::baseShape( int checkVariable, int h)

{

shapeWidth = checkVariable;

shapeHeight = h;

}

//Function to find the area of the shape

int baseShape:: findArea()

{

cout << "Base class area :" <<endl;

return 3;

}

derivedRectangle::derivedRectangle( int checkVariable, int h):

baseShape(checkVariable, h) { }

//derived redefinition of the function

int derivedRectangle::findArea ()

{

cout << "Derived class findArea :" <<endl;

return (shapeWidth * shapeHeight);

}

derivedTriangle :: derivedTriangle( int checkVariable, int h):baseShape(checkVariable, h) { }

//redefinition of findArea function

//function overloadin can be used here

int derivedTriangle::findArea ()

{

cout << "derivedTriangle class findArea :" <<endl;

return (shapeWidth * shapeHeight / 2);

}

File Name: Main.cpp

//Include the header fileData

#include <iostream>

#include <iomanip>

#include <fstream>

#include "classes.h"

//Using the namespaces

using std::setw;

using namespace std;

void userInteract ();

void pointerTest();

void selection();

void switchCase();

void ArrayElements();

void fileOPerations();

//Function to swap using pass by refference

void swapPassByReference(int *first, int *second)

{

int *temp;

*temp = *first;

* first = *second;

*second = *temp;

}

//function uses a pointer

void pointerTest()

{

int intVar;

cout << " Address of intVar variable: ";

cout << &intVar << endl;

}

void userInteract(){

float input1, input2, p;

cout << "Please Input two numbers: ";

cin >> input1 >> input2;

p = input1*input2;

cout << "Product of your inputs is = " << p;

}

//To use selection statements

void selection()

{

int checkVariable = 100;

if( checkVariable < 20 )

{

cout << "checkVariable is less than 20;" << endl;

}

else

{

cout << "checkVariable is not less than 20;" << endl;

}

cout << "value of checkVariable is : " << checkVariable << endl;

}

//To use array

void ArrayElements()

{

//declare the array         

int n[10];

//The for loops

for ( int itr = 0; itr < 10; itr++ )

{

n[itr] = itr + 100;

}

cout << "Element" << setw(13) << "Value is" << endl;

for ( int vi = 0; vi < 10; vi++ )

{

cout << setw( 7 )<< vi << setw( 13 ) << n[ vi ] << endl;

}

}

//Switch for select

void switchCase()

{

char mark = 'D';

switch(mark)

{

case 'A' :

cout << "Excellent!" << endl;

break;

case 'B' :

case 'C' :

cout << "Well done" << endl;

break;

case 'D' :

cout << "You passed" << endl;

break;

case 'F' :

cout << "Failed" << endl;

break;

default :

cout << "Invalid mark" << endl;

}

cout << "Your mark is " << mark << endl;

}

//Read the fileData

void fileOPerations() {

char fileData[100];

ofstream filewrite;

filewrite.open("file.txt");

cout << "File write" << endl;

cout << "Please input your name: ";

cin.getline(fileData, 100);

cin.getline(fileData, 100);

filewrite << fileData << endl;

cout << "Please input your age: ";

cin >> fileData;

cin.ignore();

filewrite << fileData << endl;

filewrite.close();

ifstream infile;

infile.open("file.txt");

cout << "File read" << endl;

infile >> fileData;

cout << fileData << endl;

infile >> fileData;

cout << fileData << endl;

infile.close();

}

//main function

int main() {

int a = 100;

int b = 200;

userInteract();

pointerTest();

//swapPassByReference(&a, &b);

cout<<"Now a ="<<a<<"and b = "<<b<<endl;

selection();

switchCase();

fileOPerations();

//baseShape baseShape(0, 0));

derivedRectangle rec(10,7);

derivedTriangle tri(10,5);

cout<<rec.findArea();

//baseShape = rec;

//baseShape.findArea();

//baseShape = tri;

cout<<tri.findArea();

//cout<<baseShape.findArea();

return 0;

}

Explanation / Answer

Criteria 1: Create a program that interacts with the user.

The progarammer has used a nice interactive design that initially asks the user to enter 2 numbers. After the user is done entering the numbers, the program displays their product.

The program successfully displays a number of results in a pre-defined and understandable format, prompting the user to enter data wherever needed and hiding the underlying complex implemetation details of the program, thereby, making it user-interactive as well as a user-friendly program.

Criteria 2: Use at least 3 objects (instances of different classes)

In the main() funtion in Main.cpp, an instance is declared for each of the classes :

The instances call their argumented constructor as soon as they are created. The instances are later used to invoke different methods of the respective classes.

Criteria 3: Use at least one example of inheritance

In the classes.h, we can see not just one, but two examples of inheritence:

The two classes have inherited the class baseShape so as to use the function findArea() of the base class

Criteria 4: Use at least one pointer

In the function pointerTest() in Main.cpp, pointer is used to display the address of an integer variable in the memory.

Pointers are a special kind of variables that store the addresses of other variables.

Criteria 5: Use at least one pass by reference

In Main.cpp , the function swapPassByReference() uses call by referrance to swap the values of two variables.

In Call by Referrance, the address of the actual parameters are passed to the formal parameters. Whatever changes made to the formal parameters inside the called funtion, are automatically reflected back to the actual parameters.

Criteria 6: Use at least one overloaded function

C++ allows you to specify more than one definition for a function name in the same scope, which is called function overloading.

If we look at classes.h, the function findArea() is being redefined in each class it is inherited in. This shows a case of Function Overloading.

Criteria 7: Use separate files for class definition, class function definition, cpp file that has main

It is a good design principle to separate different functionalities in different files. For example, in the above program,

Criteria 8: Use at least 2 loops

If we look closely at the ArrayElements() function in the Main.cpp, the method uses two for loops:

Criteria 9: Use at least 2 selection statements

A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not. The following keywords are used in selection statements: if. else. switch.

Following are the different selection statements used in the above program:

Criteria 10: Include at least one array

An Array is a collection of elements having same data type and are kept under a common name.

In the above program, a method ArrayElements() in Main.cpp, is used to declare an array named n that holds 10 integer type variables

Criteria 11: Include a write to a file

In the Main.cpp, in Method fileOperations(), an ofstream instance of the fstream class (an in-built class in c++), is used to write the data to the file named "file.txt". The program asks the user to enter their name and age, and then writes this data to the above mentioned file by using the following statement

Criteria 12: Include a read from file

In the Main.cpp, in Method fileOperations(), an ifstream instance of the fstream class (an in-built class in c++), is used to read the data from the same file that was written earlier, i.e., "file.txt". The program reads ther name and age of the user from the file using the following statement:

Criteria 13: Include data validation

I'm sorry but i couldn't find any legitimate data validation statements used in this program.

Hope this helps...

BEST WISHES!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote