C++ Programming Exercises: Structures, Functions, and Classes
1. Working with Structures
1a. Declaring a Structure
Declare a structure named Organization
containing:
- a string named
name
- an int named
seats
struct Organization
{
string name;
int seats;
};
1b. Using the Structure
In the main
function, create a variable named x
of type Organization
. Assign “Opera San Jose” to the string and 1000 to the int in x
.
int main(void)
{
// put your code here
Organization x;
x.name = "Opera San Jose";
x.seats = 1000;
// code to process the structure is omitted
return 0;
}
2. Modifying Values with Functions
Given the following code:
int main(void)
{
double x = 3.5;
fn1(x);
cout << x << endl;
return 0;
}
Write the function fn1
to quadruple the value of x
in main
, so that when x
is printed in main
, it prints 14.
void fn1(double & num)
{
num *= 4;
return;
}
3. Dynamic Memory Allocation with Functions
Given the following code:
int main(void)
{
double * ptr;
const int SIZE = 6;
fn2(ptr, SIZE);
// code omitted here that processes an array in the heap
delete [] ptr;
return 0;
}
Write the function fn2
to get space for an array of double values in the heap of size given by the constant SIZE
and make the variable ptr
in main
point to the array in the heap.
void fn2(double *& ptr, int size)
{
ptr = new double[SIZE];
return;
}
4. Defining a Class: ArrayOfThree
Write a complete program that includes the following:
#include <iostream>
using namespace std;
/* declare a class named ArrayOfThree containing:
- A static const int size for an array set equal to three
- An array of int of size three
Write the following function definitions within the class:
- Member functions:
- default constructor to set the values to zero
- setValues function to set the three values
- getAverage function that returns a double, the average of the three values
- destructor
*/
class ArrayOfThree
{
private:
static const int SIZE = 3;
int arr[SIZE];
public:
ArrayOfThree()
{
arr[0] = 0;
arr[1] = 0;
arr[2] = 0;
}
void setValues(int a, int b, int c)
{
arr[0] = a;
arr[1] = b;
arr[2] = c;
}
double getAverage() const
{
double ave;
ave = (arr[0] + arr[1] + arr[2]) / 3.0;
return ave;
}
~ArrayOfThree()
{
// Destructor (no specific action needed in this case)
}
};
/* write a main function that:
- gets space in the heap for an object that is a member of the class ArrayOfThree
- sets the values in this object to 2, 4, and 5
- gets their average and prints it
- gives the space back to the heap
- returns control to the operating system
*/
int main()
{
ArrayOfThree * ptr;
ptr = new ArrayOfThree;
ptr->setValues(2, 4, 5);
cout << ptr->getAverage() << endl;
delete ptr;
return 0;
}
5. Class with Copy Constructor
This problem has several parts that together make a complete program.
5a. Defining Class A
#include <iostream>
#include <cmath>
using namespace std;
class A
{
double exponent;
double base;
public:
void setExponent(double e) { exponent = e; }
void setBase(double b) { base = b; }
double value()
{
double result;
if (base == 0.0) result = 0.0;
else if (exponent == 0.0) result = 1.0;
else result = pow(base, exponent);
return result;
}
// Default constructor
A()
{
exponent = 0.0;
base = 0.0;
}
// Copy constructor prototype
A(const A& oldA);
};
5b. Defining the Copy Constructor
A::A(const A& oldA)
{
exponent = oldA.exponent;
base = oldA.base;
}
5c. Main Function
int main(void)
{
A x;
x.setExponent(2.0);
x.setBase(3.0);
A y(x); // Using the copy constructor
cout << y.value() << endl;
return 0;
}