C++ Programming Examples: 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)
{
  Organization x;
  x.name = "Opera San Jose";
  x.seats = 1000;

  // Code to process the structure is omitted

  return 0;
}

2. Modifying Variables 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;
}

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 allocate 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];
}

4. Defining a Class: ArrayOfThree

Write a complete C++ program that includes a class named ArrayOfThree with the following features:

  • A static constant integer SIZE set to 3.
  • An integer array of size SIZE.
  • A default constructor that initializes all array elements to zero.
  • A function setValues to set the three values of the array.
  • A function getAverage that returns the average of the three values as a double.
  • A destructor.

The main function should:

  • Create a dynamically allocated object of the ArrayOfThree class.
  • Set the values in the object to 2, 4, and 5.
  • Calculate and print the average of the values.
  • Deallocate the memory used by the object.

#include <iostream>
using namespace std;

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 = (arr[0] + arr[1] + arr[2]) / 3.0;
    return ave;
  }

  ~ArrayOfThree() {}
};

int main()
{
  ArrayOfThree * ptr = new ArrayOfThree;
  ptr->setValues(2, 4, 5);
  cout << ptr->getAverage() << endl;
  delete ptr;
  return 0;
}

5. Class with Copy Constructor: Class A

This problem consists of several parts that together form a complete C++ program. The program defines a class named A with a copy constructor.

5a. Class Definition

Given the following partial definition of 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;
  }

  // Complete the default constructor
  A()
  {
    exponent = 0.0;
    base = 0.0;
  }

  // Prototype declaration for the copy constructor
  A(const A& oldA);
};

5b. Copy Constructor Definition

Write the complete definition of the copy constructor that you declared within the class A.


A::A(const A& oldA)
{
  exponent = oldA.exponent;
  base = oldA.base;
}

5c. Main Function

Write the main function to create an object named x of the class A. Set its exponent to 2.0 and its base to 3.0. Make a copy of x named y using the copy constructor. Finally, print the value obtained from the value member function of the object y.


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;
}