I have a final in c++ on wednseday and I need some help with the following revie
ID: 3583058 • Letter: I
Question
I have a final in c++ on wednseday and I need some help with the following review questions.
10. Extra Credit long answer— pick ONE of the following
A) Write how you might design a class that represents a student at Old Westbury. Identify a problem domain and identify why your choice of private variables to represent this student, fits the given problem domain. Finally, identify the member functions you would write to manipulate these private variables.
B) Pick an example, OUTSIDE of class, of a problem that you think has a recursive solution and outline how you might solve the problem with recursion.
//Please answer this questions as soon as possible
Explanation / Answer
Factorial Program in C++ using Classes and Objects
In the program below, We are using defining a class with name Factorial. Then we have declared f and n variables as int data type, We then define fact and display functions under the access specifier Public (That means these functions/variables can be used anywhere throughout the program). fact() function communicates with the class with the help of ‘::‘ -> scope resolution operator. We have defined objects in the main function, Objects can be referred as variables of a Structure.
#include<iostream.h>
#include<conio.h>
class factorial{
int f, n;
public:
void fact();
void display();
};
void factorial::fact()
{
f=1;
cout<<" Enter a Number:";
cin>>n;
for(int i=1;i<=n;i++)
f=f*i;
}
void factorial::display()
{
cout<<" Factorial of "<<n<<" is "<<f;
}
void main()
{
clrscr();
factorial ob;
ob.fact();
ob.display();
getch();
}
OUTPUT:
Enter a Number: 5
Factorial of 5 is 120
The bove program can also be solved without using recursion as in the program below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
#include<conio.h>
void main()
{
int f, n;
clrscr();
cout<<" Enter a Number:";
cin>>n;
f=1;
for(int i=1;i<=n;i++)
f=f*i;
cout<<" Factorial of "<<n<<" is "<<f;
getch();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
#include<conio.h>
void main()
{
int f, n;
clrscr();
cout<<" Enter a Number:";
cin>>n;
f=1;
for(int i=1;i<=n;i++)
f=f*i;
cout<<" Factorial of "<<n<<" is "<<f;
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.