You are on page 1of 64

Classes and Objects

Rizoan Toufiq1

1 Assistant Professor

Department of Computer Science & Engineering


Rajshahi University of Engineering & Technology
rizoantoufiq@yahoo.com

Course Title: Object Oriented Programming

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 1 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 2 / 64
Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 3 / 64
Structures

#include <iostream>
using namespace std;

//Structure for a bank certificate of deposit:


struct CDAccount
{
double balance;
double interestRate;
int term; //months until maturity
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 4 / 64


Structures

//Program to demonstrate the CDAccount structure type.


#include <iostream>
using namespace std;
//Structure for a bank certificate of deposit:
struct CDAccount
{
double balance;
double interestRate;
int term; //months until maturity
void getData(){
cout << "Enter account balance: $";
cin >> balance;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 5 / 64


Structures

cout << "Enter account interest rate: ";


cin >> interestRate;
cout << "Enter the number of months until maturity\n"
<< "(must be 12 or fewer months): ";
cin >> term;

}
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 6 / 64


Structures

int main( )
{
CDAccount account;
account.getData();

double rateFraction, interest;


rateFraction = account.interestRate / 100.0;
interest = account.balance * rateFraction
*(account.term / 12.0);
account.balance = account.balance + interest;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 7 / 64


Structures

cout << "When your CD matures in "


<< account.term << " months,\n"
<< "it will have a balance of $"
<< account.balance << endl;
return 0;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 8 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 9 / 64
Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 10 / 64
Classes

A class is a data type whose variables are objects.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 11 / 64


Classes

#include <iostream>
using namespace std;
class DayOfYear
{
public:
void output( );
int month;
int day;
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 12 / 64


Classes

//Uses iostream:
void DayOfYear::output( )
{
cout << "month = " << month
<< ", day = " << day << endl;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 13 / 64


Classes

int main( )
{
DayOfYear today, birthday;

cout << "Enter today’s date:\n";


cout << "Enter month as a number: ";
cin >> today.month;
cout << "Enter the day of the month: ";
cin >> today.day;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 14 / 64


Classes

cout << "Enter your birthday:\n";


cout << "Enter month as a number: ";
cin >> birthday.month;
cout << "Enter the day of the month: ";
cin >> birthday.day;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 15 / 64


Classes

cout << "Today’s date is ";


today.output( );

cout << "Your birthday is ";


birthday.output( );

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 16 / 64


Classes

if (today.month == birthday.month
&& today.day == birthday.day)
cout << "Happy Birthday!\n";
else
cout << "Happy Unbirthday!\n";
return 0;
}

exampleClass01.cpp

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 17 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 18 / 64
Public and Private

#include <iostream>
using namespace std;
class DayOfYear
{
public:
void input( );
void output( );
void set(int newMonth, int newDay);
int getMonth( );
int getDay( );

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 19 / 64


Public and Private

private:
void checkDate( );
int month;
int day;
};

void checkDate( ) ⇒ Private Member Functions


int month⇒ Private Member Variable
int day⇒ Private Member Variable

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 20 / 64


Public and Private

void DayOfYear::checkDate( ){
if ((month < 1) || (month > 12) || (day < 1) ||
(day > 31)){
cout << "Illegal date. Aborting program.\n";
exit(1);
}
}

Private members may be used in member function definitions (but


not elsewhere)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 21 / 64


Public and Private

void DayOfYear::input( )
{
cout << "Enter the month as a number: ";
cin >> month; //Private Member Variable
cout << "Enter the day of the month: ";
cin >> day;//Private Member Variable
checkDate( ); //Private Member Function
}

Private members may be used in member function definitions (but


not elsewhere)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 22 / 64


Public and Private

void DayOfYear::output( ){
cout << "month = " << month
<< ", day = " << day << endl;
}

Private members may be used in member function definitions (but


not elsewhere)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 23 / 64


Public and Private

void DayOfYear::set(int newMonth, int newDay){


month = newMonth;
day = newDay;
checkDate();
}

Private members may be used in member function definitions (but


not elsewhere)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 24 / 64


Public and Private

int DayOfYear::getMonth( ){
return month; //Can’t access directly by object
}

int DayOfYear::getDay( ){
return day; //Can’t access directly by object
}

Private members may be used in member function definitions (but


not elsewhere)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 25 / 64


Public and Private

int main( ){
DayOfYear today, bachBirthday;

cout << "Enter today’s date:\n";


today.input( );
cout << "Today’s date is ";
today.output( );

bachBirthday.set(3, 21);
cout << "J. S. Bach’s birthday is ";
bachBirthday.output( );

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 26 / 64


Public and Private

if (today.getMonth( ) == bachBirthday.getMonth( ) &&


today.getDay( ) == bachBirthday.getDay( ) )
cout << "Happy Birthday Johann Sebastian!\n";
else
cout << "Happy Unbirthday Johann Sebastian!\n";
return 0;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 27 / 64


Encapsulation

Encapsulation is also known as data hiding.


It is the technique of making the variables in a class private and accessi-
ble only by a controlled interface of public or protected functions.
This allows you to modify the internal variables and data structures
without breaking the code that uses your class.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 28 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 29 / 64
Programming Tip: Make All Member Variables Private

When defining a class, the normal practice is to make all member


variables private.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 30 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 31 / 64
Programming Tip: Define Accessor and Mutator Functions

Member functions that allow you to find out the values of the private
member variables of a class are called accessor functions.
Member functions that allow you to change the values of the private
member variables of a class are called mutator functions.
Good Practice: the names of accessor functions normally include
the word ’get’
Good Practice: the names of mutator functions normally include
the word ’set’

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 32 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 33 / 64
Programming Tip: Use the Assignment Operator

DayOfYear dueDate, tomorrow;


dueDate = tomorrow;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 34 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 35 / 64
Summary of Some Properties of Classes

Classes have both member variables and member functions.


A member (either a member variable or a member function) may be
either public or private.
Normally, all the member variables of a class are labeled as private
members.
A private member of a class cannot be used except within the defini-
tion of another member function of the same class.
The name of a member function for a class may be overloaded just
like the name of an ordinary function.
A class may use another class as the type for a member variable.
A function may have formal parameters whose types are classes.
A function may return an object.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 36 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 37 / 64
Constructors for Initialization

A constructor must have the same name as the class.


A constructor definition cannot return a value. Moreover, no return
type, not even void, can be given at the start of the function decla-
ration or in the function header.
A constructor is called automatically when an object of the class is
declared.
Constructors are used to initialize objects.
Remember:
BankAccount account2(); //WRONG! DO NOT DO THIS!
Correct:
BankAccount account2;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 38 / 64


Constructors for Initialization

#include <iostream>
using namespace std;
class DayOfYear{
public:
DayOfYear();
DayOfYear(int m, int d);
void output();
private:
int month;
int day;
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 39 / 64


Constructors for Initialization

DayOfYear::DayOfYear(){
month = 7;
day = 6;
}

DayOfYear::DayOfYear(int m, int d){


month = m;
day = d;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 40 / 64


Constructors for Initialization

void DayOfYear::output(){
cout << "month = " << month
<< ", day = " << day << endl;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 41 / 64


Constructors for Initialization

int main(){
DayOfYear yourBirthday(5,10),testbirthday;
cout << "Your birthday is ";
yourBirthday.output( );

testbirthday.output();

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 42 / 64


Constructors for Initialization

//Explicitly call constructor


yourBirthday = DayOfYear(10,10);
yourBirthday.output();

DayOfYear *ptr;
ptr = new DayOfYear(6,6);
ptr->output();
return 0;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 43 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 44 / 64
Programming Tip: Always Include a Default Constructor

class DayOfYear{
public:
DayOfYear();
DayOfYear(int m, int d);
void output();
private:
int month;
int day;
};

DayOfYear(); ⇒ Default Constructor

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 45 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 46 / 64
Abstract Data Types

ADT
A data type is called an abstract data type (abbreviated ADT) if the
programmers who use the type do not have access to the details of how
the values and operations are implemented.
The predefined types, such as int, are abstract data types (ADTs). You
do not know how the operations, such as + and *, are implemented
for the type int.
Even if you did know, you would not use this information in any C++
program.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 47 / 64


Abstract Data Types

Programmer-defined types, such as the structure types and class types,


are not automatically ADTs.
In C++, achieve ADT’s properties by classes but not every class is an
ADT.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 48 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 49 / 64
Classes to Produce Abstract Data Types

class BankAccount
{
public:
BankAccount(int dollars, int cents, double rate);
BankAccount(int dollars, double rate);
BankAccount();
void set(int dollars, int cents, double rate);
void set(int dollars, double rate);
void update();
double getBalance();
double getRate();
void output(ostream& outs);

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 50 / 64


Classes to Produce Abstract Data Types

private:
double balance;
double interestRate;
double fraction(double percent);
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 51 / 64


Classes to Produce Abstract Data Types

The programmer who uses the type BankAccount need not know how
you implemented the definition of BankAccount::update or any of
the other member functions.

void BankAccount::update()
{
balance = balance + fraction(interestRate) * balance;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 52 / 64


How to write an ADT

Make all the member variables private members of the class.


Make each of the basic operations that the programmer needs a public
member function of the class, and fully specify how to use each such
public member function
Make any helping functions private member functions.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 53 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 54 / 64
Introduction to Inheritance

When one class is inherited by another,


The class is inherited is called the base class.
The inheriting class is called the derived class.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 55 / 64


Introduction to Inheritance

#include <iostream>
using namespace std;
//define base class
class BankAccount{
double balance;
public:
void set_balance(double bal);
double get_balance();
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 56 / 64


Introduction to Inheritance

//define derived class


class SavingsAccount:public BankAccount{
double savingsbalance;
public:
void set_savingsbalance(double sbal);
void output();
};

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 57 / 64


Introduction to Inheritance

void BankAccount::set_balance(double bal){


balance = bal;
}
double BankAccount::get_balance(){
return balance;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 58 / 64


Introduction to Inheritance

void SavingsAccount::set_savingsbalance(double sbal){


savingsbalance = sbal;
}
void SavingsAccount::output(){
cout<< "Bank Balance: "<<get_balance()<<"\n";
cout<< "Saving Balance: "<<savingsbalance<<"\n";
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 59 / 64


Introduction to Inheritance

int main( ){
SavingsAccount account;
account.set_balance(2000.00);
account.set_savingsbalance(3000.00);
account.output();
return 0;
}

The keyword public tells the compiler that BankAccount will be in-
herited such that all public elements of the base class will be public
element of the derived class.
All private elements of the base class remains private to it and are not
directly accessible by the derived class.

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 60 / 64


Overview

1 Structure
2 Classes
Defining Classes and Member Functions
Public and Private
Programming Tip: Make All Member Variables Private
Programming Tip: Define Accessor and Mutator Functions
Programming Tip: Use the Assignment Operator with Objects
Summary of Some Properties of Classes
Constructors for Initialization
Programming Tip: Always Include a Default Constructor
3 Abstract Data Types
Classes to Produce Abstract Data Types
4 Introduction to Inheritance
5 Resources
Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 61 / 64
Read

Walter Savitch,
Problem Solving with C++
- Chapter 10

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 62 / 64


Resources

Walter Savitch
Problem Solving with C++
Herbert Schildt
Teach Yourself C++

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 63 / 64


The End

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Classes and Objects 64 / 64

You might also like