RAW Pointers

The older RAW Pointers

main.cpp
// Project 08-01
// Name: Dynamically Create Rectangles

#include <iostream>
#include "Rectangle.h"
using namespace std;

int main() {

   /*
   Rectangle* myArray[3];

   
   Rectangle* rec1 = new Rectangle(3,4);
   Rectangle* rec2 = new Rectangle(5,5);
   Rectangle* rec3 = new Rectangle(1, 6);
   
    myArray[0] = rec1;
    myArray[1] = rec2;
    myArray[2] = rec3;
    
    */
    
    const int ARR_SIZE = 3;
    
    Rectangle* myArray[ARR_SIZE];
    
    myArray[0] = new Rectangle(5, 3);
    myArray[1] = new Rectangle(20, 40);
    myArray[2] = new Rectangle(2, 10);
    

    for (int i = 0; i < 3; i++)
    {
        cout << "Rectangle " << i+1 << endl << "area: ";
        cout << myArray[i]->area() << endl;
        cout << "Perimeter ";
        cout << myArray[i]->perimeter() << endl << endl;
    }
    
    /*
    delete rec1;
    delete rec2;
    delete rec3;

    rec1 = nullptr;
    rec2 = nullptr;
    rec3 = nullptr;
    */
    
    // delete
    for (int i = 0; i < ARR_SIZE; i++)
    {
    	delete myArray[i];
    	myArray[i] = nullptr;
    	}

    return 0;
}
Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
	public:
		Rectangle();
		Rectangle(double length, double width);
		double getLength() const;
		double getWidth() const;
		void setLength(double length);
		void setWidth(double width);
		double area() const;
		double perimeter() const;

	private:
		double length;
		double width;
};
#endif 
Rectangle.cpp
#include "Rectangle.h"

Rectangle::Rectangle()
{
	this->length = 1.0;
	this->width = 1.0;
}

Rectangle::Rectangle(double length, double width)
{
	this->length = length;
	this->width = width;
}

double Rectangle::getLength() const
{
	return length;
}

double Rectangle::getWidth() const
{
	return width;
}

void Rectangle::setLength(double length)
{
	this->length = length;
}

void Rectangle::setWidth(double width)
{
	this->width = width;
}

double Rectangle::area() const
{
	return length * width;
}

double Rectangle::perimeter() const
{
	return 2 * length + 2 * width;
	//return 2 * (length + width)
}

===============================

main.cpp
// Project 08-02
// Name: Dynamically Creating Circles

#include <iostream>
#include "Circle.h"
using namespace std;

void printCircles(Circle** circleArray, int numCircles);

int main()
{

	cout << "How many circles do you want to create? ";
	unsigned numCircles;
	cin >> numCircles;

	Circle **myCircles = new Circle *[numCircles];          // Dynamic array of dynamic pointers

	for (size_t i = 0; i < numCircles; i++) {
		double radius = { };
		cout << "What radius for circle " << i << ": ";
		cin >> radius;
		myCircles[i] = new Circle(radius);
	}
	for (size_t i = 0; i < numCircles; i++) {
		cout << '\t' << myCircles[i]->circumference() << endl;
		cout << '\t' << myCircles[i]->area() << endl << endl;

	}

	for (size_t i = 0; i < numCircles; i++) {
		delete myCircles[i];
		myCircles[i] = nullptr;
	}
	delete[] myCircles;
	myCircles = nullptr;



	return 0;
}

void printCircles(Circle** circleArray, int numCircles)
{
    for (int i = 0; i < numCircles; i++)
    {
        cout << "Circle " << i << endl;
        cout << "\t" << circleArray[i]->circumference() << endl;
        cout << "\t" << circleArray[i]->area() << endl;
        cout << endl;
    }
}
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H


class Circle
{
	public:
		Circle();
		Circle(double radius);
		double getRadius() const;
		void setRadius(double radius);
		double circumference() const;
		double area() const;

	private:
		double radius;
		const double MY_PI;
};
#endif 
Circle.cpp
#include "Circle.h"
#include <cmath>
using namespace std;

//const double MY_PI = 3.14159;

Circle::Circle() : MY_PI(3.14159)
{
	radius = 1;
}

Circle::Circle(double radius) : MY_PI(3.14159)
{
	this->radius = radius;
}

double Circle::getRadius() const
{
	return radius;
}

void Circle::setRadius(double radius)
{
	this->radius = radius;
}

double Circle::circumference() const
{
	//C = 2 * pi * r
	return 2 * MY_PI * radius;
}

double Circle::area() const
{
	//A = pi * r^2
	return MY_PI * pow(radius, 2);
}