Introduction To Classes: Programming Fundamentals 1
Introduction To Classes: Programming Fundamentals 1
INTRODUCTION TO CLASSES
Programming Fundamentals
Chapter 7
Classes
Information Hiding
Member functions
Dynamic Memory Allocation using new and
delete operators
Programming Fundamentals
Overview
CLASSES
Class definition
The most important feature of C++ programming is class
definition with the class keyword. You define classes the same
way you define structures.
Example:
class Time {
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
Programming Fundamentals
Instantiating an object
Programming Fundamentals
INFORMATION HIDING
Programming Fundamentals
Access Specifiers
Programming Fundamentals
Example
class Time {
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
Time
hour
minute
second
setTime()
printMilitary
pringStandard()
private
public
Programming Fundamentals
10
With large program, you need to ensure that you do not include
multiple instances of the same header file.
C++ generates an error if you attempt to compile a program that
includes multiple instances of the same header file.
Programming Fundamentals
11
Example:
#if !defined(TIME1_H)
#define TIME1_H
class Time {
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
#endif
Programming Fundamentals
12
MEMBER FUNCTIONS
Inline functions
Although member functions are usually defined in an
implementation file, they can also be defined in an interface
file. Functions defined in an interface file are called inline
functions.
Example:
Stocks
iNumShares
dPurchasePricePerShare
dCurrentPricePerShare
getTotalValue()
Programming Fundamentals
13
class Stocks {
public:
double getTotalValue(int iShares, double dCurPrice){
double dCurrentValue;
iNumShares = iShares;
dCurrentPricePerShare = dCurPrice;
dCurrentValue = iNumShares*dCurrentPricePerShare;
return dCurrentValue;
}
private:
int iNumShares;
double dPurchasePricePerShare;
double dCurrentPricePerShare;
};
Programming Fundamentals
14
Programming Fundamentals
15
// stocks.cpp
#include stocks.h
#include<iostream.h>
double Stocks::getTotalValue(int iShares, double dCurPrice){
double dCurrentValue;
iNumShares = iShares;
dCurrentPricePerShare = dCurPrice;
dCurrentValue = iNumShares*dCurrentPricePerShare;
return dCurrentValue;
}
void main(){
Stocks stockPick;
cout << stockPick.getTotalValue(200, 64.25) << endl;
}
Output of the above program:
12850
Programming Fundamentals
16
Programming Fundamentals
17
Access Functions
A set function can also translate between the form of data used
in the interface and the form used in the implementation.
A get function need not expose the data in raw format; rather,
it can edit data and limit the view of the data the client will see.
Programming Fundamentals
18
Programming Fundamentals
19
// time1.cpp
#include time1.h
#include <iostream.h>
..
void Time::setTime( int h, int m, int s ){
hour = ( h >= 0 && h < 24 ) ? h : 0;
minute = ( m >= 0 && m < 60 ) ? m : 0;
second = ( s >= 0 && s < 60 ) ? s : 0;
}
void Time::printMilitary(){
cout << ( hour < 10 ? "0" : "" ) << hour << ":"
<< ( minute < 10 ? "0" : "" ) << minute;
}
void Time::printStandard(){
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
.
Programming Fundamentals
20
Constructor Functions
class Payroll{
public:
Payroll( ){ // constructor function
dFedTax = 0.28;
dStateTax = 0.05;
};
private:
double dFedTax;
double dStateTax;
}
Programming Fundamentals
21
Programming Fundamentals
22
Example 7.3.3
#include <iostream.h>
#include <iomanip.h>
// class declaration section
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2001);
// constructor with default values
};
// implementation section
Date::Date(int mm, int dd, int yyyy)
//constructor
{
month = mm;
day = dd;
year = yyyy;
cout << "Created a new data object
with data values "
<< month << ", " << day << ", "
<< year << endl;
}
Programming Fundamentals
23
int main()
{
Date a;
// declare an object without parameters
Date b;
// declare an object without parameters
Date c(4,1,2002); // declare an object with parameters
return 0;
}
The output of the above program:
Created a new data object with data values 7, 4, 2001
Created a new data object with data values 7, 4, 2001
Created a new data object with data values 4,1, 2001
Programming Fundamentals
24
To destroy the object and free the space for this object you
must use the delete operator:
delete typeNamePtr;
Programming Fundamentals
25
For built-in data types, we also can use the new and delete
operators.
Example 1:
int *pPointer;
pPointer = new int;
Example 2:
delete pPointer;
Example 3: A 10-element integer array can be created and
assigned to arrayPtr as follows:
int *arrayPtr = new int[10];
This array is deleted with the statement
delete [] arrayPtr;
Programming Fundamentals
26
Programming Fundamentals
27
Example 7.4.1
#include<iostream.h>
void main( )
{
double* pPrimeInterest = new double;
*pPrimeInterest = 0.065;
cout << The value of pPrimeInterest is:
<< *pPrimeInterest << endl;
cout << The memory address of pPimeInterest is:
<< &pPrimeInterest << endl;
delete pPrimeInterest;
*pPimeInterest = 0.070;
cout << The value of pPrimeInterest is:
<< *pPrimeInterest << endl;
cout << The memory address of pPrimeInterest is:
<< &pPrimeInterest << endl;
}
Programming Fundamentals
28
Programming Fundamentals
29
Example 7.4.2
In the following program, we can create some objects of the
class Stocks on the stack or on the heap and then manipulate
them.
#include<iostream.h>
class Stocks{
public:
int iNumShares;
double dPurchasePricePerShare;
double dCurrentPricePerShare;
};
double totalValue(Stocks* pCurStock){
double dTotalValue;
dTotalValue = pCurStock->dCurrentPricePerShar*
pCurStock->iNumShares;
return dTotalValue;
}
Programming Fundamentals
30
void main( ){
//allocated on the stack with a pointer to the stack object
Stocks stockPick;
Stocks* pStackStock = &stockPick;
pStackStock->iNumShares = 500;
pStackStock-> dPurchasePricePerShare = 10.785;
pStackStock-> dCurrentPricePerShare = 6.5;
cout << totalValue(pStackStock) << endl;
//allocated on the heap
Stocks* pHeapStock = new Stocks;
pHeapStock->iNumShares = 200;
pHeapStock-> dPurchasePricePerShare = 32.5;
pHeapStock-> dCurrentPricePerShare = 48.25;
cout << totalValue(pHeapStock) << endl;
}
The output of the above program:
3250
9650
Programming Fundamentals
31
Note
32
A class can contain any C++ data type. Thus, the inclusion of a
pointer variable in a class should not seem surprising.
Example 7.5.1
#include <iostream.h>
#include <string.h>
// class declaration
class Book
{
private:
char *title; // a pointer to a book title
public:
Book(char * = NULL); // constructor with a default value
void showtitle();
// display the title
};
Programming Fundamentals
33
// class implementation
Book::Book(char *strng)
{
title = new char[strlen(strng)+1]; // allocate memory
strcpy(title,strng);
// store the string
}
void Book::showtitle()
{
cout << title << endl;
return;
}
int main()
{
Book book1("DOS Primer"); // create 1st title
Book book2("A Brief History of Western Civilization");
book1.showtitle(); // display book1's title
book2.showtitle(); // display book2's title
return 0;
The output of the above program:
}
DOS Primer
A Brief History of Western Civilization
Programming Fundamentals
34