You are on page 1of 3

Compiling in C++

• File names:
source files: <filename>. cpp OR <filename>.C
header files: <filename>.h
More C++
• Specify the gnu C++ compiler (in your Makefile):
Marge Coahran CC = g++
Some material from:
Kip Irvine, “C++ and Object-Oriented • To include standard libraries in your code:
Programming” #include <iostream > (iostream.h gives a warning)
using namespace std;

Public, protected, private Accessing protected from subclass


class Publication {
• Public: accessible from anywhere. Protected:
int year;
}
• Protected: accessible from class member class Book : Public Publication {
Public: Book(int yr);
functions and subclass member functions. }
Book::Book(int yr) {
year = yr; // This works, but may not be good engineering.
}
• Private: accessible only from class
• Need to do data validation in subclass and in base class.
member functions (not from subclass). • May need to change subclass code if base class implementation
changes.

Better: access via public members Pass by Reference


class Publication {
Public:
void swap(int & x, int & y) {
int getYear(void); int temp = x;
void setYear(int yr); // do data validation here (and only here) x = y;
Private:
int year; y = temp;
} return;
class Book : Public Publication {
Public: Book(int yr); }
}

Book::Book(int yr) { • Inside the function, we can access the original


setYear(yr ); argument (not a copy) by name (not a pointer).
}

1
Syntax (pass by reference) Why pass by reference?
• Prototype:
void swap(int & x, int & y);
• Allows the function to change parameter
values
• Function definition:
void swap(int & x, int & y) {
int temp = x; • Easier than passing and dereferencing
x = y;
y = temp; pointers, as we needed to in c
return;
}

• Function call: • Does not make a copy of the passed


swap(x, y) object. (Parameters can be large objects.)

Constant reference parameters “Bit by bit” copying


• What if… • What happens when objects are assigned to one
another?
– we want to save space (so don’t want to copy the
passed object)
Ship s1, s2;
– but we don’t want the function to change the passed s2 = s1;
object
• Data members are copied “bit by bit” from one object to
the other. Internally, this happens:
void display(const Table & bigTable) {
cout << bigTable.variable << endl; // OK s2.x = s1.x;
bigTable.variable = 2; // ERROR s2.y = s1.y;
return; s2.shield = s1.shield;
etc.
}

“Bit by bit” copying (II) “Bit by bit” copying (III)


• Similarly, what happens when objects are copied? • Bit by bit copy is “shallow.” Only the pointer is copied, not the
dynamic array.
• For example, when they are passed by value…
class PriceList {
float Ship::DistanceToShip(Ship s2) { private:
// compute distance from this ship to ship s2 float* price_list; //pointer to dynamically allocated array
} int item_count;
}
• Internally, this happens: // in main:
– a new object is instantiated (constructed) PriceList p1, p2; // the constructors allocate dynamic arrays
– data members are copied into it bit by bit p2 = p1; // assign p1 to p2

• This is ok if the object has no data members that are • Now p1 and p2 point to a single copy of the array (danger!), and
pointers to dynamically allocated memory. But… second array is lost (memory leak!).

2
“Canonical” Classes Canonical Classes (II)
• Classes that use dynamic memory allocation class PriceList {
always need: public:
– constructor PriceList();
PriceList(const PriceList & P); // copy constructor
– destructor
~PriceList();
– copy constructor
PriceList & operator = (const PriceList & P);
– assignment operator private:
float* price_list; //points to list of prices
• These member functions make classes robust. int item_count ;
• Classes with these are called canonical classes. }

Copy Constructors Assignment operators


PriceList& PriceList::operator = (const PriceList & P) {
PriceList::PriceList(const PriceList & P) {
if (this != &P) {
// copy individual data members // copy individual data members
item_count = P.item_count;
item_count = P.item_count;
// free old array
delete [] price_list;
//allocate space and copy array items //allocate space and copy array items
price_list = new float[item_count]; price_list = new float[item_count];
for (int i=0; i<item_count; i++)
for ( int i=0; i<item_count; i++) price_list[i] = P.price_list[i];
price_list[i] = P.price_list[i]; }
return *this;
} }

You might also like