Object Oriented Programming: CS162 Instructor: Maria Sabir: Lab 2-Solution
Object Oriented Programming: CS162 Instructor: Maria Sabir: Lab 2-Solution
class StockPurchase
{
public:
StockPurchase(const double newPrice)
{
// When someone buys a stock, modify the default price according to
//how much they were willing to pay
previousDefaultPrice = defaultPrice;
currentPrice = newPrice;
defaultPrice = previousDefaultPrice + (currentPrice -previousDefaultPrice) / 2.0;
timeLastChecked = time(0);
// record the current timestamp
}
static const double getDefaultPrice()
{
return previousDefaultPrice + (currentPrice -previousDefaultPrice) / 2.0;
}
// Returns the current time, updating the timestamp to show that this object was read
recently
const double getCurrentPrice() const
{
timeLastChecked = time(0);
return currentprice;
}
private:
double previousDefaultPrice, currentPrice;
static double defaultPrice;
time_t timeLastChecked;
// A time_t is basically just an integer
}
CORRECT CODE:
#include<time.h>
class StockPurchase
{
public:
StockPurchase(const double newPrice)
{
// When someone buys a stock, modify the default price according to how much
they were willing to pay
previousDefaultPrice = defaultPrice;
currentPrice = newPrice;
defaultPrice = previousDefaultPrice + (currentPrice -previousDefaultPrice) / 2.0;
timeLastChecked = time(0);
// record the current timestamp
}
double getDefaultPrice() const
{
return previousDefaultPrice + (currentPrice -previousDefaultPrice) / 2.0;
}
// Returns the current time, updating the timestamp to show that this object was read
recently
const double getCurrentPrice()
{
timeLastChecked = time(0);
return currentPrice;
}
private:
double previousDefaultPrice, currentPrice;
static double defaultPrice;
time_t timeLastChecked;
// A time_t is basically just an integer
};
double StockPurchase::defaultPrice =5;
2. Define an Array class that expands the functionality of C++ arrays. Show how you
would split your definition into an Array.h file and an Array.cpp file.
The class should contain one data member that is a pointer to a dynamically allocated
array of integers, and an integer that stores the current size of the array. The size integer
should be const – it should not be changeable after the class constructor is called.
The class should have 3 constructors: one that takes just a number of elements to
allocate (initializing them all to 0), another that takes a number of elements and an array
of initial elements, and a copy constructor that allocates a new array of the same size as
the Array that is being copied, and then copies the elements one by one.
Define 4 member functions: getLength to return the number of elements in the Array;
getElement to take an integer n and return a modifiable reference to the (n+1)th element
of the array; another getElement function that is declared const and returns a non-
modifiable reference; and a print function that takes a separator string and prints the
elements one by one separated by the separator string. (For instance,
myArray.print("\n") should print the array elements with newlines between each pair.)
Also define a destructor to deallocate the memory that was allocated to the internal array
when the Array object is destroyed.
Define a main function that will presumably be useful for testing purposes to try creating
a few Array objects and manipulating them.
SOLUTION:
Array.h
class Array
{
private:
int *ptr;
const int size;
public:
Array(int s): size(s)
{
ptr=new int[size];
for(int i=0;i<size;i++)
*(ptr+i)=0;
}
Array(int s, int arr[]): size(s)
{
ptr=new int[size];
for(int i=0;i<size;i++)
*(ptr+i)=arr[i];
}
Array(Array& obj): size(obj.size)
{
ptr=new int[size];
for(int i=0;i<size;i++)
*(ptr+i)=*(obj.ptr + i);
}
void showData()
{
for(int i=0;i<size;i++)
cout<<*(ptr+i);
cout<<endl;
}
~Array()
{
delete []ptr;
}
};
Array.cpp
#include<iostream.h>
#include"Array.h"
void main()
{
Array a1(10);
a1.showData();
int arr[5]={10,20,30,40,50};
Array a2(5,arr);
a2.showData();
Array a3(a1);
a3.showData();
Array a4=a2;
a4.showData();