Sunday, September 12, 2010

C++: Pointer exercise 1

Before I dive into rants on memory allocation, I thought I should pose a
small challenge for you - to make sure that you haven't forgotten about
working with basic classes:
This exercise is divided into two parts:
1. Create a computer class to store two string variables (company name and
model), as well as a double for storing CPU speed (in gigahertz (GHz). The
default constructor would set the computer model and brand name to null
string and CPU speed to 0. The parameterized constructor would assign
variables data from user-supplied arguments. That's it for now.
2. Create a computer variable named "my_computer" and a computer pointer
called "computer_ptr" that'll point to the address of my_computer. Then
create another computer object called "Apple", this time assign it the name
"Apple (brand name) Mac Book Pro (model)" and set the CPU to 2.13 GHz. Then
assign a new computer pointer called "Apple_ptr" to our Apple computer
object. Then assign computer_ptr pointer so that it now points to apple_ptr.
We'll revisit our Apple notebook later when discussing about displaying it
as part of a computer store shelf i.e. memory arithmetic and how to use
this-> operator.
// JL

No comments:

Post a Comment