Explain how and where the following code meets the 13 criteria. 1.Create a progr
ID: 3830744 • 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
1) Creates a program that interacts with user: Here in your program there is function called userInteract in Main.cpp class which interacts with the user to input two variables.
3) Use at least one example of inheritance : Here we have two classes derivedRectangle and derivedTriangle which extends class baseShape and overrides its method findArea().
4) Use at least one pointer : There is a function called pointerTest in Main.cpp which is used to check pointer functionality and gets address of intVar.
5) .Use at least one pass by reference : In swapByReference function in Main.cpp call by reference has been used for input parameters first and second and swap the value which will be reflected in actual parameters using which this function has been called.
7) Use separate files for class definition, class function definition, cpp file that has main : Here we have classes.h which defined three classes and then we have classes.cpp which is used to define the functions inside those classes and then we have Main.cpp which includes classes.h and uses the class defined.
8)Use at least 2 loops : In function ArrayElements 2 for loops has been used , to initialise array elements and to perform operations on those arrayElements.
9) Use at least 2 selection statements : In function selection(), selection statement if , else has been used to identify checkVariable and perform action based on its value.
10) Include at least one array : In ArrayElements, array has been used.
11) Include a write to a file : In function fileOperations() defined in Main.cpp filewrite has been used , an object of ofstream to write content to file.
12) Include a read from file : In function fileOperation() defined in Main,.cpp infile has been used, an object of ifstream to read contents from file.
13) Include data validation : In function switchCase() inside Main.cpp , switch statement has been used to check whether its a valid input or not.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.