Week 5: Pointers, Objects and Classes

 

Pointers Review

 

The & operator indicates the variable's address

It is usually in Hex number

// address.cpp _ using the & operator to find addresses
#include <iostream.h>
int main(void)
{
	int donuts = 6;
	double cups = 4.5;

	cout << "donuts value = " << donuts;
	cout << " and donuts address = " << &donuts << "\n";

	cout << "cups value = " << cups;
	cout << " and cups address = " << &cups << "\n";
	return 0;
}
// Output:
// donuts value = 6 and donuts address = 0x8566fff4
// cups value = 4.5 and cups address = 0x8566ffec

 

 

Pointer Review Part II

 

A computer needs to keep track of the type of value a pointer refers to

The address of char looks the same as the address a double (both are just hex number)

For example:

int * p_updates;

States the combination * p_updates is type int.

* operator is used by applying it to a pointer, the p_updates variable itself must be a pointer

We say p_updates points to type int

We can also say, p_updates is pointer-to-int or int *

p_update is a pointer (an address)

*p_update is an int

int *ptr;			/* The C way */
int* ptr;			/* The C++ way */

 

 

Pointer Review Part III

 

// init_ptr.cpp -- initialize a pointer
#include <iostream.h>
int main(void)
{
	int higgens = 5;
	int * pi = &higgens;

	cout << "Value of higgens = " << higgens
	  << "; Address of higgens = " << &higgens << "\n";
	cout << "Value of *pi = " << *pi
		 << "; Value of pi = " << pi << "\n";
	*pi = 10;
	cout << "higgens " << higgens << "*pi" << *pi << end; 
	return 0;
}
// Value of higgens = 5; Address of higgens = 0x8fa0fff4
// Value of *pi = 5; Value of pi = 0x8fafff4
// higgens 10 *pi 10

 

Allocating Memory with the new operator

 

When we use the following:

int higgens;
int *pi = &higgens;

Memory actually get allocated when the "int higgen" statement is executed

If we want to init pointer from ground up, we have to use the new operator:

int* pi = new int;
*pi = 1001;
cout << "The number is " << *pi << endl;

 

Pointers and Separate Compile Example

 

week4fr.hpp

week4fr.cpp

week4a.cpp

week4a.out

 

week4b.cpp

week4b.out

 

 

 

The name of this class is OOP using C++

 

 

 

 

Procedural and Object-Oriented Programming

 

The week4fr.hpp example we have is a classic example of procedural programming. We first concentrate upon the procedures we would follow, then think about how to represent the data. And the data structure(n and d and stuffs) is visible to the outside world

In OOP, we should first think about the data.

We would concentrate upon the object as the user perceives it

Think about the data needed to describe the object and about the operations that will describe the user's interaction with the data

 

 

Abstruction and Classes

 

Class is the second OO concept we have, the first one is function overloading.

In computing, abstraction is the crucial abstract the essential operational features of a problem and express a solution in those terms.

OSI Network model as an example

From abstraction, it is a short step to the user-defined type, which in C++ is a class design that implements that interface (We will have an example for demo)

 

 

The Class

 

It determines how much memory will be needed for a data object. (Automatic when you declare it correctly)

It determines what operations, or methods, can be performed using the data object

The class is the C++ vehiclefor translating an abstraction to a user-defined type. It combines data representation and methods for manipulating that data into one neat package.

Class is like a template for making cookie, and object is like the actually cookie

 

Making a Class and Using it

 

// stocks.cpp
#include <iostream.h>
#include <stdlib.h>	// for exit()
#include <string.h> // for strcpy()

class Stock
{
private:
	char company[30];
	int shares;
	double share_val;
	double total_val;
	void set_tot() { total_val = shares * share_val; }
public:
	void acquire(const char * co, int n, double pr);
	void buy(int num, double price);
	void sell(int num, double price);
	void update(double price);
	void show();
};


void Stock::acquire(const char * co, int n, double pr)
{
	strcpy(company, co);
	shares = n;
	share_val = pr;
	set_tot();
}

void Stock::buy(int num, double price)
{
	shares += num;
	share_val = price;
	set_tot();
}

void Stock::sell(int num, double price)
{
	if (num > shares)
	{
		cerr << "You can't sell more than you have!\n";
		exit(1);
	}
	shares -= num;
	share_val = price;
	set_tot();
}

void Stock::update(double price)
{
	share_val = price;
	set_tot();
}

void Stock::show()
{
	cout << "Company: " << company
		<< "  Shares: " << shares << '\n'
		<< "  Share Price: $" << share_val
		<< "  Total Worth: $" << total_val << '\n';
}

int main(void)
{
	Stock stock1;

	stock1.acquire("NanoSmart", 20, 12.50);

	// Setting the print out format, Optional
	cout.precision(2);			// #.## format
	cout.setf(ios::fixed);		// #.## format
	cout.setf(ios::showpoint);	// #.## format


	stock1.show();
	stock1.buy(15, 18.25);
	stock1.show();
	return 0;
}

 

Class Constructor and Destructors

 

Special method call constructor and destructor should be provided for a class

If not provided, C++ will provide a default constructor and destructor (which just do nothing but initization or basic clean up)

Constructor is used to construct (create) an object, normally programmer will init all the necessary variable in the constructor

Destructor is used to clean up when an object is no long in use

Example:
// Constructor Example
Stock::Stock()
{
	strcpy(company, "no name");
	shares = 0;
	share_val = 0.0;
	total_val = 0.0;
}

// Destructor
Stock::~Stock()
{
	cout << "Bye, " << company << endl;
}


... // In another program ...

// Init an object

Stock stock1();

// When program exit, destructor get call automatically
// More constructor example later ...

 

Classes and Object Lab

 

Fraction.hpp (class definition)

Fraction.cpp (class implementation)

week5.cpp (source code)

week5.out (output)

 

 

Copyright 1996-2001 OpenLoop Computing. All rights reserved.